Skip to content

Commit 1d3ddcb

Browse files
authored
Merge pull request #1652 from fluxcd/backport-1651-to-release/v1.8.x
[release/v1.8.x] Fix management of skipped objects
2 parents 3b5ad19 + 897f431 commit 1d3ddcb

6 files changed

Lines changed: 533 additions & 73 deletions

File tree

internal/controller/kustomization_controller.go

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -468,24 +468,13 @@ func (r *KustomizationReconciler) reconcile(
468468
}
469469

470470
// Validate and apply resources in stages.
471-
drifted, changeSetWithSkipped, err := r.apply(ctx, resourceManager, obj, revision, originRevision, objects)
471+
drifted, changeSet, err := r.apply(ctx, resourceManager, obj, revision, originRevision, objects)
472472
if err != nil {
473473
obj.Status.History.Upsert(checksum, time.Now(), time.Since(reconcileStart), meta.ReconciliationFailedReason, historyMeta)
474474
conditions.MarkFalse(obj, meta.ReadyCondition, meta.ReconciliationFailedReason, "%s", err)
475475
return err
476476
}
477477

478-
// Filter out skipped entries from the change set.
479-
changeSet := ssa.NewChangeSet()
480-
skippedSet := make(map[object.ObjMetadata]struct{})
481-
for _, entry := range changeSetWithSkipped.Entries {
482-
if entry.Action == ssa.SkippedAction {
483-
skippedSet[entry.ObjMetadata] = struct{}{}
484-
} else {
485-
changeSet.Add(entry)
486-
}
487-
}
488-
489478
// Create an inventory from the reconciled resources.
490479
newInventory := inventory.New()
491480
err = inventory.AddChangeSet(newInventory, changeSet)
@@ -499,7 +488,7 @@ func (r *KustomizationReconciler) reconcile(
499488
obj.Status.Inventory = newInventory
500489

501490
// Detect stale resources which are subject to garbage collection.
502-
staleObjects, err := inventory.Diff(oldInventory, newInventory, skippedSet)
491+
staleObjects, err := inventory.Diff(oldInventory, newInventory)
503492
if err != nil {
504493
obj.Status.History.Upsert(checksum, time.Now(), time.Since(reconcileStart), meta.ReconciliationFailedReason, historyMeta)
505494
conditions.MarkFalse(obj, meta.ReadyCondition, meta.ReconciliationFailedReason, "%s", err)
@@ -523,7 +512,7 @@ func (r *KustomizationReconciler) reconcile(
523512
originRevision,
524513
isNewRevision,
525514
drifted,
526-
changeSet.ToObjMetadataSet(),
515+
changeSet,
527516
ssautil.ExtractJobsWithTTL(objects)); err != nil {
528517

529518
if errors.Is(err, &runtimeCtrl.QueueEventSource{}) {
@@ -981,8 +970,19 @@ func (r *KustomizationReconciler) checkHealth(ctx context.Context,
981970
originRevision string,
982971
isNewRevision bool,
983972
drifted bool,
984-
objects object.ObjMetadataSet,
973+
changeSet *ssa.ChangeSet,
985974
jobsWithTTL object.ObjMetadataSet) error {
975+
976+
// We should not check the health of skipped objects, as they are chosen
977+
// to be ignored by the user and may not be in a healthy state.
978+
changeSetWithoutSkipped := ssa.NewChangeSet()
979+
for _, entry := range changeSet.Entries {
980+
if entry.Action != ssa.SkippedAction {
981+
changeSetWithoutSkipped.Add(entry)
982+
}
983+
}
984+
objects := changeSetWithoutSkipped.ToObjMetadataSet()
985+
986986
if len(obj.Spec.HealthChecks) == 0 && !obj.Spec.Wait {
987987
conditions.Delete(obj, meta.HealthyCondition)
988988
return nil
@@ -1066,16 +1066,7 @@ func (r *KustomizationReconciler) prune(ctx context.Context,
10661066

10671067
log := ctrl.LoggerFrom(ctx)
10681068

1069-
opts := ssa.DeleteOptions{
1070-
PropagationPolicy: metav1.DeletePropagationBackground,
1071-
Inclusions: manager.GetOwnerLabels(obj.Name, obj.Namespace),
1072-
Exclusions: map[string]string{
1073-
fmt.Sprintf("%s/prune", kustomizev1.GroupVersion.Group): kustomizev1.DisabledValue,
1074-
fmt.Sprintf("%s/reconcile", kustomizev1.GroupVersion.Group): kustomizev1.DisabledValue,
1075-
},
1076-
}
1077-
1078-
changeSet, err := manager.DeleteAll(ctx, objects, opts)
1069+
changeSet, err := deleteObjects(ctx, obj, manager, objects)
10791070
if err != nil {
10801071
return false, err
10811072
}
@@ -1160,16 +1151,7 @@ func (r *KustomizationReconciler) finalize(ctx context.Context,
11601151
Group: kustomizev1.GroupVersion.Group,
11611152
})
11621153

1163-
opts := ssa.DeleteOptions{
1164-
PropagationPolicy: metav1.DeletePropagationBackground,
1165-
Inclusions: resourceManager.GetOwnerLabels(obj.Name, obj.Namespace),
1166-
Exclusions: map[string]string{
1167-
fmt.Sprintf("%s/prune", kustomizev1.GroupVersion.Group): kustomizev1.DisabledValue,
1168-
fmt.Sprintf("%s/reconcile", kustomizev1.GroupVersion.Group): kustomizev1.DisabledValue,
1169-
},
1170-
}
1171-
1172-
changeSet, err := resourceManager.DeleteAll(ctx, objects, opts)
1154+
changeSet, err := deleteObjects(ctx, obj, resourceManager, objects)
11731155
if err != nil {
11741156
r.event(obj, obj.Status.LastAppliedRevision, obj.Status.LastAppliedOriginRevision, eventv1.EventSeverityError, "pruning for deleted resource failed", nil)
11751157
// Return the error so we retry the failed garbage collection
@@ -1344,6 +1326,26 @@ func (r *KustomizationReconciler) getProviderRESTConfigFetcher(obj *kustomizev1.
13441326
return provider
13451327
}
13461328

1329+
// deleteObjects deletes the given objects using the provided ResourceManager
1330+
// and returns a ChangeSet containing the metadata of the deleted objects.
1331+
func deleteObjects(
1332+
ctx context.Context,
1333+
obj *kustomizev1.Kustomization,
1334+
manager *ssa.ResourceManager,
1335+
objects []*unstructured.Unstructured,
1336+
) (*ssa.ChangeSet, error) {
1337+
opts := ssa.DeleteOptions{
1338+
PropagationPolicy: metav1.DeletePropagationBackground,
1339+
Inclusions: manager.GetOwnerLabels(obj.Name, obj.Namespace),
1340+
Exclusions: map[string]string{
1341+
fmt.Sprintf("%s/prune", kustomizev1.GroupVersion.Group): kustomizev1.DisabledValue,
1342+
fmt.Sprintf("%s/reconcile", kustomizev1.GroupVersion.Group): kustomizev1.DisabledValue,
1343+
fmt.Sprintf("%s/ssa", kustomizev1.GroupVersion.Group): kustomizev1.IgnoreValue,
1344+
},
1345+
}
1346+
return manager.DeleteAll(ctx, objects, opts)
1347+
}
1348+
13471349
// getOriginRevision returns the origin revision of the source artifact,
13481350
// or the empty string if it's not present, or if the artifact itself
13491351
// is not present.

internal/controller/kustomization_inventory_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,6 @@ data:
6363
key: "%[2]s"
6464
---
6565
apiVersion: v1
66-
kind: ConfigMap
67-
metadata:
68-
name: "%[1]s-ssa-ignore"
69-
annotations:
70-
# This tests that objects with the SSA ignore annotation are not stored in the inventory.
71-
kustomize.toolkit.fluxcd.io/ssa: ignore
72-
data:
73-
key: "%[2]s"
74-
---
75-
apiVersion: v1
7666
kind: Secret
7767
metadata:
7868
name: "%[1]s"

0 commit comments

Comments
 (0)