-
Notifications
You must be signed in to change notification settings - Fork 6
/
client_util.go
289 lines (219 loc) · 7.93 KB
/
client_util.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package updater
// updater uploads client updates
import (
"fmt"
appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
"github.com/l7mp/stunner-gateway-operator/internal/store"
)
func (u *Updater) updateGatewayClass(gc *gwapiv1a2.GatewayClass, gen int) error {
u.log.V(2).Info("update gateway class", "resource", store.GetObjectKey(gc), "generation",
gen)
cli := u.manager.GetClient()
current := &gwapiv1a2.GatewayClass{ObjectMeta: metav1.ObjectMeta{
Name: gc.GetName(),
Namespace: gc.GetNamespace(),
}}
if err := cli.Get(u.ctx, client.ObjectKeyFromObject(current), current); err != nil {
return err
}
// the only thing we change on gatewayclasses is the status: copy
gc.Status.DeepCopyInto(¤t.Status)
if err := cli.Status().Update(u.ctx, current); err != nil {
return err
}
u.log.V(1).Info("gateway-class updated", "resource", store.GetObjectKey(gc), "generation",
gen, "result", store.DumpObject(current))
return nil
}
func (u *Updater) updateGateway(gw *gwapiv1a2.Gateway, gen int) error {
u.log.V(2).Info("updating gateway", "resource", store.GetObjectKey(gw), "generation",
gen)
cli := u.manager.GetClient()
current := &gwapiv1a2.Gateway{ObjectMeta: metav1.ObjectMeta{
Name: gw.GetName(),
Namespace: gw.GetNamespace(),
}}
if err := cli.Get(u.ctx, client.ObjectKeyFromObject(current), current); err != nil {
return err
}
gw.Status.DeepCopyInto(¤t.Status)
if err := cli.Status().Update(u.ctx, current); err != nil {
return err
}
u.log.V(1).Info("gateway updated", "resource", store.GetObjectKey(gw), "generation", gen,
"result", store.DumpObject(current))
return nil
}
func (u *Updater) updateUDPRoute(ro *gwapiv1a2.UDPRoute, gen int) error {
u.log.V(2).Info("updating UDP-route", "resource", store.GetObjectKey(ro), "generation",
gen)
cli := u.manager.GetClient()
current := &gwapiv1a2.UDPRoute{ObjectMeta: metav1.ObjectMeta{
Name: ro.GetName(),
Namespace: ro.GetNamespace(),
}}
if err := cli.Get(u.ctx, client.ObjectKeyFromObject(current), current); err != nil {
return err
}
ro.Status.DeepCopyInto(¤t.Status)
if err := cli.Status().Update(u.ctx, current); err != nil {
return err
}
u.log.V(1).Info("UDP-route updated", "resource", store.GetObjectKey(ro), "generation",
gen, "result", store.DumpObject(current))
return nil
}
func (u *Updater) upsertService(svc *corev1.Service, gen int) (ctrlutil.OperationResult, error) {
u.log.V(2).Info("upsert service", "resource", store.GetObjectKey(svc), "generation", gen)
client := u.manager.GetClient()
current := &corev1.Service{ObjectMeta: metav1.ObjectMeta{
Name: svc.GetName(),
Namespace: svc.GetNamespace(),
}}
op, err := ctrlutil.CreateOrUpdate(u.ctx, client, current, func() error {
if err := mergeMetadata(current, svc); err != nil {
return nil
}
// rewrite spec
svc.Spec.DeepCopyInto(¤t.Spec)
return nil
})
if err != nil {
return ctrlutil.OperationResultNone, fmt.Errorf("cannot upsert service %q: %w",
store.GetObjectKey(svc), err)
}
u.log.V(1).Info("service upserted", "resource", store.GetObjectKey(svc), "generation",
gen, "result", store.DumpObject(current))
return op, nil
}
func (u *Updater) upsertConfigMap(cm *corev1.ConfigMap, gen int) (ctrlutil.OperationResult, error) {
u.log.V(2).Info("upsert config-map", "resource", store.GetObjectKey(cm), "generation",
gen)
client := u.manager.GetClient()
current := &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{
Name: cm.GetName(),
Namespace: cm.GetNamespace(),
}}
op, err := ctrlutil.CreateOrUpdate(u.ctx, client, current, func() error {
// thing that might have been changed by the controler: the owner ref, annotations
// and the data
// u.log.Info("before", "cm", fmt.Sprintf("%#v\n", current))
if err := mergeMetadata(current, cm); err != nil {
return nil
}
current.Data = make(map[string]string)
for k, v := range cm.Data {
current.Data[k] = v
}
// u.log.Info("after", "cm", fmt.Sprintf("%#v\n", current))
return nil
})
if err != nil {
return ctrlutil.OperationResultNone, fmt.Errorf("cannot upsert config-map %q: %w",
store.GetObjectKey(cm), err)
}
u.log.V(1).Info("config-map upserted", "resource", store.GetObjectKey(cm), "generation",
gen, "result", store.DumpObject(current))
return op, nil
}
func (u *Updater) upsertDeployment(dp *appv1.Deployment, gen int) (ctrlutil.OperationResult, error) {
u.log.V(2).Info("upsert deployment", "resource", store.GetObjectKey(dp), "generation", gen)
client := u.manager.GetClient()
current := &appv1.Deployment{ObjectMeta: metav1.ObjectMeta{
Name: dp.GetName(),
Namespace: dp.GetNamespace(),
}}
// use CreateOrPatch: hopefully more clever than CreateOrUpdate
op, err := ctrlutil.CreateOrPatch(u.ctx, client, current, func() error {
if err := mergeMetadata(current, dp); err != nil {
return nil
}
current.Spec.Selector = dp.Spec.Selector
if dp.Spec.Replicas != nil {
current.Spec.Replicas = dp.Spec.Replicas
}
// the pod template is copied verbatim
dp.Spec.Template.ObjectMeta.DeepCopyInto(¤t.Spec.Template.ObjectMeta)
dpspec := &dp.Spec.Template.Spec
currentspec := ¤t.Spec.Template.Spec
currentspec.Containers = make([]corev1.Container, len(dpspec.Containers))
for i := range dpspec.Containers {
dpspec.Containers[i].DeepCopyInto(¤tspec.Containers[i])
}
currentspec.Volumes = make([]corev1.Volume, len(dpspec.Volumes))
for i := range dpspec.Volumes {
dpspec.Volumes[i].DeepCopyInto(¤tspec.Volumes[i])
}
// rest is optional
if dpspec.TerminationGracePeriodSeconds != nil {
currentspec.TerminationGracePeriodSeconds = dpspec.TerminationGracePeriodSeconds
}
currentspec.HostNetwork = dpspec.HostNetwork
// affinity
if dpspec.Affinity != nil {
currentspec.Affinity = dpspec.Affinity
}
// tolerations
if dpspec.Tolerations != nil {
currentspec.Tolerations = dpspec.Tolerations
}
// security context
if dpspec.SecurityContext != nil {
currentspec.SecurityContext = dpspec.SecurityContext
}
// u.log.Info("after", "cm", fmt.Sprintf("%#v\n", current))
return nil
})
// u.log.V(4).Info("upserting deployment", "resource", store.GetObjectKey(dp), "generation",
// gen, "deployment", store.DumpObject(dp))
if err != nil {
return ctrlutil.OperationResultNone, fmt.Errorf("cannot upsert deployment %q: %w",
store.GetObjectKey(dp), err)
}
u.log.V(1).Info("deployment upserted", "resource", store.GetObjectKey(dp), "generation",
gen, "result", store.DumpObject(current))
return op, nil
}
func (u *Updater) deleteObject(o client.Object, gen int) error {
u.log.V(1).Info("delete objec", "resource", store.GetObjectKey(o), "generation", gen)
return u.manager.GetClient().Delete(u.ctx, o)
}
func mergeMetadata(dst, src client.Object) error {
labs := labels.Merge(dst.GetLabels(), src.GetLabels())
dst.SetLabels(labs)
annotations := dst.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}
for k, v := range src.GetAnnotations() {
annotations[k] = v
}
dst.SetAnnotations(annotations)
return addOwnerRef(dst, src)
}
func addOwnerRef(dst, src client.Object) error {
ownerRefs := src.GetOwnerReferences()
if len(ownerRefs) != 1 {
return fmt.Errorf("addOwnerRef: expecting a singleton ownerRef in %q, found %d",
store.GetObjectKey(src), len(ownerRefs))
}
ownerRef := src.GetOwnerReferences()[0]
for i, ref := range dst.GetOwnerReferences() {
if ref.Name == ownerRef.Name && ref.Kind == ownerRef.Kind {
ownerRefs = dst.GetOwnerReferences()
ownerRef.DeepCopyInto(&ownerRefs[i])
dst.SetOwnerReferences(ownerRefs)
return nil
}
}
ownerRefs = dst.GetOwnerReferences()
ownerRefs = append(ownerRefs, ownerRef)
dst.SetOwnerReferences(ownerRefs)
return nil
}