Skip to content
This repository was archived by the owner on Feb 1, 2026. It is now read-only.

Commit 0e55097

Browse files
authored
Merge pull request #415 from beraldoleal/clean-refact
ccruntime: processCcRuntimeDeleteRequest split
2 parents d0bdc5c + 42aec49 commit 0e55097

File tree

1 file changed

+88
-74
lines changed

1 file changed

+88
-74
lines changed

controllers/ccruntime_controller.go

Lines changed: 88 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,6 @@ func (r *CcRuntimeReconciler) setCleanupNodeLabels() (ctrl.Result, error) {
188188
}
189189

190190
func (r *CcRuntimeReconciler) processCcRuntimeDeleteRequest() (ctrl.Result, error) {
191-
var result = ctrl.Result{}
192-
193191
// Create the uninstall DaemonSet
194192
ds := r.processDaemonset(UninstallOperation)
195193
if err := controllerutil.SetControllerReference(r.ccRuntime, ds, r.Scheme); err != nil {
@@ -207,90 +205,106 @@ func (r *CcRuntimeReconciler) processCcRuntimeDeleteRequest() (ctrl.Result, erro
207205
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 10}, err
208206
}
209207

210-
if contains(r.ccRuntime.GetFinalizers(), RuntimeConfigFinalizer) {
211-
// Check for nodes with label set by install DS prestop hook.
212-
// If no nodes exist then remove finalizer and reconcile
213-
err, nodes := r.getNodesWithLabels(r.ccRuntime.Spec.Config.UninstallDoneLabel)
214-
if err != nil {
215-
r.Log.Error(err, "Error in getting list of nodes with uninstallDoneLabel")
216-
return ctrl.Result{}, err
208+
if !contains(r.ccRuntime.GetFinalizers(), RuntimeConfigFinalizer) {
209+
return ctrl.Result{}, err
210+
}
211+
212+
return handleFinalizers(r)
213+
}
214+
215+
func handleFinalizers(r *CcRuntimeReconciler) (ctrl.Result, error) {
216+
var result = ctrl.Result{}
217+
218+
// Check for nodes with label set by install DS prestop hook.
219+
// If no nodes exist then remove finalizer and reconcile
220+
err, nodes := r.getNodesWithLabels(r.ccRuntime.Spec.Config.UninstallDoneLabel)
221+
if err != nil {
222+
r.Log.Error(err, "Error in getting list of nodes with uninstallDoneLabel")
223+
return ctrl.Result{}, err
224+
}
225+
226+
finishedNodes := len(nodes.Items)
227+
228+
if allNodesDone(finishedNodes, r) {
229+
if r.ccRuntime.Spec.Config.PostUninstall.Image == "" {
230+
controllerutil.RemoveFinalizer(r.ccRuntime, RuntimeConfigFinalizer)
231+
232+
if err != nil {
233+
return result, err
234+
}
235+
236+
return r.updateCcRuntime()
217237
}
218-
finishedNodes := len(nodes.Items)
219-
if !allNodesDone(finishedNodes, r) {
220-
result, err = r.setCleanupNodeLabels()
238+
239+
// FXIME: This should be treated in a better way, as just having the sleep
240+
// here won't do us any good in the future.
241+
//
242+
// What's basically happening, and forcing us to do this, is the
243+
// fact that the Uninstall and postUninstall daemonsets are being
244+
// started at exactly the same time, leading to a race condition
245+
// when changing the containerd configuration.
246+
//
247+
// When looking at the kata-containers payload code, we see that the
248+
// the label is only set after containerd is successfully reconfigured,
249+
// and looking at this function we see we shouldn't reach this part
250+
// before the label is set. However, that's not what we're facing ...
251+
time.Sleep(time.Second * 60)
252+
253+
result, err = handlePostUninstall(r)
254+
if !result.Requeue {
255+
controllerutil.RemoveFinalizer(r.ccRuntime, RuntimeConfigFinalizer)
256+
result, err = r.updateCcRuntime()
221257
if err != nil {
222-
r.Log.Error(err, "updating the cleanup labels on nodes failed")
223-
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 5}, err
258+
return result, err
224259
}
225-
} else {
226-
if r.ccRuntime.Spec.Config.PostUninstall.Image == "" {
227-
controllerutil.RemoveFinalizer(r.ccRuntime, RuntimeConfigFinalizer)
228-
} else if r.ccRuntime.Spec.Config.PostUninstall.Image != "" {
229-
// FXIME: This should be treated in a better way, as just having the sleep
230-
// here won't do us any good in the future.
231-
//
232-
// What's basically happening, and forcing us to do this, is the
233-
// fact that the Uninstall and postUninstall daemonsets are being
234-
// started at exactly the same time, leading to a race condition
235-
// when changing the containerd configuration.
236-
//
237-
// When looking at the kata-containers payload code, we see that the
238-
// the label is only set after containerd is successfully reconfigured,
239-
// and looking at this function we see we shouldn't reach this part
240-
// before the label is set. However, that's not what we're facing ...
241-
time.Sleep(time.Second * 60)
242-
243-
result, err = handlePostUninstall(r)
244-
if !result.Requeue {
245-
controllerutil.RemoveFinalizer(r.ccRuntime, RuntimeConfigFinalizer)
246-
result, err = r.updateCcRuntime()
247-
if err != nil {
248-
return result, err
249-
}
250-
result, err = r.deleteUninstallDaemonsets()
251-
prepostLabels := map[string]string{}
252-
if r.ccRuntime.Spec.Config.PreInstall.Image != "" {
253-
prepostLabels[PreInstallDoneLabel[0]] = PreInstallDoneLabel[1]
254-
}
255-
if r.ccRuntime.Spec.Config.PostUninstall.Image != "" {
256-
prepostLabels[PostUninstallDoneLabel[0]] = PostUninstallDoneLabel[1]
260+
result, err = r.deleteUninstallDaemonsets()
261+
prepostLabels := map[string]string{}
262+
if r.ccRuntime.Spec.Config.PreInstall.Image != "" {
263+
prepostLabels[PreInstallDoneLabel[0]] = PreInstallDoneLabel[1]
264+
}
265+
if r.ccRuntime.Spec.Config.PostUninstall.Image != "" {
266+
prepostLabels[PostUninstallDoneLabel[0]] = PostUninstallDoneLabel[1]
257267

258-
}
259-
err, nodes := r.getNodesWithLabels(prepostLabels)
260-
if err != nil {
261-
r.Log.Error(err, "an error occured when getting the list of nodes from which we want to"+
262-
"remove preinstall/postuninstall labels")
263-
return ctrl.Result{}, err
264-
}
265-
postuninstalledNodes := len(nodes.Items)
266-
if !allNodesDone(postuninstalledNodes, r) {
267-
return ctrl.Result{Requeue: true}, nil
268-
} else {
269-
if postuninstalledNodes > 0 {
270-
result, err = r.removeNodeLabels(nodes)
271-
if err != nil {
272-
r.Log.Error(err, "removing the labels from nodes failed")
273-
return ctrl.Result{}, err
274-
}
275-
}
276-
}
277-
}
278268
}
269+
err, nodes := r.getNodesWithLabels(prepostLabels)
279270
if err != nil {
280-
return result, err
271+
r.Log.Error(err, "an error occured when getting the list of nodes from which we want to"+
272+
"remove preinstall/postuninstall labels")
273+
return ctrl.Result{}, err
281274
}
282-
result, err = r.updateCcRuntime()
283-
return result, err
284-
}
285275

286-
result, err = r.updateUninstallationStatus(finishedNodes)
276+
postuninstalledNodes := len(nodes.Items)
277+
if !allNodesDone(postuninstalledNodes, r) {
278+
return ctrl.Result{Requeue: true}, nil
279+
}
280+
281+
if postuninstalledNodes > 0 {
282+
result, err = r.removeNodeLabels(nodes)
283+
if err != nil {
284+
r.Log.Error(err, "removing the labels from nodes failed")
285+
return ctrl.Result{}, err
286+
}
287+
}
288+
}
287289
if err != nil {
288-
r.Log.Info("Error from updateUninstallationStatus")
289290
return result, err
290291
}
291-
result.Requeue = true
292-
result.RequeueAfter = time.Second * 10
292+
return r.updateCcRuntime()
293+
}
294+
295+
result, err = r.setCleanupNodeLabels()
296+
if err != nil {
297+
r.Log.Error(err, "updating the cleanup labels on nodes failed")
298+
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 5}, err
299+
}
300+
301+
result, err = r.updateUninstallationStatus(finishedNodes)
302+
if err != nil {
303+
r.Log.Info("Error from updateUninstallationStatus")
304+
return result, err
293305
}
306+
result.Requeue = true
307+
result.RequeueAfter = time.Second * 10
294308
return result, err
295309
}
296310

0 commit comments

Comments
 (0)