Skip to content

Commit 2d0094c

Browse files
committed
Onboarding: Cleanup also when we need to abort
The spec can disable the life-cycle or we can get a terminating condition, even while we are testing. We better clean up the test instances then too.
1 parent c6a2ca3 commit 2d0094c

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

internal/controller/onboarding_controller.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ var errRequeue = fmt.Errorf("requeue requested")
5353
const (
5454
defaultWaitTime = 1 * time.Minute
5555
ConditionTypeOnboarding = "Onboarding"
56+
ConditionReasonAborted = "aborted"
5657
ConditionReasonInitial = "initial"
5758
ConditionReasonOnboarding = "onboarding"
5859
ConditionReasonTesting = "testing"
@@ -101,17 +102,18 @@ func (r *OnboardingController) Reconcile(ctx context.Context, req ctrl.Request)
101102
return ctrl.Result{}, k8sclient.IgnoreNotFound(err)
102103
}
103104

105+
computeHost := hv.Name
106+
104107
// check if lifecycle management is enabled
105108
if !hv.Spec.LifecycleEnabled {
106-
return ctrl.Result{}, nil
109+
return r.abortOnboarding(ctx, hv, computeHost)
107110
}
108111

109112
// check if hv is terminating
110113
if meta.IsStatusConditionTrue(hv.Status.Conditions, kvmv1.ConditionTypeTerminating) {
111-
return ctrl.Result{}, nil
114+
return r.abortOnboarding(ctx, hv, computeHost)
112115
}
113116

114-
computeHost := hv.Name
115117
// We bail here out, because the openstack api is not the best to poll
116118
if hv.Status.HypervisorID == "" || hv.Status.ServiceID == "" {
117119
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
@@ -168,6 +170,34 @@ func (r *OnboardingController) Reconcile(ctx context.Context, req ctrl.Request)
168170
}
169171
}
170172

173+
func (r *OnboardingController) abortOnboarding(ctx context.Context, hv *kvmv1.Hypervisor, computeHost string) (ctrl.Result, error) {
174+
status := meta.FindStatusCondition(hv.Status.Conditions, ConditionTypeOnboarding)
175+
// Never onboarded
176+
if status == nil {
177+
return ctrl.Result{}, nil
178+
}
179+
180+
changed := meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{
181+
Type: kvmv1.ConditionTypeReady,
182+
Status: metav1.ConditionFalse,
183+
Reason: ConditionReasonOnboarding,
184+
Message: "Onboarding aborted",
185+
}) || meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{
186+
Type: ConditionTypeOnboarding,
187+
Status: metav1.ConditionTrue,
188+
Reason: ConditionReasonAborted,
189+
Message: "Aborted due to LivecycleEnabled being false",
190+
})
191+
if !changed {
192+
// Already aborted
193+
return ctrl.Result{}, nil
194+
}
195+
if err := r.deleteTestServers(ctx, computeHost); err != nil {
196+
return ctrl.Result{}, err
197+
}
198+
return ctrl.Result{}, r.Status().Update(ctx, hv)
199+
}
200+
171201
func (r *OnboardingController) initialOnboarding(ctx context.Context, hv *kvmv1.Hypervisor, host string) error {
172202
node := &corev1.Node{}
173203
if err := r.Get(ctx, types.NamespacedName{Name: hv.Name}, node); err != nil {

0 commit comments

Comments
 (0)