Skip to content

Commit

Permalink
Support control plane only clusters (#14)
Browse files Browse the repository at this point in the history
* Support control plane only clusters

Signed-off-by: Atanas Dinov <[email protected]>

* Drop unnecessary reconciliations

Signed-off-by: Atanas Dinov <[email protected]>

---------

Signed-off-by: Atanas Dinov <[email protected]>
  • Loading branch information
atanasdinov authored Jul 22, 2024
1 parent 0ec27f4 commit 0aaa478
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
45 changes: 26 additions & 19 deletions internal/controller/reconcile_kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/suse-edge/upgrade-controller/internal/upgrade"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -22,16 +21,7 @@ func (r *UpgradePlanReconciler) reconcileKubernetes(ctx context.Context, upgrade
return ctrl.Result{}, err
}

return r.createPlan(ctx, upgradePlan, controlPlanePlan)
}

workerPlan := upgrade.KubernetesWorkerPlan(kubernetesVersion)
if err := r.Get(ctx, client.ObjectKeyFromObject(workerPlan), workerPlan); err != nil {
if !errors.IsNotFound(err) {
return ctrl.Result{}, err
}

return r.createPlan(ctx, upgradePlan, workerPlan)
return ctrl.Result{}, r.createPlan(ctx, upgradePlan, controlPlanePlan)
}

nodeList := &corev1.NodeList{}
Expand All @@ -45,10 +35,20 @@ func (r *UpgradePlanReconciler) reconcileKubernetes(ctx context.Context, upgrade
}

if !isKubernetesUpgraded(nodeList, selector, kubernetesVersion) {
condition := metav1.Condition{Type: lifecyclev1alpha1.KubernetesUpgradedCondition, Status: metav1.ConditionFalse, Reason: lifecyclev1alpha1.UpgradeInProgress, Message: "Control plane nodes are being upgraded"}
meta.SetStatusCondition(&upgradePlan.Status.Conditions, condition)

setInProgressCondition(upgradePlan, lifecyclev1alpha1.KubernetesUpgradedCondition, "Control plane nodes are being upgraded")
return ctrl.Result{}, nil
} else if controlPlaneOnlyCluster(nodeList) {
setSuccessfulCondition(upgradePlan, lifecyclev1alpha1.KubernetesUpgradedCondition, "All cluster nodes are upgraded")
return ctrl.Result{Requeue: true}, nil
}

workerPlan := upgrade.KubernetesWorkerPlan(kubernetesVersion)
if err = r.Get(ctx, client.ObjectKeyFromObject(workerPlan), workerPlan); err != nil {
if !errors.IsNotFound(err) {
return ctrl.Result{}, err
}

return ctrl.Result{}, r.createPlan(ctx, upgradePlan, workerPlan)
}

selector, err = metav1.LabelSelectorAsSelector(workerPlan.Spec.NodeSelector)
Expand All @@ -57,14 +57,11 @@ func (r *UpgradePlanReconciler) reconcileKubernetes(ctx context.Context, upgrade
}

if !isKubernetesUpgraded(nodeList, selector, kubernetesVersion) {
condition := metav1.Condition{Type: lifecyclev1alpha1.KubernetesUpgradedCondition, Status: metav1.ConditionFalse, Reason: lifecyclev1alpha1.UpgradeInProgress, Message: "Worker nodes are being upgraded"}
meta.SetStatusCondition(&upgradePlan.Status.Conditions, condition)
setInProgressCondition(upgradePlan, lifecyclev1alpha1.KubernetesUpgradedCondition, "Worker nodes are being upgraded")
return ctrl.Result{}, nil
}

condition := metav1.Condition{Type: lifecyclev1alpha1.KubernetesUpgradedCondition, Status: metav1.ConditionTrue, Reason: lifecyclev1alpha1.UpgradeSucceeded, Message: "All cluster nodes are upgraded"}
meta.SetStatusCondition(&upgradePlan.Status.Conditions, condition)

