forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd_test.go
More file actions
385 lines (316 loc) · 12.1 KB
/
add_test.go
File metadata and controls
385 lines (316 loc) · 12.1 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package operatingsystemconfig_test
import (
"context"
"time"
machinev1alpha1 "github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
"github.com/go-logr/logr"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/gardener/gardener/pkg/client/kubernetes"
. "github.com/gardener/gardener/pkg/nodeagent/controller/operatingsystemconfig"
mockworkqueue "github.com/gardener/gardener/third_party/mock/client-go/util/workqueue"
)
var _ = Describe("Add", func() {
Describe("#SecretPredicate", func() {
var (
p predicate.Predicate
secret *corev1.Secret
)
BeforeEach(func() {
p = (&Reconciler{}).SecretPredicate()
secret = &corev1.Secret{}
})
Describe("#Create", func() {
It("should return true", func() {
Expect(p.Create(event.CreateEvent{})).To(BeTrue())
})
})
Describe("#Update", func() {
It("should return false because old object is not a secret", func() {
Expect(p.Update(event.UpdateEvent{})).To(BeFalse())
})
It("should return false because new object is not a secret", func() {
Expect(p.Update(event.UpdateEvent{ObjectOld: secret})).To(BeFalse())
})
It("should return false because OSC data does not change", func() {
Expect(p.Update(event.UpdateEvent{ObjectOld: secret, ObjectNew: secret})).To(BeFalse())
})
It("should return true because OSC data changes", func() {
oldSecret := secret.DeepCopy()
secret.Data = map[string][]byte{"osc.yaml": []byte("foo")}
Expect(p.Update(event.UpdateEvent{ObjectOld: oldSecret, ObjectNew: secret})).To(BeTrue())
})
})
Describe("#Delete", func() {
It("should return false", func() {
Expect(p.Delete(event.DeleteEvent{})).To(BeFalse())
})
})
Describe("#Generic", func() {
It("should return false", func() {
Expect(p.Generic(event.GenericEvent{})).To(BeFalse())
})
})
})
Describe("#EnqueueWithJitterDelay", func() {
var (
ctx = context.Background()
log = logr.Discard()
fakeClient client.Client
hdlr handler.EventHandler
queue *mockworkqueue.MockTypedRateLimitingInterface[reconcile.Request]
obj *corev1.Secret
req reconcile.Request
nodeName string
)
BeforeEach(func() {
fakeClient = fakeclient.NewClientBuilder().WithScheme(kubernetes.ShootScheme).Build()
nodeName = ""
})
JustBeforeEach(func() {
hdlr = (&Reconciler{
Client: fakeClient,
NodeName: nodeName,
}).EnqueueWithJitterDelay(ctx, log)
queue = mockworkqueue.NewMockTypedRateLimitingInterface[reconcile.Request](gomock.NewController(GinkgoT()))
obj = &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "osc-secret", Namespace: "namespace"}}
req = reconcile.Request{NamespacedName: types.NamespacedName{Name: obj.Name, Namespace: obj.Namespace}}
})
Context("Create events", func() {
It("should enqueue the object without delay", func() {
queue.EXPECT().Add(req)
hdlr.Create(ctx, event.CreateEvent{Object: obj}, queue)
})
})
Context("Update events", func() {
It("should not enqueue the object when the OSC did not change", func() {
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
})
It("should not enqueue the object when the OSC is the same", func() {
obj.Data = map[string][]byte{"osc.yaml": []byte(`{"apiVersion":"extensions.gardener.cloud/v1alpha1","kind":"OperatingSystemConfig"}`)}
oldObj := obj.DeepCopy()
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: oldObj}, queue)
})
Context("when the OSC changed", func() {
var oldObj *corev1.Secret
JustBeforeEach(func() {
obj.Data = map[string][]byte{"osc.yaml": []byte(`{"apiVersion":"extensions.gardener.cloud/v1alpha1","kind":"OperatingSystemConfig"}`)}
oldObj = obj.DeepCopy()
oldObj.Data = map[string][]byte{"osc.yaml": []byte(`{"apiVersion":"extensions.gardener.cloud/v1alpha1","kind":"OperatingSystemConfig","generation":1}`)}
})
When("node name is not known yet", func() {
It("should enqueue the object without delay", func() {
queue.EXPECT().AddAfter(req, time.Duration(0))
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: oldObj}, queue)
})
})
When("node name is known", func() {
BeforeEach(func() {
nodeName = "1"
})
When("node does not exist or cannot be read", func() {
It("should enqueue the object without delay", func() {
queue.EXPECT().AddAfter(req, time.Duration(0))
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: oldObj}, queue)
})
})
When("node exists", func() {
var node *corev1.Node
BeforeEach(func() {
node = &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: nodeName}}
})
JustBeforeEach(func() {
Expect(fakeClient.Create(ctx, node)).To(Succeed())
DeferCleanup(func() {
Expect(fakeClient.Delete(ctx, node)).To(Succeed())
})
})
When("node has no reconciliation delay annotation", func() {
It("should enqueue the object without delay", func() {
queue.EXPECT().AddAfter(req, time.Duration(0))
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: oldObj}, queue)
})
When("node had a reconciliation delay previously", func() {
It("should enqueue the object with the previous delay", func() {
metav1.SetMetaDataAnnotation(&node.ObjectMeta, "node-agent.gardener.cloud/reconciliation-delay", "8m")
Expect(fakeClient.Update(ctx, node)).To(Succeed())
queue.EXPECT().AddAfter(req, 8*time.Minute)
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: oldObj}, queue)
delete(node.Annotations, "node-agent.gardener.cloud/reconciliation-delay")
Expect(fakeClient.Update(ctx, node)).To(Succeed())
queue.EXPECT().AddAfter(req, 8*time.Minute)
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: oldObj}, queue)
})
})
})
When("node has reconciliation annotation but it cannot be parsed", func() {
It("should enqueue the object without delay", func() {
metav1.SetMetaDataAnnotation(&node.ObjectMeta, "node-agent.gardener.cloud/reconciliation-delay", "fjj123hi")
Expect(fakeClient.Update(ctx, node)).To(Succeed())
queue.EXPECT().AddAfter(req, time.Duration(0))
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: oldObj}, queue)
})
When("node had a reconciliation delay previously", func() {
It("should enqueue the object with the previous delay", func() {
metav1.SetMetaDataAnnotation(&node.ObjectMeta, "node-agent.gardener.cloud/reconciliation-delay", "13s")
Expect(fakeClient.Update(ctx, node)).To(Succeed())
queue.EXPECT().AddAfter(req, 13*time.Second)
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: oldObj}, queue)
metav1.SetMetaDataAnnotation(&node.ObjectMeta, "node-agent.gardener.cloud/reconciliation-delay", "fjj123hi")
Expect(fakeClient.Update(ctx, node)).To(Succeed())
queue.EXPECT().AddAfter(req, 13*time.Second)
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: oldObj}, queue)
})
})
})
When("node has reconciliation annotation and it can be parsed", func() {
BeforeEach(func() {
metav1.SetMetaDataAnnotation(&node.ObjectMeta, "node-agent.gardener.cloud/reconciliation-delay", "12h")
})
It("should enqueue the object with expected delay", func() {
queue.EXPECT().AddAfter(req, 12*time.Hour)
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: oldObj}, queue)
})
})
})
})
})
})
Context("Delete events", func() {
It("should not enqueue the object", func() {
hdlr.Delete(ctx, event.DeleteEvent{Object: obj}, queue)
})
})
Context("Generic events", func() {
It("should not enqueue the object", func() {
hdlr.Generic(ctx, event.GenericEvent{Object: obj}, queue)
})
})
})
Describe("#NodeToSecretMapper", func() {
var (
mapper handler.MapFunc
ctx context.Context
node *corev1.Node
)
BeforeEach(func() {
ctx = context.TODO()
mapper = (&Reconciler{}).NodeToSecretMapper()
node = &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node",
Labels: map[string]string{
"worker.gardener.cloud/gardener-node-agent-secret-name": "secret-1",
},
},
}
})
It("should return nil when the object is not a Node", func() {
Expect(mapper(ctx, &corev1.Secret{})).To(BeNil())
})
It("should return nil when the node does not have the secret label", func() {
delete(node.Labels, "worker.gardener.cloud/gardener-node-agent-secret-name")
Expect(mapper(ctx, node)).To(BeNil())
})
It("should map the node to the secret", func() {
Expect(mapper(ctx, node)).To(ConsistOf(
reconcile.Request{
NamespacedName: types.NamespacedName{
Name: "secret-1",
Namespace: "kube-system",
},
}),
)
})
})
Describe("#NodeReadyForUpdatePredicate", func() {
var (
p predicate.Predicate
node *corev1.Node
)
BeforeEach(func() {
p = (&Reconciler{}).NodeReadyForUpdate()
node = &corev1.Node{
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{
{
Type: "InPlaceUpdate",
Status: corev1.ConditionTrue,
Reason: machinev1alpha1.ReadyForUpdate,
},
},
},
}
})
Describe("#Create", func() {
It("should return false when object is not node", func() {
Expect(p.Create(event.CreateEvent{Object: &corev1.Secret{}})).To(BeFalse())
})
It("should return false when node status does not have inPlaceUpdate condition with reason ReadyForUpdate", func() {
Expect(p.Create(event.CreateEvent{Object: &corev1.Node{}})).To(BeFalse())
})
It("should return true when node status has inPlaceUpdate condition with reason ReadyForUpdate", func() {
Expect(p.Create(event.CreateEvent{Object: node})).To(BeTrue())
})
})
Describe("#Update", func() {
It("should return false because new object is not node", func() {
Expect(p.Update(event.UpdateEvent{
ObjectOld: &corev1.Node{},
ObjectNew: &corev1.Secret{},
})).To(BeFalse())
})
It("should return false because old object is not node", func() {
Expect(p.Update(event.UpdateEvent{
ObjectOld: &corev1.Secret{},
ObjectNew: &corev1.Node{},
})).To(BeFalse())
})
It("should return false because both new and old object have inPlaceUpdate condition with reason ReadyForUpdate", func() {
Expect(p.Update(event.UpdateEvent{
ObjectOld: node,
ObjectNew: node,
})).To(BeFalse())
})
It("should return false because new object does not have inPlaceUpdate condition with reason ReadyForUpdate", func() {
newNode := node.DeepCopy()
newNode.Status.Conditions[0].Reason = machinev1alpha1.SelectedForUpdate
Expect(p.Update(event.UpdateEvent{
ObjectOld: node,
ObjectNew: newNode,
})).To(BeFalse())
})
It("should return true because old object does not have inPlaceUpdate condition with reason ReadyForUpdate and new object has", func() {
oldNode := node.DeepCopy()
oldNode.Status.Conditions[0].Reason = machinev1alpha1.SelectedForUpdate
Expect(p.Update(event.UpdateEvent{
ObjectOld: oldNode,
ObjectNew: node,
})).To(BeTrue())
})
})
Describe("#Delete", func() {
It("should return false", func() {
Expect(p.Delete(event.DeleteEvent{})).To(BeFalse())
})
})
Describe("#Generic", func() {
It("should return false", func() {
Expect(p.Generic(event.GenericEvent{})).To(BeFalse())
})
})
})
})