Skip to content

Commit dd54c6a

Browse files
committed
Align ResourceManager.Diff Skipping resources with ResourceManager.Apply
Signed-off-by: Yvan <[email protected]>
1 parent e454462 commit dd54c6a

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

ssa/manager_diff.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ import (
3232
// DiffOptions contains options for server-side dry-run apply requests.
3333
type DiffOptions struct {
3434
// Exclusions determines which in-cluster objects are skipped from dry-run apply
35-
// based on the specified key-value pairs.
36-
// A nil Exclusions map means all objects are applied
37-
// regardless of their metadata labels and annotations.
35+
// based on the matching labels or annotations.
3836
Exclusions map[string]string `json:"exclusions"`
37+
38+
// IfNotPresentSelector determines which in-cluster objects are skipped from dry-run apply
39+
// based on the matching labels or annotations.
40+
IfNotPresentSelector map[string]string `json:"ifNotPresentSelector"`
3941
}
4042

4143
// DefaultDiffOptions returns the default dry-run apply options.
@@ -57,7 +59,7 @@ func (m *ResourceManager) Diff(ctx context.Context, object *unstructured.Unstruc
5759
existingObject.SetGroupVersionKind(object.GroupVersionKind())
5860
_ = m.client.Get(ctx, client.ObjectKeyFromObject(object), existingObject)
5961

60-
if existingObject != nil && utils.AnyInMetadata(existingObject, opts.Exclusions) {
62+
if m.shouldSkipDiff(object, existingObject, opts) {
6163
return m.changeSetEntry(existingObject, SkippedAction), nil, nil, nil
6264
}
6365

@@ -123,3 +125,22 @@ func prepareObjectForDiff(object *unstructured.Unstructured) *unstructured.Unstr
123125
}
124126
return deepCopy
125127
}
128+
129+
// shouldSkipDiff determines based on the object metadata and DiffOptions if the object should be skipped.
130+
// An object is not applied if it contains a label or annotation
131+
// which matches the DiffOptions.Exclusions or DiffOptions.IfNotPresentSelector.
132+
func (m *ResourceManager) shouldSkipDiff(desiredObject *unstructured.Unstructured,
133+
existingObject *unstructured.Unstructured, opts DiffOptions) bool {
134+
if utils.AnyInMetadata(desiredObject, opts.Exclusions) ||
135+
(existingObject != nil && utils.AnyInMetadata(existingObject, opts.Exclusions)) {
136+
return true
137+
}
138+
139+
if existingObject != nil &&
140+
existingObject.GetUID() != "" &&
141+
utils.AnyInMetadata(desiredObject, opts.IfNotPresentSelector) {
142+
return true
143+
}
144+
145+
return false
146+
}

0 commit comments

Comments
 (0)