-
Notifications
You must be signed in to change notification settings - Fork 231
/
kube.go
62 lines (51 loc) · 1.54 KB
/
kube.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
53
54
55
56
57
58
59
60
61
62
package statusmanager
import (
"context"
"encoding/json"
"fmt"
"github.com/openshift/cluster-network-operator/pkg/apply"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
)
// Some silly types so that we can actually do testing
type DaemonSetLister interface {
List(selector labels.Selector) (ret []*appsv1.DaemonSet, err error)
}
type DeploymentLister interface {
List(selector labels.Selector) (ret []*appsv1.Deployment, err error)
}
type StatefulSetLister interface {
List(selector labels.Selector) (ret []*appsv1.StatefulSet, err error)
}
// just the scaffolding needed to be able to send patched annotations
// including a nil, which deletes an annotation
type patchAnnotations struct {
Metadata md `json:"metadata"`
}
type md struct {
Annotations map[string]interface{} `json:"annotations"`
}
func (status *StatusManager) setAnnotation(ctx context.Context, obj crclient.Object, key string, value *string) error {
anno := obj.GetAnnotations()
existing, set := anno[key]
if value != nil && set && existing == *value {
return nil
}
if !set && value == nil {
return nil
}
patch := &patchAnnotations{
Metadata: md{
Annotations: map[string]interface{}{
key: value,
},
},
}
patchData, err := json.Marshal(&patch)
if err != nil {
return fmt.Errorf("failed to create patch: %v", err)
}
return status.client.ClientFor(apply.GetClusterName(obj)).CRClient().Patch(ctx, obj, crclient.RawPatch(types.MergePatchType, patchData))
}