setSuccessfulCondition(upgradePlan, lifecyclev1alpha1.KubernetesUpgradedCondition, "All cluster nodes are upgraded")
return ctrl.Result{Requeue: true}, nil
}

Expand Down Expand Up @@ -93,3 +90,13 @@ func isKubernetesUpgraded(nodeList *corev1.NodeList, selector labels.Selector, k

return true
}

func controlPlaneOnlyCluster(nodeList *corev1.NodeList) bool {
for _, node := range nodeList.Items {
if node.Labels[upgrade.ControlPlaneLabel] != "true" {
return false
}
}

return true
}
18 changes: 14 additions & 4 deletions internal/controller/upgradeplan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,28 @@ func (r *UpgradePlanReconciler) recordCreatedPlan(upgradePlan *lifecyclev1alpha1
r.Recorder.Eventf(upgradePlan, corev1.EventTypeNormal, "PlanCreated", "Upgrade plan created: %s/%s", namespace, name)
}

func (r *UpgradePlanReconciler) createPlan(ctx context.Context, upgradePlan *lifecyclev1alpha1.UpgradePlan, plan *upgradecattlev1.Plan) (ctrl.Result, error) {
func (r *UpgradePlanReconciler) createPlan(ctx context.Context, upgradePlan *lifecyclev1alpha1.UpgradePlan, plan *upgradecattlev1.Plan) error {
if err := ctrl.SetControllerReference(upgradePlan, plan, r.Scheme); err != nil {
return ctrl.Result{}, fmt.Errorf("setting controller reference: %w", err)
return fmt.Errorf("setting controller reference: %w", err)
}

if err := r.Create(ctx, plan); err != nil {
return ctrl.Result{}, fmt.Errorf("creating upgrade plan: %w", err)
return fmt.Errorf("creating upgrade plan: %w", err)
}

r.recordCreatedPlan(upgradePlan, plan.Name, plan.Namespace)

return ctrl.Result{Requeue: true}, nil
return nil
}

func setInProgressCondition(plan *lifecyclev1alpha1.UpgradePlan, conditionType, message string) {
condition := metav1.Condition{Type: conditionType, Status: metav1.ConditionFalse, Reason: lifecyclev1alpha1.UpgradeInProgress, Message: message}
meta.SetStatusCondition(&plan.Status.Conditions, condition)
}

func setSuccessfulCondition(plan *lifecyclev1alpha1.UpgradePlan, conditionType, message string) {
condition := metav1.Condition{Type: conditionType, Status: metav1.ConditionTrue, Reason: lifecyclev1alpha1.UpgradeSucceeded, Message: message}
meta.SetStatusCondition(&plan.Status.Conditions, condition)
}

// SetupWithManager sets up the controller with the Manager.
Expand Down
2 changes: 1 addition & 1 deletion internal/upgrade/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
const (
upgradeNamespace = "cattle-system"

controlPlaneLabel = "node-role.kubernetes.io/control-plane"
ControlPlaneLabel = "node-role.kubernetes.io/control-plane"
)

func baseUpgradePlan(name string) *upgradecattlev1.Plan {
Expand Down
6 changes: 3 additions & 3 deletions internal/upgrade/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func KubernetesControlPlanePlan(version string) *upgradecattlev1.Plan {
controlPlanePlan.Spec.NodeSelector = &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: controlPlaneLabel,
Key: ControlPlaneLabel,
Operator: "In",
Values: []string{
"true",
Expand All @@ -52,7 +52,7 @@ func KubernetesControlPlanePlan(version string) *upgradecattlev1.Plan {
Effect: "NoExecute",
},
{
Key: controlPlaneLabel,
Key: ControlPlaneLabel,
Operator: "Equal",
Value: "",
Effect: "NoSchedule",
Expand Down Expand Up @@ -80,7 +80,7 @@ func KubernetesWorkerPlan(version string) *upgradecattlev1.Plan {
workerPlan.Spec.NodeSelector = &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: controlPlaneLabel,
Key: ControlPlaneLabel,
Operator: "NotIn",
Values: []string{
"true",
Expand Down

0 comments on commit 0aaa478

Please sign in to comment.