@@ -27,6 +27,7 @@ import (
2727 "k8s.io/apimachinery/pkg/runtime"
2828 "k8s.io/apimachinery/pkg/runtime/schema"
2929 kerrors "k8s.io/apimachinery/pkg/util/errors"
30+ "k8s.io/apimachinery/pkg/util/sets"
3031 "k8s.io/apimachinery/pkg/util/wait"
3132 "k8s.io/klog/v2"
3233 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -44,7 +45,7 @@ type Helper struct {
4445 beforeObject client.Object
4546 before * unstructured.Unstructured
4647 after * unstructured.Unstructured
47- changes map [string ]bool
48+ changes sets. Set [string ]
4849
4950 isConditionsSetter bool
5051}
@@ -157,7 +158,7 @@ func (h *Helper) Patch(ctx context.Context, obj client.Object, opts ...Option) e
157158
158159// patch issues a patch for metadata and spec.
159160func (h * Helper ) patch (ctx context.Context , obj client.Object ) error {
160- if ! h .shouldPatch ("metadata" ) && ! h . shouldPatch ( "spec" ) {
161+ if ! h .shouldPatch (specPatch ) {
161162 return nil
162163 }
163164 beforeObject , afterObject , err := h .calculatePatch (obj , specPatch )
@@ -169,7 +170,7 @@ func (h *Helper) patch(ctx context.Context, obj client.Object) error {
169170
170171// patchStatus issues a patch if the status has changed.
171172func (h * Helper ) patchStatus (ctx context.Context , obj client.Object ) error {
172- if ! h .shouldPatch ("status" ) {
173+ if ! h .shouldPatch (statusPatch ) {
173174 return nil
174175 }
175176 beforeObject , afterObject , err := h .calculatePatch (obj , statusPatch )
@@ -285,13 +286,18 @@ func (h *Helper) calculatePatch(afterObj client.Object, focus patchType) (client
285286 return beforeObj , afterObj , nil
286287}
287288
288- func (h * Helper ) shouldPatch (in string ) bool {
289- return h .changes [in ]
289+ func (h * Helper ) shouldPatch (focus patchType ) bool {
290+ if focus == specPatch {
291+ // If we're looking to patch anything other than status,
292+ // return true if the changes map has any fields after removing `status`.
293+ return h .changes .Clone ().Delete ("status" ).Len () > 0
294+ }
295+ return h .changes .Has (string (focus ))
290296}
291297
292298// calculate changes tries to build a patch from the before/after objects we have
293299// and store in a map which top-level fields (e.g. `metadata`, `spec`, `status`, etc.) have changed.
294- func (h * Helper ) calculateChanges (after client.Object ) (map [string ]bool , error ) {
300+ func (h * Helper ) calculateChanges (after client.Object ) (sets. Set [string ], error ) {
295301 // Calculate patch data.
296302 patch := client .MergeFrom (h .beforeObject )
297303 diff , err := patch .Data (after )
@@ -306,9 +312,9 @@ func (h *Helper) calculateChanges(after client.Object) (map[string]bool, error)
306312 }
307313
308314 // Return the map.
309- res := make ( map [string ]bool , len ( patchDiff ) )
315+ res := sets . New [string ]( )
310316 for key := range patchDiff {
311- res [ key ] = true
317+ res . Insert ( key )
312318 }
313319 return res , nil
314320}
0 commit comments