/
annotation.go
52 lines (42 loc) · 1.26 KB
/
annotation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package k8s
import (
"context"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// AnnotateObject adds the specified annotations to the object
func AnnotateObject(ctx context.Context, c client.Client, obj client.Object, annotations map[string]string) error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
_obj := obj.DeepCopyObject().(client.Object)
err := c.Get(ctx, client.ObjectKeyFromObject(obj), _obj)
if err != nil {
return err
}
a := _obj.GetAnnotations()
if a == nil {
a = make(map[string]string)
}
for k, v := range annotations {
a[k] = v
}
_obj.SetAnnotations(a)
return c.Patch(ctx, _obj, client.MergeFrom(obj))
})
}
// DeannotateObject removes the specified annotation from the object
func DeannotateObject(ctx context.Context, c client.Client, obj client.Object, annotation string) error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
_obj := obj.DeepCopyObject().(client.Object)
err := c.Get(ctx, client.ObjectKeyFromObject(obj), _obj)
if err != nil {
return err
}
a := _obj.GetAnnotations()
if a == nil {
a = make(map[string]string)
}
delete(a, annotation)
_obj.SetAnnotations(a)
return c.Patch(ctx, _obj, client.MergeFrom(obj))
})
}