Skip to content

Commit

Permalink
Cleanup code base
Browse files Browse the repository at this point in the history
  • Loading branch information
mvshao committed Sep 18, 2024
1 parent 672d04c commit 8bcc685
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
45 changes: 20 additions & 25 deletions hack/runtime-cleanup/cmd/cleaner/cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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"
}
31 changes: 29 additions & 2 deletions hack/runtime-cleanup/cmd/main.go
Original file line number Diff line number Diff line change
@@ -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})
}

0 comments on commit 8bcc685

Please sign in to comment.