Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a DiffTypeExclude when exclusion annotation is set in cluster #752

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading