Skip to content

Commit

Permalink
Runtime cleaner for KIM
Browse files Browse the repository at this point in the history
  • Loading branch information
mvshao committed Sep 17, 2024
1 parent aff1b4f commit 672d04c
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 0 deletions.
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"]
58 changes: 58 additions & 0 deletions hack/runtime-cleanup/cmd/cleaner/cleaner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cleaner

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

func Execute() error {

k8sClient, err := createKubernetesClient()

if err != nil {
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 {
runtimes := &imv1.RuntimeList{}
if err := client.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 err != nil {
return err
}
}
}
return nil
}

func isControlledByKIM(runtimeObj imv1.Runtime) bool {
return runtimeObj.Labels["kyma-project.io/controlled-by-provisioner"] == "false"
}
18 changes: 18 additions & 0 deletions hack/runtime-cleanup/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/kyma-project/infrastructure-manager/hack/runtime-cleanup/cmd/cleaner"
"log/slog"
"os"
)

func main() {
textHandler := slog.NewTextHandler(os.Stdout, nil)
log := slog.New(textHandler)
log.Info("Starting runtime cleanup")
err := cleaner.Execute()
if err != nil {
log.Error("Error during running runtime cleanup ", err)
}
return
}
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

0 comments on commit 672d04c

Please sign in to comment.