Skip to content

Commit 288cd7a

Browse files
authored
Merge pull request #752 from fluxcd/handle-in-cluster-annotation
Return a DiffTypeExclude when exclusion annotation is set in cluster
2 parents 46467e7 + 4971c85 commit 288cd7a

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

ssa/jsondiff/unstructured.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ func Unstructured(ctx context.Context, c client.Client, obj *unstructured.Unstru
137137
return nil, err
138138
}
139139

140+
// Short-circuit if an annotation is set to ignore the resource in-cluster.
141+
if utils.AnyInMetadata(existingObj, o.ExclusionSelector) {
142+
return NewDiffForUnstructured(obj, nil, DiffTypeExclude, nil), nil
143+
}
144+
140145
dryRunObj := obj.DeepCopy()
141146
patchOpts := []client.PatchOption{
142147
client.DryRunAll,

ssa/jsondiff/unstructured_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,43 @@ func TestUnstructuredList(t *testing.T) {
175175
}
176176
},
177177
},
178+
{
179+
name: "excludes resources with matching annotation set in the cluster",
180+
paths: []string{
181+
"testdata/deployment.yaml",
182+
"testdata/service.yaml",
183+
},
184+
mutateCluster: func(obj *unstructured.Unstructured) {
185+
if obj.GetKind() != "Service" {
186+
return
187+
}
188+
189+
annotations := obj.GetAnnotations()
190+
if annotations == nil {
191+
annotations = make(map[string]string)
192+
}
193+
annotations["exclude"] = "enabled"
194+
obj.SetAnnotations(annotations)
195+
196+
_ = unstructured.SetNestedField(obj.Object, "NodePort", "spec", "type")
197+
},
198+
opts: []ListOption{
199+
ExclusionSelector{"exclude": "enabled"},
200+
},
201+
want: func(desired, cluster []*unstructured.Unstructured) DiffSet {
202+
return DiffSet{
203+
&Diff{
204+
Type: DiffTypeNone,
205+
DesiredObject: desired[0],
206+
ClusterObject: cluster[0],
207+
},
208+
&Diff{
209+
Type: DiffTypeExclude,
210+
DesiredObject: desired[1],
211+
},
212+
}
213+
},
214+
},
178215
{
179216
name: "ignores JSON pointers for resources matching selector",
180217
paths: []string{

0 commit comments

Comments
 (0)