Skip to content

Commit

Permalink
Merge pull request #162 from fluxcd/omit-gc-label
Browse files Browse the repository at this point in the history
Omit checksum label if GC is disabled
  • Loading branch information
stefanprodan authored Nov 3, 2020
2 parents 5f69745 + d8caeb7 commit 0f635d2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 52 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
45 changes: 0 additions & 45 deletions controllers/kustomization_request.go

This file was deleted.

21 changes: 21 additions & 0 deletions docs/spec/v1beta1/kustomization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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="<Kustomization name>" \
-l=kustomize.toolkit.fluxcd.io/namespace="<Kustomization namespace>"
```

## Garbage collection

To enable garbage collection, set `spec.prune` to `true`.
Expand All @@ -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: "<Kustomization name>"
kustomize.toolkit.fluxcd.io/namespace: "<Kustomization namespace>"
kustomize.toolkit.fluxcd.io/checksum: "<manifests 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
Expand Down

0 comments on commit 0f635d2

Please sign in to comment.