From 46f828ff431ac20e466e4ae59060fdbe004e2c54 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Tue, 3 Nov 2020 14:25:32 +0200 Subject: [PATCH 1/3] Omit checksum label if GC is disabled Signed-off-by: Stefan Prodan --- controllers/kustomization_controller.go | 10 ++++------ controllers/kustomization_gc.go | 7 +++++++ controllers/kustomization_generator.go | 9 ++++++++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/controllers/kustomization_controller.go b/controllers/kustomization_controller.go index 13685e10..29d5a445 100644 --- a/controllers/kustomization_controller.go +++ b/controllers/kustomization_controller.go @@ -186,7 +186,7 @@ func (r *KustomizationReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro // set the reconciliation status to progressing kustomization = kustomizev1.KustomizationProgressing(kustomization) if err := r.Status().Update(ctx, &kustomization); err != nil { - log.Error(err, "unable to update status") + log.Error(err, "unable to update status to progressing") return ctrl.Result{Requeue: true}, err } r.recordReadiness(kustomization, false) @@ -765,13 +765,11 @@ func (r *KustomizationReconciler) applyWithRetry(kustomization kustomizev1.Kusto } func (r *KustomizationReconciler) prune(client client.Client, kustomization kustomizev1.Kustomization, snapshot *kustomizev1.Snapshot, force bool) error { - if kustomization.Status.Snapshot == nil || snapshot == nil { + if !kustomization.Spec.Prune || kustomization.Status.Snapshot == nil || snapshot == nil { return nil } - if !force { - if kustomization.Status.Snapshot.Checksum == snapshot.Checksum { - return nil - } + if !force && kustomization.Status.Snapshot.Checksum == snapshot.Checksum { + return nil } gc := NewGarbageCollector(client, *kustomization.Status.Snapshot, r.Log) diff --git a/controllers/kustomization_gc.go b/controllers/kustomization_gc.go index d5fbf43e..bd090f70 100644 --- a/controllers/kustomization_gc.go +++ b/controllers/kustomization_gc.go @@ -131,3 +131,10 @@ func gcLabels(name, namespace, checksum string) map[string]string { fmt.Sprintf("%s/checksum", kustomizev1.GroupVersion.Group): checksum, } } + +func selectorLabels(name, namespace string) map[string]string { + return map[string]string{ + fmt.Sprintf("%s/name", kustomizev1.GroupVersion.Group): name, + fmt.Sprintf("%s/namespace", kustomizev1.GroupVersion.Group): namespace, + } +} diff --git a/controllers/kustomization_generator.go b/controllers/kustomization_generator.go index 227dda57..dc07caab 100644 --- a/controllers/kustomization_generator.go +++ b/controllers/kustomization_generator.go @@ -214,6 +214,13 @@ func (kg *KustomizeGenerator) checksum(dirPath string) (string, error) { } func (kg *KustomizeGenerator) generateLabelTransformer(checksum, dirPath string) error { + labels := selectorLabels(kg.kustomization.GetName(), kg.kustomization.GetNamespace()) + + // add checksum label only if GC is enabled + if kg.kustomization.Spec.Prune { + labels = gcLabels(kg.kustomization.GetName(), kg.kustomization.GetNamespace(), checksum) + } + var lt = struct { ApiVersion string `json:"apiVersion" yaml:"apiVersion"` Kind string `json:"kind" yaml:"kind"` @@ -230,7 +237,7 @@ func (kg *KustomizeGenerator) generateLabelTransformer(checksum, dirPath string) }{ Name: kg.kustomization.GetName(), }, - Labels: gcLabels(kg.kustomization.GetName(), kg.kustomization.GetNamespace(), checksum), + Labels: labels, FieldSpecs: []kustypes.FieldSpec{ {Path: "metadata/labels", CreateIfNotPresent: true}, }, From 97cef5a84e54c6eb2a3e9eda776929d71d26de7b Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Tue, 3 Nov 2020 15:44:54 +0200 Subject: [PATCH 2/3] Add labels and selector example to docs Signed-off-by: Stefan Prodan --- docs/spec/v1beta1/kustomization.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/spec/v1beta1/kustomization.md b/docs/spec/v1beta1/kustomization.md index 56927e01..6b68cd0c 100644 --- a/docs/spec/v1beta1/kustomization.md +++ b/docs/spec/v1beta1/kustomization.md @@ -244,6 +244,14 @@ On-demand execution example: kubectl annotate --overwrite kustomization/podinfo reconcile.fluxcd.io/requestedAt="$(date +%s)" ``` +List all Kubernetes objects reconciled from a Kustomization: + +```sh +kubectl get all --all-namespaces \ +-l=kustomize.toolkit.fluxcd.io/name="" \ +-l=kustomize.toolkit.fluxcd.io/namespace="" +``` + ## Garbage collection To enable garbage collection, set `spec.prune` to `true`. @@ -253,6 +261,19 @@ but are missing from the current source revision, are removed from cluster autom Garbage collection is also performed when a Kustomization object is deleted, triggering a removal of all Kubernetes objects previously applied on the cluster. +To keep track of the Kubernetes objects reconciled from a Kustomization, the following labels +are injected into the manifests: + +```yaml +labels: + kustomize.toolkit.fluxcd.io/name: "" + kustomize.toolkit.fluxcd.io/namespace: "" + kustomize.toolkit.fluxcd.io/checksum: "" +``` + +The checksum label value is updated if the content of `spec.path` changes. +When pruning is disabled, the checksum label is omitted. + ## Health assessment A kustomization can contain a series of health checks used to determine the From d8caeb7cec3c53aa6aba501ee9bda41c16efa900 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Tue, 3 Nov 2020 16:07:44 +0200 Subject: [PATCH 3/3] Remove unused code Signed-off-by: Stefan Prodan --- controllers/kustomization_request.go | 45 ---------------------------- 1 file changed, 45 deletions(-) delete mode 100644 controllers/kustomization_request.go diff --git a/controllers/kustomization_request.go b/controllers/kustomization_request.go deleted file mode 100644 index 53c1899e..00000000 --- a/controllers/kustomization_request.go +++ /dev/null @@ -1,45 +0,0 @@ -/* -Copyright 2020 The Flux authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package controllers - -import ( - "context" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/util/retry" - "sigs.k8s.io/controller-runtime/pkg/client" - - kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1" - "github.com/fluxcd/pkg/apis/meta" -) - -func requestReconciliation(r client.Client, name types.NamespacedName) error { - var kustomization kustomizev1.Kustomization - return retry.RetryOnConflict(retry.DefaultBackoff, func() (err error) { - if err := r.Get(context.TODO(), name, &kustomization); err != nil { - return err - } - - if kustomization.Annotations == nil { - kustomization.Annotations = make(map[string]string) - } - kustomization.Annotations[meta.ReconcileRequestAnnotation] = metav1.Now().String() - err = r.Update(context.TODO(), &kustomization) - return - }) -}