Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update K8s version on Capi machine for in-place upgrades #7325

Merged
merged 6 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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
}

Check warning on line 221 in controllers/controlplaneupgrade_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/controlplaneupgrade_controller.go#L220-L221

Added lines #L220 - L221 were not covered by tests
machinePatchHelper, err := patch.NewHelper(machine, r.client)
if err != nil {
return err
}

Check warning on line 225 in controllers/controlplaneupgrade_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/controlplaneupgrade_controller.go#L224-L225

Added lines #L224 - L225 were not covered by tests
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)
}

Check warning on line 230 in controllers/controlplaneupgrade_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/controlplaneupgrade_controller.go#L229-L230

Added lines #L229 - L230 were not covered by tests
nodesUpgradeCompleted++
nodesUpgradeRequired--
}
Expand All @@ -224,3 +238,11 @@
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)
}

Check warning on line 246 in controllers/controlplaneupgrade_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/controlplaneupgrade_controller.go#L245-L246

Added lines #L245 - L246 were not covered by tests
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"},
}
}