diff --git a/hack/runtime-cleanup/cmd/cleaner/cleaner.go b/hack/runtime-cleanup/cmd/cleaner/cleaner.go index f416640f..de68dc69 100644 --- a/hack/runtime-cleanup/cmd/cleaner/cleaner.go +++ b/hack/runtime-cleanup/cmd/cleaner/cleaner.go @@ -3,48 +3,39 @@ package cleaner import ( "context" imv1 "github.com/kyma-project/infrastructure-manager/api/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/client-go/rest" + "log/slog" "sigs.k8s.io/controller-runtime/pkg/client" "time" ) -func Execute() error { +type RuntimeCleaner struct { + k8sClient client.Client + log *slog.Logger +} + +func NewRuntimeCleaner(k8sClient client.Client, log *slog.Logger) *RuntimeCleaner { + return &RuntimeCleaner{k8sClient: k8sClient, log: log} +} - k8sClient, err := createKubernetesClient() +func (r RuntimeCleaner) Execute() error { + err := r.removeOldRuntimes() if err != nil { + r.log.Error("Error during removing old runtimes ", err) return err } - - err = removeOldRuntimes(k8sClient) - return nil } -func createKubernetesClient() (client.Client, error) { - config, err := rest.InClusterConfig() - if err != nil { - return nil, err - } - scheme := runtime.NewScheme() - err = imv1.AddToScheme(scheme) - if err != nil { - return nil, err - } - - return client.New(config, client.Options{Scheme: scheme}) -} - -func removeOldRuntimes(client client.Client) error { +func (r RuntimeCleaner) removeOldRuntimes() error { runtimes := &imv1.RuntimeList{} - if err := client.List(context.Background(), runtimes); err != nil { + if err := r.k8sClient.List(context.Background(), runtimes); err != nil { return err } for _, runtimeObj := range runtimes.Items { - if runtimeObj.CreationTimestamp.Add(24*time.Hour).Before(time.Now()) && isControlledByKIM(runtimeObj) { - err := client.Delete(context.Background(), &runtimeObj) + if isTimeForCleanup(runtimeObj) && isControlledByKIM(runtimeObj) { + err := r.k8sClient.Delete(context.Background(), &runtimeObj) if err != nil { return err } @@ -53,6 +44,10 @@ func removeOldRuntimes(client client.Client) error { return nil } +func isTimeForCleanup(runtimeObj imv1.Runtime) bool { + return runtimeObj.CreationTimestamp.Add(24 * time.Hour).Before(time.Now()) +} + func isControlledByKIM(runtimeObj imv1.Runtime) bool { return runtimeObj.Labels["kyma-project.io/controlled-by-provisioner"] == "false" } diff --git a/hack/runtime-cleanup/cmd/main.go b/hack/runtime-cleanup/cmd/main.go index be069372..b7285095 100644 --- a/hack/runtime-cleanup/cmd/main.go +++ b/hack/runtime-cleanup/cmd/main.go @@ -1,18 +1,45 @@ package main import ( + imv1 "github.com/kyma-project/infrastructure-manager/api/v1" "github.com/kyma-project/infrastructure-manager/hack/runtime-cleanup/cmd/cleaner" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/rest" "log/slog" "os" + "sigs.k8s.io/controller-runtime/pkg/client" ) func main() { textHandler := slog.NewTextHandler(os.Stdout, nil) log := slog.New(textHandler) - log.Info("Starting runtime cleanup") - err := cleaner.Execute() + log.Info("Starting runtime cleaner") + + k8sClient, err := createKubernetesClient() + + if err != nil { + log.Error("Error during creating k8s client ", err) + return + } + + runtimeCleaner := cleaner.NewRuntimeCleaner(k8sClient, log) + err = runtimeCleaner.Execute() if err != nil { log.Error("Error during running runtime cleanup ", err) } return } + +func createKubernetesClient() (client.Client, error) { + config, err := rest.InClusterConfig() + if err != nil { + return nil, err + } + scheme := runtime.NewScheme() + err = imv1.AddToScheme(scheme) + if err != nil { + return nil, err + } + + return client.New(config, client.Options{Scheme: scheme}) +}