Skip to content

Commit

Permalink
Update K8s version on Capi machine for in-place upgrades (#7325)
Browse files Browse the repository at this point in the history
* Patch capi machine object with updated k8s version for in-place for control plane nodes

Signed-off-by: Rahul Ganesh <[email protected]>
---------

Signed-off-by: Rahul Ganesh <[email protected]>
Co-authored-by: Rahul Ganesh <[email protected]>
  • Loading branch information
rahulbabu95 and Rahul Ganesh authored Jan 23, 2024
1 parent a5676f3 commit e616a36
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
22 changes: 22 additions & 0 deletions controllers/controlplaneupgrade_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kerrors "k8s.io/apimachinery/pkg/util/errors"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/patch"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -214,6 +215,19 @@ func (r *ControlPlaneUpgradeReconciler) updateStatus(ctx context.Context, log lo
return fmt.Errorf("getting node upgrader for machine %s: %v", machineRef.Name, err)
}
if nodeUpgrade.Status.Completed {
machine, err := getCapiMachine(ctx, r.client, nodeUpgrade)
if err != nil {
return err
}
machinePatchHelper, err := patch.NewHelper(machine, r.client)
if err != nil {
return err
}
log.Info("Updating K8s version in machine", "Machine", machine.Name)
machine.Spec.Version = &nodeUpgrade.Spec.KubernetesVersion
if err := machinePatchHelper.Patch(ctx, machine); err != nil {
return fmt.Errorf("updating spec for machine %s: %v", machine.Name, err)
}
nodesUpgradeCompleted++
nodesUpgradeRequired--
}
Expand All @@ -224,3 +238,11 @@ func (r *ControlPlaneUpgradeReconciler) updateStatus(ctx context.Context, log lo
cpUpgrade.Status.Ready = nodesUpgradeRequired == 0
return nil
}

func getCapiMachine(ctx context.Context, client client.Client, nodeUpgrade *anywherev1.NodeUpgrade) (*clusterv1.Machine, error) {
machine := &clusterv1.Machine{}
if err := client.Get(ctx, GetNamespacedNameType(nodeUpgrade.Spec.Machine.Name, nodeUpgrade.Spec.Machine.Namespace), machine); err != nil {
return nil, fmt.Errorf("getting machine %s: %v", nodeUpgrade.Spec.Machine.Name, err)
}
return machine, nil
}
28 changes: 28 additions & 0 deletions controllers/controlplaneupgrade_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers_test
import (
"context"
"fmt"
"strings"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -191,6 +192,33 @@ func TestCPUpgradeObjectDoesNotExist(t *testing.T) {
g.Expect(err).To(MatchError("controlplaneupgrades.anywhere.eks.amazonaws.com \"cp-upgrade-request\" not found"))
}

func TestCPUpgradeReconcileUpdateCapiMachineVersion(t *testing.T) {
g := NewWithT(t)
ctx := context.Background()

cluster, machines, nodes, cpUpgrade, nodeUpgrades := getObjectsForCPUpgradeTest()
for i := range nodeUpgrades {
nodeUpgrades[i].Name = fmt.Sprintf("%s-node-upgrader", machines[i].Name)
nodeUpgrades[i].Status = anywherev1.NodeUpgradeStatus{
Completed: true,
}
}
objs := []runtime.Object{cluster, machines[0], machines[1], nodes[0], nodes[1], cpUpgrade, nodeUpgrades[0], nodeUpgrades[1]}
nodeUpgrades[0].Status.Completed = true
client := fake.NewClientBuilder().WithRuntimeObjects(objs...).Build()
r := controllers.NewControlPlaneUpgradeReconciler(client)

req := cpUpgradeRequest(cpUpgrade)
_, err := r.Reconcile(ctx, req)
g.Expect(err).ToNot(HaveOccurred())
machine := &clusterv1.Machine{}
err = client.Get(ctx, types.NamespacedName{Name: nodeUpgrades[0].Spec.Machine.Name, Namespace: "eksa-system"}, machine)
g.Expect(err).ToNot(HaveOccurred())
if !strings.Contains(*machine.Spec.Version, "v1.28.3-eks-1-28-9") {
t.Fatalf("unexpected k8s version in capi machine: %s", *machine.Spec.Version)
}
}

func getObjectsForCPUpgradeTest() (*clusterv1.Cluster, []*clusterv1.Machine, []*corev1.Node, *anywherev1.ControlPlaneUpgrade, []*anywherev1.NodeUpgrade) {
cluster := generateCluster()
node1 := generateNode()
Expand Down
6 changes: 3 additions & 3 deletions controllers/nodeupgrade_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func generateNodeUpgrade(machine *clusterv1.Machine) *anywherev1.NodeUpgrade {
Name: machine.Name,
Namespace: machine.Namespace,
},
KubernetesVersion: "v1.28.1",
KubernetesVersion: "v1.28.3-eks-1-28-9",
},
}
}
Expand All @@ -258,7 +258,7 @@ func generateMachine(cluster *clusterv1.Cluster, node *corev1.Node) *clusterv1.M
Namespace: "eksa-system",
},
Spec: clusterv1.MachineSpec{
Version: ptr.String("v1.28.0"),
Version: ptr.String("v1.27.8-eks-1-27-18"),
ClusterName: cluster.Name,
},
Status: clusterv1.MachineStatus{
Expand Down Expand Up @@ -292,6 +292,6 @@ func generateConfigMap() *corev1.ConfigMap {
Name: "in-place-upgrade",
Namespace: "eksa-system",
},
Data: map[string]string{"v1.28.1": "test"},
Data: map[string]string{"v1.28.3-eks-1-28-9": "test"},
}
}

0 comments on commit e616a36

Please sign in to comment.