forked from openshift/machine-config-operator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
meta.go
73 lines (66 loc) · 1.91 KB
/
meta.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
63
64
65
66
67
68
69
70
71
72
73
package resourcemerge
import (
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// EnsureObjectMeta ensures that the existing matches the required.
// modified is set to true when existing had to be updated with required.
func EnsureObjectMeta(modified *bool, existing *metav1.ObjectMeta, required metav1.ObjectMeta) {
setStringIfSet(modified, &existing.Namespace, required.Namespace)
setStringIfSet(modified, &existing.Name, required.Name)
mergeMap(modified, &existing.Labels, required.Labels)
mergeMap(modified, &existing.Annotations, required.Annotations)
mergeOwnerRefs(modified, &existing.OwnerReferences, required.OwnerReferences)
}
func setStringIfSet(modified *bool, existing *string, required string) {
if len(required) == 0 {
return
}
if required != *existing {
*existing = required
*modified = true
}
}
func setBytesIfSet(modified *bool, existing *[]byte, required []byte) {
if len(required) == 0 {
return
}
if string(required) != string(*existing) {
*existing = required
*modified = true
}
}
func mergeMap(modified *bool, existing *map[string]string, required map[string]string) {
if *existing == nil {
*existing = map[string]string{}
}
for k, v := range required {
if existingV, ok := (*existing)[k]; !ok || v != existingV {
*modified = true
(*existing)[k] = v
}
}
}
func mergeOwnerRefs(modified *bool, existing *[]metav1.OwnerReference, required []metav1.OwnerReference) {
for ridx := range required {
found := false
for eidx := range *existing {
if required[ridx].UID == (*existing)[eidx].UID {
found = true
if !equality.Semantic.DeepEqual((*existing)[eidx], required[ridx]) {
*modified = true
(*existing)[eidx] = required[ridx]
}
break
}
}
if !found {
*modified = true
*existing = append(*existing, required[ridx])
}
}
}
// BoolPtr returns ptr to bool.
func BoolPtr(val bool) *bool {
return &val
}