-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoffboarding_controller_test.go
More file actions
525 lines (461 loc) · 18.7 KB
/
offboarding_controller_test.go
File metadata and controls
525 lines (461 loc) · 18.7 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
SPDX-FileCopyrightText: Copyright 2026 SAP SE or an SAP affiliate company and cobaltcore-dev contributors
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"fmt"
"net/http"
"os"
"github.com/gophercloud/gophercloud/v2/testhelper"
"github.com/gophercloud/gophercloud/v2/testhelper/client"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
)
var _ = Describe("Offboarding Controller", func() {
const (
EOF = "EOF"
serviceId = "service-1234"
hypervisorName = "node-test"
namespaceName = "namespace-test"
)
var (
offboardingReconciler *HypervisorOffboardingReconciler
resourceName = types.NamespacedName{Name: hypervisorName}
reconcileReq = ctrl.Request{NamespacedName: resourceName}
fakeServer testhelper.FakeServer
)
BeforeEach(func(ctx SpecContext) {
By("Setting up the OpenStack http mock server")
fakeServer = testhelper.SetupHTTP()
DeferCleanup(fakeServer.Teardown)
// Install default handler to fail unhandled requests
fakeServer.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Fail("Unhandled request to fake server: " + r.Method + " " + r.URL.Path)
})
By("Setting KVM_HA_SERVICE_URL environment variable")
os.Setenv("KVM_HA_SERVICE_URL", fakeServer.Endpoint()+"instance-ha")
DeferCleanup(func() {
os.Unsetenv("KVM_HA_SERVICE_URL")
})
By("Creating the HypervisorOffboardingReconciler")
offboardingReconciler = &HypervisorOffboardingReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
computeClient: client.ServiceClient(fakeServer),
placementClient: client.ServiceClient(fakeServer),
}
By("Creating the namespace for the reconciler")
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespaceName}}
Expect(k8sclient.IgnoreAlreadyExists(k8sClient.Create(ctx, ns))).To(Succeed())
DeferCleanup(func(ctx SpecContext) {
Expect(k8sClient.Delete(ctx, ns)).To(Succeed())
})
By("Creating the hypervisor resource with lifecycle enabled")
hypervisor := &kvmv1.Hypervisor{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName.Name,
},
Spec: kvmv1.HypervisorSpec{
LifecycleEnabled: true,
},
}
Expect(k8sClient.Create(ctx, hypervisor)).To(Succeed())
DeferCleanup(func(ctx SpecContext) {
Expect(k8sClient.Delete(ctx, hypervisor)).To(Succeed())
})
})
Context("Happy Path", func() {
Context("When marking the hypervisor terminating", func() {
JustBeforeEach(func(ctx SpecContext) {
By("Reconciling first to add the finalizer")
_, err := offboardingReconciler.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())
By("Marking the hypervisor for termination")
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, resourceName, hypervisor)).To(Succeed())
hypervisor.Spec.Maintenance = kvmv1.MaintenanceTermination
Expect(k8sClient.Update(ctx, hypervisor)).To(Succeed())
By("Setting terminating condition")
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: kvmv1.ConditionTypeTerminating,
Status: metav1.ConditionTrue,
Reason: "dontcare",
Message: "dontcare",
})
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
})
Context("when the hypervisor was set to ready and has been evicted", func() {
var getHypervisorsCalled int
BeforeEach(func(ctx SpecContext) {
getHypervisorsCalled = 0
By("Setting hypervisor to ready and evicted")
hv := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, resourceName, hv)).To(Succeed())
meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{
Type: kvmv1.ConditionTypeReady,
Status: metav1.ConditionTrue,
Reason: "dontcare",
Message: "dontcare",
})
meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{
Type: kvmv1.ConditionTypeEvicting,
Status: metav1.ConditionFalse,
Reason: "dontcare",
Message: "dontcare",
})
Expect(k8sClient.Status().Update(ctx, hv)).To(Succeed())
By("Mocking OpenStack API endpoints")
fakeServer.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
getHypervisorsCalled++
Expect(fmt.Fprintf(w, HypervisorWithServers, serviceId, "some reason", hypervisorName)).ToNot(BeNil())
})
// c48f6247-abe4-4a24-824e-ea39e108874f comes from the HypervisorWithServers const
fakeServer.Mux.HandleFunc("GET /resource_providers/c48f6247-abe4-4a24-824e-ea39e108874f", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, `{"uuid": "rp-uuid", "name": "hv-test"}`)
Expect(err).NotTo(HaveOccurred())
})
fakeServer.Mux.HandleFunc("GET /resource_providers/rp-uuid/allocations", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, `{"allocations": {}}}`)
Expect(err).NotTo(HaveOccurred())
})
fakeServer.Mux.HandleFunc("DELETE /resource_providers/rp-uuid", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusAccepted)
})
fakeServer.Mux.HandleFunc("DELETE /os-services/service-1234", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
})
})
It("should set the hypervisor offboarding condition", func(ctx SpecContext) {
_, err := offboardingReconciler.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, resourceName, hypervisor)).To(Succeed())
Expect(hypervisor.Status.Conditions).To(ContainElement(
SatisfyAll(
HaveField("Type", kvmv1.ConditionTypeOffboarded),
HaveField("Status", metav1.ConditionFalse),
HaveField("Reason", "Offboarding"),
),
))
})
It("should set the hypervisor offboarded condition", func(ctx SpecContext) {
for range 3 {
_, err := offboardingReconciler.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())
}
Expect(getHypervisorsCalled).To(BeNumerically(">", 0))
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, resourceName, hypervisor)).To(Succeed())
Expect(hypervisor.Status.Conditions).To(ContainElement(
SatisfyAll(
HaveField("Type", kvmv1.ConditionTypeOffboarded),
HaveField("Status", metav1.ConditionTrue),
HaveField("Reason", "Offboarded"),
HaveField("Message", "Offboarding successful"),
),
))
})
It("should clear Status.Aggregates when removing from all aggregates", func(ctx SpecContext) {
By("Setting initial aggregates and IDs in status")
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, resourceName, hypervisor)).To(Succeed())
hypervisor.Status.Aggregates = []kvmv1.Aggregate{
{Name: "zone-a", UUID: "uuid-zone-a"},
{Name: "test-aggregate", UUID: "uuid-test"},
}
hypervisor.Status.ServiceID = serviceId
hypervisor.Status.HypervisorID = "c48f6247-abe4-4a24-824e-ea39e108874f"
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
By("Reconciling - offboarding controller will wait for aggregates to be cleared")
_, err := offboardingReconciler.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())
By("Simulating aggregates controller clearing aggregates")
Expect(k8sClient.Get(ctx, resourceName, hypervisor)).To(Succeed())
hypervisor.Status.Aggregates = []kvmv1.Aggregate{}
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
By("Reconciling again after aggregates are cleared")
for range 3 {
_, err := offboardingReconciler.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())
}
By("Verifying Status.Aggregates is empty")
Expect(k8sClient.Get(ctx, resourceName, hypervisor)).To(Succeed())
Expect(hypervisor.Status.Aggregates).To(BeEmpty(), "Status.Aggregates should be cleared after offboarding")
})
})
})
})
Context("Guard Conditions", func() {
JustBeforeEach(func(ctx SpecContext) {
result, err := offboardingReconciler.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(ctrl.Result{}))
})
Context("When lifecycle is not enabled, but maintenance is set to termination", func() {
BeforeEach(func(ctx SpecContext) {
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, resourceName, hypervisor)).To(Succeed())
hypervisor.Spec.Maintenance = kvmv1.MaintenanceTermination
hypervisor.Spec.LifecycleEnabled = false
Expect(k8sClient.Update(ctx, hypervisor)).To(Succeed())
})
It("should not reconcile", func(ctx SpecContext) {
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, resourceName, hypervisor)).To(Succeed())
Expect(hypervisor.Status.Conditions).To(BeEmpty())
})
})
Context("When maintenance is not set to termination", func() {
BeforeEach(func(ctx SpecContext) {
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, resourceName, hypervisor)).To(Succeed())
hypervisor.Spec.Maintenance = kvmv1.MaintenanceManual
hypervisor.Spec.MaintenanceReason = "Test maintenance reason"
Expect(k8sClient.Update(ctx, hypervisor)).To(Succeed())
})
It("should not reconcile", func(ctx SpecContext) {
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, resourceName, hypervisor)).To(Succeed())
Expect(hypervisor.Status.Conditions).To(BeEmpty())
})
})
})
Context("Failure Modes", func() {
var sharedOffboardingErrorCheck = func(ctx SpecContext, expectedMessageSubstring string) {
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, resourceName, hypervisor)).To(Succeed())
Expect(hypervisor.Status.Conditions).To(ContainElement(
SatisfyAll(
HaveField("Type", kvmv1.ConditionTypeOffboarded),
HaveField("Status", metav1.ConditionFalse),
HaveField("Reason", "Offboarding"),
HaveField("Message", ContainSubstring(expectedMessageSubstring)),
),
))
}
BeforeEach(func(ctx SpecContext) {
By("Setting hypervisor to maintenance termination mode")
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, resourceName, hypervisor)).To(Succeed())
hypervisor.Spec.Maintenance = kvmv1.MaintenanceTermination
Expect(k8sClient.Update(ctx, hypervisor)).To(Succeed())
By("Setting initial offboarding condition")
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: kvmv1.ConditionTypeOffboarded,
Status: metav1.ConditionFalse,
Reason: "Offboarding",
Message: "Hypervisor is being offboarded, removing host from nova",
})
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
})
Context("When getting hypervisor by name fails", func() {
BeforeEach(func() {
fakeServer.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, `{"error": "Internal Server Error"}`)
})
})
It("should set offboarding condition with error", func(ctx SpecContext) {
_, err := offboardingReconciler.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())
sharedOffboardingErrorCheck(ctx, "")
})
})
Context("When hypervisor still has running VMs", func() {
BeforeEach(func() {
hvResponse := `{
"hypervisors": [{
"id": "c48f6247-abe4-4a24-824e-ea39e108874f",
"hypervisor_hostname": "node-test",
"hypervisor_version": 2002000,
"state": "up",
"status": "enabled",
"running_vms": 2,
"service": {
"id": "service-1234",
"host": "node-test",
"disabled_reason": ""
}
}]
}`
fakeServer.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, hvResponse)
})
})
It("should set offboarding condition about running VMs", func(ctx SpecContext) {
_, err := offboardingReconciler.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())
sharedOffboardingErrorCheck(ctx, "still has 2 running VMs")
})
})
Context("When aggregates are not empty", func() {
BeforeEach(func(ctx SpecContext) {
fakeServer.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{
"hypervisors": [{
"id": "c48f6247-abe4-4a24-824e-ea39e108874f",
"hypervisor_hostname": "node-test",
"hypervisor_version": 2002000,
"state": "up",
"status": "enabled",
"running_vms": 0,
"service": {
"id": "service-1234",
"host": "node-test"
}
}]
}`)
})
// Simulate aggregates controller hasn't cleared aggregates yet
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, resourceName, hypervisor)).To(Succeed())
hypervisor.Status.Aggregates = []kvmv1.Aggregate{
{Name: "zone-a", UUID: "uuid-zone-a"},
{Name: "test-aggregate", UUID: "uuid-test"},
}
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
})
It("should wait for aggregates to be cleared", func(ctx SpecContext) {
_, err := offboardingReconciler.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())
sharedOffboardingErrorCheck(ctx, "Waiting for aggregates to be removed")
})
})
Context("When deleting service fails", func() {
BeforeEach(func() {
fakeServer.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{
"hypervisors": [{
"id": "c48f6247-abe4-4a24-824e-ea39e108874f",
"hypervisor_hostname": "node-test",
"hypervisor_version": 2002000,
"state": "up",
"status": "enabled",
"running_vms": 0,
"service": {
"id": "service-1234",
"host": "node-test"
}
}]
}`)
})
fakeServer.Mux.HandleFunc("DELETE /os-services/service-1234", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, `{"error": "Internal Server Error"}`)
})
})
It("should set offboarding condition with error", func(ctx SpecContext) {
_, err := offboardingReconciler.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())
sharedOffboardingErrorCheck(ctx, "cannot delete service")
})
})
Context("When getting resource provider fails", func() {
BeforeEach(func() {
fakeServer.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{
"hypervisors": [{
"id": "c48f6247-abe4-4a24-824e-ea39e108874f",
"hypervisor_hostname": "node-test",
"hypervisor_version": 2002000,
"state": "up",
"status": "enabled",
"running_vms": 0,
"service": {
"id": "service-1234",
"host": "node-test"
}
}]
}`)
})
fakeServer.Mux.HandleFunc("DELETE /os-services/service-1234", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
})
fakeServer.Mux.HandleFunc("GET /resource_providers/c48f6247-abe4-4a24-824e-ea39e108874f", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, `{"error": "Internal Server Error"}`)
})
})
It("should set offboarding condition with error", func(ctx SpecContext) {
_, err := offboardingReconciler.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())
sharedOffboardingErrorCheck(ctx, "cannot get resource provider")
})
})
Context("When cleaning up resource provider fails", func() {
BeforeEach(func() {
fakeServer.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{
"hypervisors": [{
"id": "c48f6247-abe4-4a24-824e-ea39e108874f",
"hypervisor_hostname": "node-test",
"hypervisor_version": 2002000,
"state": "up",
"status": "enabled",
"running_vms": 0,
"service": {
"id": "service-1234",
"host": "node-test"
}
}]
}`)
})
fakeServer.Mux.HandleFunc("DELETE /os-services/service-1234", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
})
fakeServer.Mux.HandleFunc("GET /resource_providers/c48f6247-abe4-4a24-824e-ea39e108874f", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"uuid": "rp-uuid", "name": "hv-test"}`)
})
fakeServer.Mux.HandleFunc("GET /resource_providers/rp-uuid/allocations", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"allocations": {}}`)
})
fakeServer.Mux.HandleFunc("DELETE /resource_providers/rp-uuid", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, `{"error": "Internal Server Error"}`)
})
})
It("should set offboarding condition with error", func(ctx SpecContext) {
_, err := offboardingReconciler.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())
sharedOffboardingErrorCheck(ctx, "cannot clean up resource provider")
})
})
})
})