@@ -344,3 +344,39 @@ func patchFinalizers(ctx context.Context, cl client.Client, cr client.Object) er
344
344
}
345
345
return cl .Patch (ctx , cr , client .RawPatch (types .MergePatchType , patch ))
346
346
}
347
+
348
+ func addAnnotation (ctx context.Context , cl client.Client , cr client.Object , key string , value string ) error {
349
+ crAnnotations := cr .GetAnnotations ()
350
+
351
+ if crAnnotations [key ] == value {
352
+ return nil
353
+ }
354
+
355
+ // Add key to map and create patch
356
+ crAnnotations [key ] = value
357
+ patch , err := json .Marshal (map [string ]interface {}{"metadata" : map [string ]interface {}{"annotations" : crAnnotations }})
358
+ if err != nil {
359
+ return err
360
+ }
361
+
362
+ return cl .Patch (ctx , cr , client .RawPatch (types .MergePatchType , patch ))
363
+ }
364
+
365
+ func removeAnnotation (ctx context.Context , cl client.Client , cr client.Object , key string ) error {
366
+ crAnnotations := cr .GetAnnotations ()
367
+ if crAnnotations [key ] == "" {
368
+ return nil
369
+ }
370
+
371
+ // Escape slash '/' according to RFC6901
372
+ // We could also escape tilde '~', but that is not a valid character in annotation keys.
373
+ key = strings .ReplaceAll (key , "/" , "~1" )
374
+ patch , err := json .Marshal ([]interface {}{map [string ]interface {}{"op" : "remove" , "path" : "/metadata/annotations/" + key }})
375
+ if err != nil {
376
+ return err
377
+ }
378
+
379
+ // MergePatchType only removes map keys when the value is null. JSONPatchType allows removing anything under a path.
380
+ // Differs from removeFinalizer where we overwrite an array.
381
+ return cl .Patch (ctx , cr , client .RawPatch (types .JSONPatchType , patch ))
382
+ }
0 commit comments