From 66db899a31052fe3efdb157019e42a69cb7225ab Mon Sep 17 00:00:00 2001 From: Raymond Zhang <67798267+raymond-zhang00@users.noreply.github.com> Date: Fri, 19 Jan 2024 16:57:54 -0600 Subject: [PATCH] adding docker upgrade rollout strategy (#7315) * adding docker upgrade rollout strategy * refactoring upgrade rollout strategy tests --- pkg/providers/docker/config/template-cp.yaml | 5 +++ pkg/providers/docker/config/template-md.yaml | 6 +++ pkg/providers/docker/controlplane_test.go | 27 ++++++++++++ pkg/providers/docker/docker.go | 11 +++++ pkg/providers/docker/workers_test.go | 45 ++++++++++++++++++++ 5 files changed, 94 insertions(+) diff --git a/pkg/providers/docker/config/template-cp.yaml b/pkg/providers/docker/config/template-cp.yaml index 793051977eb9..3eea40e85d5a 100644 --- a/pkg/providers/docker/config/template-cp.yaml +++ b/pkg/providers/docker/config/template-cp.yaml @@ -257,6 +257,11 @@ spec: - systemctl restart containerd {{- end }} replicas: {{.control_plane_replicas}} +{{- if .upgradeRolloutStrategy }} + rolloutStrategy: + rollingUpdate: + maxSurge: {{.maxSurge}} +{{- end }} version: {{.kubernetesVersion}} {{- if .externalEtcd }} --- diff --git a/pkg/providers/docker/config/template-md.yaml b/pkg/providers/docker/config/template-md.yaml index ae8f701289bd..94d8867e7437 100644 --- a/pkg/providers/docker/config/template-md.yaml +++ b/pkg/providers/docker/config/template-md.yaml @@ -94,6 +94,12 @@ spec: name: {{.workloadTemplateName}} namespace: {{.eksaSystemNamespace}} version: {{.kubernetesVersion}} +{{- if .upgradeRolloutStrategy }} + strategy: + rollingUpdate: + maxSurge: {{.maxSurge}} + maxUnavailable: {{.maxUnavailable}} +{{- end }} --- apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 kind: DockerMachineTemplate diff --git a/pkg/providers/docker/controlplane_test.go b/pkg/providers/docker/controlplane_test.go index c93ba2f134bf..e379cd09fc78 100644 --- a/pkg/providers/docker/controlplane_test.go +++ b/pkg/providers/docker/controlplane_test.go @@ -10,6 +10,7 @@ import ( . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/intstr" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" @@ -271,6 +272,32 @@ func TestControlPlaneSpecRegistryMirrorConfiguration(t *testing.T) { } } +func TestControlPlaneUpgradeRolloutStrategy(t *testing.T) { + g := NewWithT(t) + logger := test.NewNullLogger() + ctx := context.Background() + client := test.NewFakeKubeClient() + spec := testClusterSpec(func(s *cluster.Spec) { + s.Cluster.Spec.ControlPlaneConfiguration.UpgradeRolloutStrategy = &anywherev1.ControlPlaneUpgradeRolloutStrategy{ + RollingUpdate: anywherev1.ControlPlaneRollingUpdateParams{ + MaxSurge: 1, + }, + } + }) + + cp, err := docker.ControlPlaneSpec(ctx, logger, client, spec) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(cp).NotTo(BeNil()) + g.Expect(cp.KubeadmControlPlane).To(Equal(kubeadmControlPlane(func(k *controlplanev1.KubeadmControlPlane) { + maxSurge := intstr.FromInt(1) + k.Spec.RolloutStrategy = &controlplanev1.RolloutStrategy{ + RollingUpdate: &controlplanev1.RollingUpdate{ + MaxSurge: &maxSurge, + }, + } + }))) +} + func testClusterSpec(opts ...test.ClusterSpecOpt) *cluster.Spec { name := "test" namespace := "test-namespace" diff --git a/pkg/providers/docker/docker.go b/pkg/providers/docker/docker.go index d9e0db8b083b..662bf816525c 100644 --- a/pkg/providers/docker/docker.go +++ b/pkg/providers/docker/docker.go @@ -198,6 +198,12 @@ func (d *DockerTemplateBuilder) GenerateCAPISpecWorkers(clusterSpec *cluster.Spe values["workloadTemplateName"] = workloadTemplateNames[workerNodeGroupConfiguration.Name] values["workloadkubeadmconfigTemplateName"] = kubeadmconfigTemplateNames[workerNodeGroupConfiguration.Name] + if workerNodeGroupConfiguration.UpgradeRolloutStrategy != nil { + values["upgradeRolloutStrategy"] = true + values["maxSurge"] = workerNodeGroupConfiguration.UpgradeRolloutStrategy.RollingUpdate.MaxSurge + values["maxUnavailable"] = workerNodeGroupConfiguration.UpgradeRolloutStrategy.RollingUpdate.MaxUnavailable + } + bytes, err := templater.Execute(defaultCAPIConfigMD, values) if err != nil { return nil, err @@ -316,6 +322,11 @@ func buildTemplateMapCP(clusterSpec *cluster.Spec) (map[string]interface{}, erro } } + if clusterSpec.Cluster.Spec.ControlPlaneConfiguration.UpgradeRolloutStrategy != nil { + values["upgradeRolloutStrategy"] = true + values["maxSurge"] = clusterSpec.Cluster.Spec.ControlPlaneConfiguration.UpgradeRolloutStrategy.RollingUpdate.MaxSurge + } + return values, nil } diff --git a/pkg/providers/docker/workers_test.go b/pkg/providers/docker/workers_test.go index 193150fa1d2a..b3a586637520 100644 --- a/pkg/providers/docker/workers_test.go +++ b/pkg/providers/docker/workers_test.go @@ -8,6 +8,7 @@ import ( . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/intstr" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" dockerv1 "sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1beta1" @@ -15,6 +16,7 @@ import ( "github.com/aws/eks-anywhere/internal/test" anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1" "github.com/aws/eks-anywhere/pkg/clients/kubernetes" + "github.com/aws/eks-anywhere/pkg/cluster" "github.com/aws/eks-anywhere/pkg/clusterapi" "github.com/aws/eks-anywhere/pkg/controller/clientutil" "github.com/aws/eks-anywhere/pkg/providers/docker" @@ -334,6 +336,49 @@ func TestWorkersSpecRegistryMirrorConfiguration(t *testing.T) { } } +func TestWorkersSpecUpgradeRolloutStrategy(t *testing.T) { + g := NewWithT(t) + logger := test.NewNullLogger() + ctx := context.Background() + spec := testClusterSpec(func(s *cluster.Spec) { + s.Cluster.Spec.WorkerNodeGroupConfigurations = []anywherev1.WorkerNodeGroupConfiguration{ + { + Count: ptr.Int(3), + MachineGroupRef: &anywherev1.Ref{Name: "test"}, + Name: "md-0", + UpgradeRolloutStrategy: &anywherev1.WorkerNodesUpgradeRolloutStrategy{ + RollingUpdate: anywherev1.WorkerNodesRollingUpdateParams{ + MaxSurge: 1, + MaxUnavailable: 0, + }, + }, + }, + } + }) + client := test.NewFakeKubeClient() + + workers, err := docker.WorkersSpec(ctx, logger, client, spec) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(workers).NotTo(BeNil()) + g.Expect(workers.Groups).To(HaveLen(1)) + g.Expect(workers.Groups).To(ConsistOf( + clusterapi.WorkerGroup[*dockerv1.DockerMachineTemplate]{ + KubeadmConfigTemplate: kubeadmConfigTemplate(), + MachineDeployment: machineDeployment(func(m *clusterv1.MachineDeployment) { + maxSurge := intstr.FromInt(1) + maxUnavailable := intstr.FromInt(0) + m.Spec.Strategy = &clusterv1.MachineDeploymentStrategy{ + RollingUpdate: &clusterv1.MachineRollingUpdateDeployment{ + MaxSurge: &maxSurge, + MaxUnavailable: &maxUnavailable, + }, + } + }), + ProviderMachineTemplate: dockerMachineTemplate("test-md-0-1"), + }, + )) +} + func kubeadmConfigTemplate(opts ...func(*bootstrapv1.KubeadmConfigTemplate)) *bootstrapv1.KubeadmConfigTemplate { o := &bootstrapv1.KubeadmConfigTemplate{ TypeMeta: metav1.TypeMeta{