Skip to content

Commit

Permalink
Return a DiffTypeExclude when exclusion annotation is set in cluster
Browse files Browse the repository at this point in the history
We want to enable this behaviour for use cases where different
controllers must coordinate in order to mutate a resource.

Signed-off-by: Soule BA <[email protected]>
  • Loading branch information
souleb committed Apr 2, 2024
1 parent 46467e7 commit 4971c85
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ssa/jsondiff/unstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ func Unstructured(ctx context.Context, c client.Client, obj *unstructured.Unstru
return nil, err
}

// Short-circuit if an annotation is set to ignore the resource in-cluster.
if utils.AnyInMetadata(existingObj, o.ExclusionSelector) {
return NewDiffForUnstructured(obj, nil, DiffTypeExclude, nil), nil
}

dryRunObj := obj.DeepCopy()
patchOpts := []client.PatchOption{
client.DryRunAll,
Expand Down
37 changes: 37 additions & 0 deletions ssa/jsondiff/unstructured_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,43 @@ func TestUnstructuredList(t *testing.T) {
}
},
},
{
name: "excludes resources with matching annotation set in the cluster",
paths: []string{
"testdata/deployment.yaml",
"testdata/service.yaml",
},
mutateCluster: func(obj *unstructured.Unstructured) {
if obj.GetKind() != "Service" {
return
}

annotations := obj.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}
annotations["exclude"] = "enabled"
obj.SetAnnotations(annotations)

_ = unstructured.SetNestedField(obj.Object, "NodePort", "spec", "type")
},
opts: []ListOption{
ExclusionSelector{"exclude": "enabled"},
},
want: func(desired, cluster []*unstructured.Unstructured) DiffSet {
return DiffSet{
&Diff{
Type: DiffTypeNone,
DesiredObject: desired[0],
ClusterObject: cluster[0],
},
&Diff{
Type: DiffTypeExclude,
DesiredObject: desired[1],
},
}
},
},
{
name: "ignores JSON pointers for resources matching selector",
paths: []string{
Expand Down

0 comments on commit 4971c85

Please sign in to comment.