Skip to content

Commit ba95ff3

Browse files
committed
Onboarding: Use unique log message for localising text
The log message was the same as in another delete case, and that makes it hard to identify where it gets deleted.
1 parent e12357a commit ba95ff3

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

internal/controller/onboarding_controller.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const (
5454
defaultWaitTime = 1 * time.Minute
5555
ConditionTypeOnboarding = "Onboarding"
5656
ConditionReasonAborted = "aborted"
57+
ConditionReasonError = "error"
5758
ConditionReasonInitial = "initial"
5859
ConditionReasonOnboarding = "onboarding"
5960
ConditionReasonTesting = "testing"
@@ -92,12 +93,12 @@ func (r *OnboardingController) Reconcile(ctx context.Context, req ctrl.Request)
9293

9394
// check if lifecycle management is enabled
9495
if !hv.Spec.LifecycleEnabled {
95-
return r.abortOnboarding(ctx, hv, computeHost)
96+
return ctrl.Result{}, r.abortOnboarding(ctx, hv, computeHost)
9697
}
9798

9899
// check if hv is terminating
99100
if meta.IsStatusConditionTrue(hv.Status.Conditions, kvmv1.ConditionTypeTerminating) {
100-
return r.abortOnboarding(ctx, hv, computeHost)
101+
return ctrl.Result{}, r.abortOnboarding(ctx, hv, computeHost)
101102
}
102103

103104
// We bail here out, because the openstack api is not the best to poll
@@ -146,32 +147,44 @@ func (r *OnboardingController) Reconcile(ctx context.Context, req ctrl.Request)
146147
}
147148
}
148149

149-
func (r *OnboardingController) abortOnboarding(ctx context.Context, hv *kvmv1.Hypervisor, computeHost string) (ctrl.Result, error) {
150+
func (r *OnboardingController) abortOnboarding(ctx context.Context, hv *kvmv1.Hypervisor, computeHost string) error {
150151
status := meta.FindStatusCondition(hv.Status.Conditions, ConditionTypeOnboarding)
151152
// Never onboarded
152153
if status == nil {
153-
return ctrl.Result{}, nil
154+
return nil
154155
}
155156

156157
changed := meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{
157158
Type: kvmv1.ConditionTypeReady,
158159
Status: metav1.ConditionFalse,
159160
Reason: ConditionReasonOnboarding,
160161
Message: "Onboarding aborted",
161-
}) || meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{
162+
})
163+
164+
if meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{
162165
Type: ConditionTypeOnboarding,
163-
Status: metav1.ConditionTrue,
166+
Status: metav1.ConditionFalse,
164167
Reason: ConditionReasonAborted,
165-
Message: "Aborted due to LivecycleEnabled being false",
166-
})
168+
Message: "Aborted due to LifecycleEnabled being false",
169+
}) {
170+
changed = true
171+
}
172+
167173
if !changed {
168174
// Already aborted
169-
return ctrl.Result{}, nil
175+
return nil
170176
}
171177
if err := r.deleteTestServers(ctx, computeHost); err != nil {
172-
return ctrl.Result{}, err
178+
meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{
179+
Type: ConditionTypeOnboarding,
180+
Status: metav1.ConditionTrue, // No cleanup, so we are still "onboarding"
181+
Reason: ConditionReasonAborted,
182+
Message: err.Error(),
183+
})
184+
185+
return errors.Join(err, r.Status().Update(ctx, hv))
173186
}
174-
return ctrl.Result{}, r.Status().Update(ctx, hv)
187+
return r.Status().Update(ctx, hv)
175188
}
176189

177190
func (r *OnboardingController) initialOnboarding(ctx context.Context, hv *kvmv1.Hypervisor, host string) error {
@@ -293,7 +306,16 @@ func (r *OnboardingController) completeOnboarding(ctx context.Context, host stri
293306

294307
err := r.deleteTestServers(ctx, host)
295308
if err != nil {
296-
return ctrl.Result{}, fmt.Errorf("failed to delete test servers due to %w", err)
309+
if meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{
310+
Type: ConditionTypeOnboarding,
311+
Status: metav1.ConditionTrue, // No cleanup, so we are still "onboarding"
312+
Reason: ConditionReasonAborted,
313+
Message: err.Error(),
314+
}) {
315+
return ctrl.Result{}, errors.Join(err, r.Status().Update(ctx, hv))
316+
}
317+
318+
return ctrl.Result{}, err
297319
}
298320

299321
aggs, err := aggregatesByName(ctx, r.computeClient)
@@ -433,7 +455,7 @@ func (r *OnboardingController) createOrGetTestServer(ctx context.Context, zone,
433455
continue
434456
}
435457
if server.Name != serverName || foundServer != nil {
436-
log.Info("deleting server", "name", server.Name)
458+
log.Info("deleting outdated server", "name", server.Name)
437459
err = servers.Delete(ctx, r.testComputeClient, server.ID).ExtractErr()
438460
if err != nil && !gophercloud.ResponseCodeIs(err, http.StatusNotFound) {
439461
log.Error(err, "failed deleting instance due to", "instance", server.ID)

internal/controller/onboarding_controller_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,20 @@ import (
3131
)
3232

3333
var _ = Describe("Onboarding Controller", func() {
34+
const (
35+
hypervisorName = "some-test"
36+
)
37+
3438
var (
3539
onboardingReconciler *OnboardingController
40+
namespacedName = types.NamespacedName{Name: hypervisorName}
3641
)
3742

3843
Context("When reconciling a hypervisor", func() {
39-
const hypervisorName = "some-test"
40-
4144
ctx := context.Background() //nolint:govet
4245

4346
reconcileLoop := func(steps int) (res ctrl.Result, err error) {
44-
req := ctrl.Request{
45-
NamespacedName: types.NamespacedName{Name: hypervisorName},
46-
}
47+
req := ctrl.Request{NamespacedName: namespacedName}
4748
for range steps {
4849
res, err = onboardingReconciler.Reconcile(ctx, req)
4950
if err != nil {
@@ -60,19 +61,18 @@ var _ = Describe("Onboarding Controller", func() {
6061
}
6162

6263
By("creating the resource for the Kind Hypervisor")
63-
resource := &kvmv1.Hypervisor{
64+
hv := &kvmv1.Hypervisor{
6465
ObjectMeta: metav1.ObjectMeta{
6566
Name: hypervisorName,
6667
},
6768
Spec: kvmv1.HypervisorSpec{},
6869
}
69-
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
70-
})
70+
Expect(k8sClient.Create(ctx, hv)).To(Succeed())
7171

72-
AfterEach(func() {
73-
hv := &kvmv1.Hypervisor{ObjectMeta: metav1.ObjectMeta{Name: hypervisorName}}
74-
By("Cleanup the specific hypervisor CRO")
75-
Expect(client.IgnoreAlreadyExists(k8sClient.Delete(ctx, hv))).To(Succeed())
72+
DeferCleanup(func(ctx context.Context) {
73+
By("Cleanup the specific hypervisor CRO")
74+
Expect(client.IgnoreAlreadyExists(k8sClient.Delete(ctx, hv))).To(Succeed())
75+
})
7676
})
7777

7878
It("should successfully reconcile the resource", func() {

0 commit comments

Comments
 (0)