Skip to content

Commit

Permalink
Drain refactor to handle complex cluster types (#57)
Browse files Browse the repository at this point in the history
* Drain refactor to handle complex cluster types

* Bring back old func name

* Rework drain configuration to support only disables

* Rework user configured drain option implementation

* Use normal value for DisableDrain
  • Loading branch information
ipetrov117 authored Aug 23, 2024
1 parent 8db0eb2 commit 3e14ffd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 42 deletions.
11 changes: 5 additions & 6 deletions api/v1alpha1/upgradeplan_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,16 @@ type UpgradePlanSpec struct {
// ReleaseVersion specifies the target version for platform upgrade.
// The version format is X.Y.Z, for example "3.0.2".
ReleaseVersion string `json:"releaseVersion"`
// Drain specifies whether control-plane and worker nodes should be drained.
// If left unspecified, drain is done on both control-plane and worker nodes by default.
// DisableDrain specifies whether control-plane and worker nodes drain should be disabled.
// +optional
Drain *Drain `json:"drain"`
DisableDrain DisableDrain `json:"disableDrain"`
}

type Drain struct {
type DisableDrain struct {
// +optional
ControlPlane *bool `json:"controlPlane"`
ControlPlane bool `json:"controlPlane"`
// +optional
Worker *bool `json:"worker"`
Worker bool `json:"worker"`
}

// UpgradePlanStatus defines the observed state of UpgradePlan
Expand Down
26 changes: 6 additions & 20 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions config/crd/bases/lifecycle.suse.com_upgradeplans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ spec:
spec:
description: UpgradePlanSpec defines the desired state of UpgradePlan
properties:
drain:
description: |-
Drain specifies whether control-plane and worker nodes should be drained.
If left unspecified, drain is done on both control-plane and worker nodes by default.
disableDrain:
description: DisableDrain specifies whether control-plane and worker
nodes drain should be disabled.
properties:
controlPlane:
type: boolean
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/reconcile_kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (r *UpgradePlanReconciler) reconcileKubernetes(ctx context.Context, upgrade
}

identifierAnnotations := upgrade.PlanIdentifierAnnotations(upgradePlan.Name, upgradePlan.Namespace)
drainControlPlane, drainWorker := parseDrainOptions(upgradePlan)
drainControlPlane, drainWorker := parseDrainOptions(nodeList, upgradePlan)
controlPlanePlan := upgrade.KubernetesControlPlanePlan(kubernetesVersion, drainControlPlane, identifierAnnotations)
if err = r.Get(ctx, client.ObjectKeyFromObject(controlPlanePlan), controlPlanePlan); err != nil {
if !errors.IsNotFound(err) {
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/reconcile_os.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (r *UpgradePlanReconciler) reconcileOS(ctx context.Context, upgradePlan *li
return ctrl.Result{}, r.createObject(ctx, upgradePlan, secret)
}

drainControlPlane, drainWorker := parseDrainOptions(upgradePlan)
drainControlPlane, drainWorker := parseDrainOptions(nodeList, upgradePlan)
controlPlanePlan := upgrade.OSControlPlanePlan(releaseVersion, secret.Name, releaseOS, drainControlPlane, identifierAnnotations)
if err = r.Get(ctx, client.ObjectKeyFromObject(controlPlanePlan), controlPlanePlan); err != nil {
if !errors.IsNotFound(err) {
Expand Down
41 changes: 31 additions & 10 deletions internal/controller/upgradeplan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,39 @@ func isHelmUpgradeFinished(plan *lifecyclev1alpha1.UpgradePlan, conditionType st
return false
}

func parseDrainOptions(plan *lifecyclev1alpha1.UpgradePlan) (drainControlPlane bool, drainWorker bool) {
drainControlPlane = true
drainWorker = true

if plan.Spec.Drain != nil {
if plan.Spec.Drain.ControlPlane != nil {
drainControlPlane = *plan.Spec.Drain.ControlPlane
func parseDrainOptions(nodeList *corev1.NodeList, plan *lifecyclev1alpha1.UpgradePlan) (drainControlPlane bool, drainWorker bool) {
var controlPlaneCounter, workerCounter int
for _, node := range nodeList.Items {
if node.Labels[upgrade.ControlPlaneLabel] != "true" {
workerCounter++
} else {
controlPlaneCounter++
}
}

if plan.Spec.Drain.Worker != nil {
drainWorker = *plan.Spec.Drain.Worker
}
switch {
case controlPlaneCounter > 1 && workerCounter <= 1:
drainControlPlane = true
drainWorker = false
case controlPlaneCounter == 1 && workerCounter > 1:
drainControlPlane = false
drainWorker = true
case controlPlaneCounter <= 1 && workerCounter <= 1:
drainControlPlane = false
drainWorker = false
default:
drainControlPlane = true
drainWorker = true
}

// If user has explicitly disabled control-plane drains
if plan.Spec.DisableDrain.ControlPlane {
drainControlPlane = false
}

// If user has explicitly disabled worker drains
if plan.Spec.DisableDrain.Worker {
drainWorker = false
}

return drainControlPlane, drainWorker
Expand Down

0 comments on commit 3e14ffd

Please sign in to comment.