|
| 1 | +package kube_janitor |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log/slog" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/prometheus/client_golang/prometheus" |
| 9 | + prometheusCommon "github.com/webdevops/go-common/prometheus" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 12 | +) |
| 13 | + |
| 14 | +func (j *Janitor) runTtlResources() error { |
| 15 | + j.connect() |
| 16 | + |
| 17 | + ctx := context.Background() |
| 18 | + |
| 19 | + metricResourceTtl := prometheusCommon.NewMetricsList() |
| 20 | + |
| 21 | + labelSelector := "" |
| 22 | + // we can use a label selector if ttl is configured by label |
| 23 | + if j.config.Ttl.Label != "" { |
| 24 | + labelSelector = j.config.Ttl.Label |
| 25 | + } |
| 26 | + |
| 27 | + for _, resourceType := range j.config.Ttl.Resources { |
| 28 | + gvkLogger := j.logger.With(slog.Any("gvk", resourceType)) |
| 29 | + gvkLogger.Debug("checking resources") |
| 30 | + |
| 31 | + err := j.kubeEachResource(ctx, resourceType.AsGVR(), labelSelector, func(resource unstructured.Unstructured) error { |
| 32 | + var ttlValue string |
| 33 | + |
| 34 | + if j.config.Ttl.Annotation != "" { |
| 35 | + // get from meta.annotations |
| 36 | + if val, exists := resource.GetAnnotations()[j.config.Ttl.Annotation]; exists { |
| 37 | + ttlValue = strings.TrimSpace(val) |
| 38 | + } |
| 39 | + } else if j.config.Ttl.Label != "" { |
| 40 | + // get from meta.labels |
| 41 | + if val, exists := resource.GetLabels()[j.config.Ttl.Label]; exists { |
| 42 | + ttlValue = strings.TrimSpace(val) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // check if we got a valid ttl value |
| 47 | + if ttlValue == "" { |
| 48 | + return nil |
| 49 | + } |
| 50 | + |
| 51 | + resourceLogger := gvkLogger.With( |
| 52 | + slog.String("namespace", resource.GetNamespace()), |
| 53 | + slog.String("resource", resource.GetName()), |
| 54 | + slog.String("ttl", ttlValue), |
| 55 | + ) |
| 56 | + |
| 57 | + parsedDate, expired, err := j.checkExpiryDate(resource.GetCreationTimestamp().Time, ttlValue) |
| 58 | + if err != nil { |
| 59 | + resourceLogger.Error("unable to parse expiration date", slog.String("raw", ttlValue), slog.Any("error", err)) |
| 60 | + return nil |
| 61 | + } |
| 62 | + |
| 63 | + metricResourceTtl.AddTime( |
| 64 | + prometheus.Labels{ |
| 65 | + "version": resource.GetAPIVersion(), |
| 66 | + "kind": resource.GetKind(), |
| 67 | + "namespace": resource.GetNamespace(), |
| 68 | + "name": resource.GetName(), |
| 69 | + "ttl": ttlValue, |
| 70 | + }, |
| 71 | + *parsedDate, |
| 72 | + ) |
| 73 | + |
| 74 | + resourceLogger.Debug("found resource with valid TTL", slog.Time("expirationDate", *parsedDate)) |
| 75 | + |
| 76 | + if expired { |
| 77 | + if j.dryRun { |
| 78 | + resourceLogger.Info("resource is expired, would delete resource (DRY-RUN)", slog.Time("expirationDate", *parsedDate)) |
| 79 | + } else { |
| 80 | + resourceLogger.Info("deleting expired resource", slog.Time("expirationDate", *parsedDate)) |
| 81 | + err := j.dynClient.Resource(resourceType.AsGVR()).Namespace(resource.GetNamespace()).Delete(ctx, resource.GetName(), metav1.DeleteOptions{}) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return nil |
| 89 | + }) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + metricResourceTtl.GaugeSet(j.prometheus.ttl) |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
0 commit comments