forked from openshift/library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_merger.go
153 lines (130 loc) · 3.42 KB
/
object_merger.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package resourcemerge
import (
"reflect"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// EnsureObjectMeta writes namespace, name, labels, and annotations. Don't set other things here.
// TODO finalizer support maybe?
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)
}
func stringPtr(val string) *string {
return &val
}
func SetString(modified *bool, existing *string, required string) {
if required != *existing {
*existing = required
*modified = true
}
}
func SetStringIfSet(modified *bool, existing *string, required string) {
if len(required) == 0 {
return
}
if required != *existing {
*existing = required
*modified = true
}
}
func setStringPtr(modified *bool, existing **string, required *string) {
if *existing == nil || (required == nil && *existing != nil) {
*modified = true
*existing = required
return
}
SetString(modified, *existing, *required)
}
func SetStringSlice(modified *bool, existing *[]string, required []string) {
if !reflect.DeepEqual(required, *existing) {
*existing = required
*modified = true
}
}
func SetStringSliceIfSet(modified *bool, existing *[]string, required []string) {
if required == nil {
return
}
if !reflect.DeepEqual(required, *existing) {
*existing = required
*modified = true
}
}
func BoolPtr(val bool) *bool {
return &val
}
func SetBool(modified *bool, existing *bool, required bool) {
if required != *existing {
*existing = required
*modified = true
}
}
func setBoolPtr(modified *bool, existing **bool, required *bool) {
if *existing == nil || (required == nil && *existing != nil) {
*modified = true
*existing = required
return
}
SetBool(modified, *existing, *required)
}
func int64Ptr(val int64) *int64 {
return &val
}
func SetInt32(modified *bool, existing *int32, required int32) {
if required != *existing {
*existing = required
*modified = true
}
}
func SetInt32IfSet(modified *bool, existing *int32, required int32) {
if required == 0 {
return
}
SetInt32(modified, existing, required)
}
func SetInt64(modified *bool, existing *int64, required int64) {
if required != *existing {
*existing = required
*modified = true
}
}
func setInt64Ptr(modified *bool, existing **int64, required *int64) {
if *existing == nil || (required == nil && *existing != nil) {
*modified = true
*existing = required
return
}
SetInt64(modified, *existing, *required)
}
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 SetMapStringString(modified *bool, existing *map[string]string, required map[string]string) {
if *existing == nil {
*existing = map[string]string{}
}
if !reflect.DeepEqual(*existing, required) {
*existing = required
}
}
func SetMapStringStringIfSet(modified *bool, existing *map[string]string, required map[string]string) {
if required == nil {
return
}
if *existing == nil {
*existing = map[string]string{}
}
if !reflect.DeepEqual(*existing, required) {
*existing = required
}
}