Skip to content

Commit e53d4a1

Browse files
committed
Fix skipped entries from SSA being stored in the inventory
Signed-off-by: Matheus Pimenta <[email protected]>
1 parent 2566c69 commit e53d4a1

File tree

6 files changed

+54
-9
lines changed

6 files changed

+54
-9
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ require (
2929
github.com/fluxcd/pkg/http/fetch v0.19.0
3030
github.com/fluxcd/pkg/kustomize v1.22.0
3131
github.com/fluxcd/pkg/runtime v0.86.0
32-
github.com/fluxcd/pkg/ssa v0.56.0
32+
github.com/fluxcd/pkg/ssa v0.56.1-0.20250922213907-018ecdf4fc5f
3333
github.com/fluxcd/pkg/tar v0.14.0
3434
github.com/fluxcd/pkg/testserver v0.13.0
3535
github.com/fluxcd/source-controller/api v1.7.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ github.com/fluxcd/pkg/runtime v0.86.0 h1:q7aBSerJwt0N9hpurPVElG+HWpVhZcs6t96bcNQ
211211
github.com/fluxcd/pkg/runtime v0.86.0/go.mod h1:Wt9mUzQgMPQMu2D/wKl5pG4zh5vu/tfF5wq9pPobxOQ=
212212
github.com/fluxcd/pkg/sourceignore v0.14.0 h1:ZiZzbXtXb/Qp7I7JCStsxOlX8ri8rWwCvmvIrJ0UzQQ=
213213
github.com/fluxcd/pkg/sourceignore v0.14.0/go.mod h1:E3zKvyTyB+oQKqm/2I/jS6Rrt3B7fNuig/4bY2vi3bg=
214-
github.com/fluxcd/pkg/ssa v0.56.0 h1:OuWTPr0kI0alQYX1B3byJmUQol4BrpnrsXOoBmaTCPY=
215-
github.com/fluxcd/pkg/ssa v0.56.0/go.mod h1:iN/QDMqdJaVXKkqwbXqGa4PyWQwtyIy2WkeM2+9kfXA=
214+
github.com/fluxcd/pkg/ssa v0.56.1-0.20250922213907-018ecdf4fc5f h1:NDWjceiwSc1c0FNPWlop0FrLeCWT9yryQKKYxodTUMo=
215+
github.com/fluxcd/pkg/ssa v0.56.1-0.20250922213907-018ecdf4fc5f/go.mod h1:iN/QDMqdJaVXKkqwbXqGa4PyWQwtyIy2WkeM2+9kfXA=
216216
github.com/fluxcd/pkg/tar v0.14.0 h1:9Gku8FIvPt2bixKldZnzXJ/t+7SloxePlzyVGOK8GVQ=
217217
github.com/fluxcd/pkg/tar v0.14.0/go.mod h1:+rOWYk93qLEJ8WwmkvJOkB8i0dna1mrwJFybE8i9Udo=
218218
github.com/fluxcd/pkg/testserver v0.13.0 h1:xEpBcEYtD7bwvZ+i0ZmChxKkDo/wfQEV3xmnzVybSSg=

internal/controller/kustomization_controller.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,13 +441,24 @@ func (r *KustomizationReconciler) reconcile(
441441
}
442442

443443
// Validate and apply resources in stages.
444-
drifted, changeSet, err := r.apply(ctx, resourceManager, obj, revision, originRevision, objects)
444+
drifted, changeSetWithSkipped, err := r.apply(ctx, resourceManager, obj, revision, originRevision, objects)
445445
if err != nil {
446446
obj.Status.History.Upsert(checksum, time.Now(), time.Since(reconcileStart), meta.ReconciliationFailedReason, historyMeta)
447447
conditions.MarkFalse(obj, meta.ReadyCondition, meta.ReconciliationFailedReason, "%s", err)
448448
return err
449449
}
450450

451+
// Filter out skipped entries from the change set.
452+
changeSet := ssa.NewChangeSet()
453+
skippedSet := make(map[object.ObjMetadata]struct{})
454+
for _, entry := range changeSetWithSkipped.Entries {
455+
if entry.Action == ssa.SkippedAction {
456+
skippedSet[entry.ObjMetadata] = struct{}{}
457+
} else {
458+
changeSet.Add(entry)
459+
}
460+
}
461+
451462
// Create an inventory from the reconciled resources.
452463
newInventory := inventory.New()
453464
err = inventory.AddChangeSet(newInventory, changeSet)
@@ -461,7 +472,7 @@ func (r *KustomizationReconciler) reconcile(
461472
obj.Status.Inventory = newInventory
462473

463474
// Detect stale resources which are subject to garbage collection.
464-
staleObjects, err := inventory.Diff(oldInventory, newInventory)
475+
staleObjects, err := inventory.Diff(oldInventory, newInventory, skippedSet)
465476
if err != nil {
466477
obj.Status.History.Upsert(checksum, time.Now(), time.Since(reconcileStart), meta.ReconciliationFailedReason, historyMeta)
467478
conditions.MarkFalse(obj, meta.ReadyCondition, meta.ReconciliationFailedReason, "%s", err)

internal/controller/kustomization_inventory_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ 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
6676
kind: Secret
6777
metadata:
6878
name: "%[1]s"

internal/inventory/inventory.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ func ListMetadata(inv *kustomizev1.ResourceInventory) (object.ObjMetadataSet, er
9494
return metas, nil
9595
}
9696

97-
// Diff returns the slice of objects that do not exist in the target inventory.
98-
func Diff(inv *kustomizev1.ResourceInventory, target *kustomizev1.ResourceInventory) ([]*unstructured.Unstructured, error) {
97+
// Diff returns the slice of objects that do not exist in the target inventory,
98+
// ignoring those in the skippedSet.
99+
func Diff(inv *kustomizev1.ResourceInventory, target *kustomizev1.ResourceInventory,
100+
skippedSet map[object.ObjMetadata]struct{}) ([]*unstructured.Unstructured, error) {
101+
99102
versionOf := func(i *kustomizev1.ResourceInventory, objMetadata object.ObjMetadata) string {
100103
for _, entry := range i.Entries {
101104
if entry.ID == objMetadata.String() {
@@ -106,10 +109,16 @@ func Diff(inv *kustomizev1.ResourceInventory, target *kustomizev1.ResourceInvent
106109
}
107110

108111
objects := make([]*unstructured.Unstructured, 0)
109-
aList, err := ListMetadata(inv)
112+
aListWithSkipped, err := ListMetadata(inv)
110113
if err != nil {
111114
return nil, err
112115
}
116+
var aList object.ObjMetadataSet
117+
for _, m := range aListWithSkipped {
118+
if _, found := skippedSet[m]; !found {
119+
aList = append(aList, m)
120+
}
121+
}
113122

114123
bList, err := ListMetadata(target)
115124
if err != nil {

internal/inventory/inventory_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/fluxcd/pkg/ssa"
2525
ssautil "github.com/fluxcd/pkg/ssa/utils"
2626
. "github.com/onsi/gomega"
27+
"k8s.io/apimachinery/pkg/runtime/schema"
2728

2829
"github.com/fluxcd/cli-utils/pkg/object"
2930
)
@@ -60,11 +61,25 @@ func Test_Inventory(t *testing.T) {
6061
})
6162

6263
t.Run("diff objects in inventory", func(t *testing.T) {
63-
unList, err := Diff(inv2, inv1)
64+
unList, err := Diff(inv2, inv1, nil)
6465
g.Expect(err).ToNot(HaveOccurred())
6566
g.Expect(len(unList)).To(BeIdenticalTo(1))
6667
g.Expect(unList[0].GetName()).To(BeIdenticalTo("test2"))
6768
})
69+
70+
t.Run("diff objects in inventory ignoring skipped", func(t *testing.T) {
71+
skipped := object.ObjMetadata{
72+
Name: "test2",
73+
Namespace: "test",
74+
GroupKind: schema.GroupKind{
75+
Group: "",
76+
Kind: "ConfigMap",
77+
},
78+
}
79+
unList, err := Diff(inv2, inv1, map[object.ObjMetadata]struct{}{skipped: {}})
80+
g.Expect(err).ToNot(HaveOccurred())
81+
g.Expect(len(unList)).To(BeIdenticalTo(0))
82+
})
6883
}
6984

7085
func readManifest(manifest string) (*ssa.ChangeSet, error) {

0 commit comments

Comments
 (0)