Skip to content

Commit

Permalink
Omit checksum label if GC is disabled
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Nov 3, 2020
1 parent 5f69745 commit 46f828f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
10 changes: 4 additions & 6 deletions controllers/kustomization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions controllers/kustomization_gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
9 changes: 8 additions & 1 deletion controllers/kustomization_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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},
},
Expand Down

0 comments on commit 46f828f

Please sign in to comment.