Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WiP] Runtime cleaner for KIM #386

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/build_runtime-cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: runtime-cleanup Build

on:
push:
branches:
- main
paths:
- "hack/runtime-cleanup/**"
- "!hack/runtime-cleanup/**/*.md"
pull_request_target:
types: [opened, synchronize]
paths:
- "hack/runtime-cleanup/**"
- "!hack/runtime-cleanup/**/*.md"

permissions:
id-token: write # This is required for requesting the JWT token
contents: read # This is required for actions/checkout

jobs:
build-image:
uses: kyma-project/test-infra/.github/workflows/image-builder.yml@main # Usage: kyma-project/test-infra/.github/workflows/image-builder.yml@main
with:
name: runtime-cleanup
dockerfile: Dockerfile
context: hack/runtime-cleanup
export-tags: false
22 changes: 22 additions & 0 deletions hack/runtime-cleanup/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM golang:1.23.0-alpine3.20 as build
ARG TARGETOS
ARG TARGETARCH

WORKDIR /workdir

COPY go.mod go.mod
COPY go.sum go.sum
RUN go mod download

COPY cmd/ cmd/


ARG BIN
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o runtime-cleanup cmd/main.go

FROM gcr.io/distroless/static:nonroot
WORKDIR /
COPY --from=build /workdir/runtime-cleanup .
USER 65532:65532

ENTRYPOINT ["/runtime-cleanup"]
56 changes: 56 additions & 0 deletions hack/runtime-cleanup/cmd/cleaner/cleaner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cleaner

import (
"context"
imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
"log/slog"
"sigs.k8s.io/controller-runtime/pkg/client"
"time"
)

const deleteAfter = 1 * time.Second

type RuntimeCleaner struct {
k8sClient client.Client
log *slog.Logger
}

func NewRuntimeCleaner(k8sClient client.Client, log *slog.Logger) *RuntimeCleaner {
return &RuntimeCleaner{k8sClient: k8sClient, log: log}
}

func (r RuntimeCleaner) Execute() error {

err := r.removeOldRuntimes()
if err != nil {
r.log.With("error", err).Error("Error during removing old runtimes ")
return err
}
return nil
}

func (r RuntimeCleaner) removeOldRuntimes() error {
runtimes := &imv1.RuntimeList{}
if err := r.k8sClient.List(context.Background(), runtimes); err != nil {
return err
}

for _, runtimeObj := range runtimes.Items {
if isTimeForCleanup(runtimeObj) && isControlledByKIM(runtimeObj) && runtimeObj.DeletionTimestamp == nil {
err := r.k8sClient.Delete(context.Background(), &runtimeObj)
if err != nil {
return err
}
r.log.With("runtime", runtimeObj.Name).Info("Runtime was marked to be removed by KIM reconciler")
}
}
return nil
}

func isTimeForCleanup(runtimeObj imv1.Runtime) bool {
return runtimeObj.CreationTimestamp.Add(deleteAfter).Before(time.Now())
}

func isControlledByKIM(runtimeObj imv1.Runtime) bool {
return runtimeObj.Labels["kyma-project.io/controlled-by-provisioner"] == "false"
}
46 changes: 46 additions & 0 deletions hack/runtime-cleanup/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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"
"k8s.io/utils/ptr"
"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 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, DryRun: ptr.To(true)})
}
54 changes: 54 additions & 0 deletions hack/runtime-cleanup/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module github.com/kyma-project/infrastructure-manager/hack/runtime-cleanup

go 1.23.0

require (
github.com/kyma-project/infrastructure-manager v0.0.0-20240917080316-aff1b4fdda4a
k8s.io/client-go v0.31.1
sigs.k8s.io/controller-runtime v0.19.0
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/gardener/gardener v1.100.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.31.1 // indirect
k8s.io/apiextensions-apiserver v0.31.0 // indirect
k8s.io/apimachinery v0.31.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading
Loading