@@ -32,10 +32,12 @@ import (
32
32
// DiffOptions contains options for server-side dry-run apply requests.
33
33
type DiffOptions struct {
34
34
// 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.
38
36
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"`
39
41
}
40
42
41
43
// DefaultDiffOptions returns the default dry-run apply options.
@@ -57,7 +59,7 @@ func (m *ResourceManager) Diff(ctx context.Context, object *unstructured.Unstruc
57
59
existingObject .SetGroupVersionKind (object .GroupVersionKind ())
58
60
_ = m .client .Get (ctx , client .ObjectKeyFromObject (object ), existingObject )
59
61
60
- if existingObject != nil && utils . AnyInMetadata ( existingObject , opts . Exclusions ) {
62
+ if m . shouldSkipDiff ( object , existingObject , opts ) {
61
63
return m .changeSetEntry (existingObject , SkippedAction ), nil , nil , nil
62
64
}
63
65
@@ -123,3 +125,22 @@ func prepareObjectForDiff(object *unstructured.Unstructured) *unstructured.Unstr
123
125
}
124
126
return deepCopy
125
127
}
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