From 869ed512479b28f3c80c76b9e8974342af539e22 Mon Sep 17 00:00:00 2001 From: eaudetcobello Date: Tue, 30 Jul 2024 13:37:07 -0400 Subject: [PATCH] simplify impl. of disabledefaultcni --- .../cloudinit/cloudinit_common_test.go | 122 +++++++----------- controllers/cloudinit/controlplane_init.go | 9 +- .../cloudinit/controlplane_init_test.go | 3 +- controllers/cloudinit/controlplane_join.go | 9 +- .../cloudinit/controlplane_join_test.go | 3 +- controllers/cloudinit/embed.go | 4 - .../cloudinit/scripts/00-install-microk8s.sh | 10 +- .../scripts/10-disable-default-cni.sh | 22 ---- controllers/cloudinit/worker_join.go | 2 +- controllers/cloudinit/worker_join_test.go | 2 +- 10 files changed, 59 insertions(+), 127 deletions(-) mode change 100644 => 100755 controllers/cloudinit/scripts/00-install-microk8s.sh delete mode 100644 controllers/cloudinit/scripts/10-disable-default-cni.sh diff --git a/controllers/cloudinit/cloudinit_common_test.go b/controllers/cloudinit/cloudinit_common_test.go index a790c43..25c9418 100644 --- a/controllers/cloudinit/cloudinit_common_test.go +++ b/controllers/cloudinit/cloudinit_common_test.go @@ -78,9 +78,9 @@ func TestCloudConfigInput(t *testing.T) { g.Expect(err).NotTo(HaveOccurred()) if confinement == "classic" { - g.Expect(c.RunCommands).To(ContainElement(fmt.Sprintf(`/capi-scripts/00-install-microk8s.sh "--channel 1.25/%s --classic"`, risk))) + g.Expect(c.RunCommands).To(ContainElement(fmt.Sprintf(`/capi-scripts/00-install-microk8s.sh "--channel 1.25/%s --classic" false`, risk))) } else { - g.Expect(c.RunCommands).To(ContainElement(fmt.Sprintf(`/capi-scripts/00-install-microk8s.sh "--channel 1.25-strict/%s"`, risk))) + g.Expect(c.RunCommands).To(ContainElement(fmt.Sprintf(`/capi-scripts/00-install-microk8s.sh "--channel 1.25-strict/%s" false`, risk))) } _, err = cloudinit.GenerateCloudConfig(c) @@ -93,6 +93,48 @@ func TestCloudConfigInput(t *testing.T) { } }) + t.Run("DisableDefaultCNI", func(t *testing.T) { + for _, tc := range []struct { + name string + makeCloudConfig func() (*cloudinit.CloudConfig, error) + }{ + { + name: "ControlPlaneJoin", + makeCloudConfig: func() (*cloudinit.CloudConfig, error) { + return cloudinit.NewJoinControlPlane(&cloudinit.ControlPlaneJoinInput{ + KubernetesVersion: "v1.25.0", + Confinement: "classic", + Token: strings.Repeat("a", 32), + TokenTTL: 100, + DisableDefaultCNI: true, + }) + }, + }, + { + name: "ControlPlaneInit", + makeCloudConfig: func() (*cloudinit.CloudConfig, error) { + return cloudinit.NewInitControlPlane(&cloudinit.ControlPlaneInitInput{ + KubernetesVersion: "v1.25.0", + Confinement: "classic", + Token: strings.Repeat("a", 32), + TokenTTL: 100, + DisableDefaultCNI: true, + }) + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + g := NewWithT(t) + c, err := tc.makeCloudConfig() + g.Expect(err).NotTo(HaveOccurred()) + + g.Expect(c.RunCommands).To(ContainElement(`/capi-scripts/00-install-microk8s.sh "--channel 1.25 --classic" true`)) + _, err = cloudinit.GenerateCloudConfig(c) + g.Expect(err).NotTo(HaveOccurred()) + }) + } + }) + t.Run("ExtraWriteFiles", func(t *testing.T) { files := []v1beta1.CloudInitWriteFile{{ Content: "contents", @@ -382,80 +424,4 @@ func TestCloudConfigInput(t *testing.T) { }) } }) - - t.Run("DisableDefaultCNI", func(t *testing.T) { - for _, tc := range []struct { - name string - makeCloudConfig func() (*cloudinit.CloudConfig, error) - }{ - { - name: "ControlPlaneInit", - makeCloudConfig: func() (*cloudinit.CloudConfig, error) { - return cloudinit.NewInitControlPlane(&cloudinit.ControlPlaneInitInput{ - DisableDefaultCNI: true, - KubernetesVersion: "v1.25.0", - Token: strings.Repeat("a", 32), - TokenTTL: 100, - }) - }, - }, - { - name: "ControlPlaneJoin", - makeCloudConfig: func() (*cloudinit.CloudConfig, error) { - return cloudinit.NewJoinControlPlane(&cloudinit.ControlPlaneJoinInput{ - DisableDefaultCNI: true, - KubernetesVersion: "v1.25.0", - Token: strings.Repeat("a", 32), - TokenTTL: 100, - }) - }, - }, - } { - t.Run(tc.name, func(t *testing.T) { - g := NewWithT(t) - c, err := tc.makeCloudConfig() - g.Expect(err).NotTo(HaveOccurred()) - - g.Expect(c.RunCommands).To(ContainElement(`/capi-scripts/10-disable-default-cni.sh`)) - }) - } - }) - - t.Run("DefaultCNI", func(t *testing.T) { - for _, tc := range []struct { - name string - makeCloudConfig func() (*cloudinit.CloudConfig, error) - }{ - { - name: "ControlPlaneInit", - makeCloudConfig: func() (*cloudinit.CloudConfig, error) { - return cloudinit.NewInitControlPlane(&cloudinit.ControlPlaneInitInput{ - DisableDefaultCNI: false, - KubernetesVersion: "v1.25.0", - Token: strings.Repeat("a", 32), - TokenTTL: 100, - }) - }, - }, - { - name: "ControlPlaneJoin", - makeCloudConfig: func() (*cloudinit.CloudConfig, error) { - return cloudinit.NewJoinControlPlane(&cloudinit.ControlPlaneJoinInput{ - DisableDefaultCNI: false, - KubernetesVersion: "v1.25.0", - Token: strings.Repeat("a", 32), - TokenTTL: 100, - }) - }, - }, - } { - t.Run(tc.name, func(t *testing.T) { - g := NewWithT(t) - c, err := tc.makeCloudConfig() - g.Expect(err).NotTo(HaveOccurred()) - - g.Expect(c.RunCommands).NotTo(ContainElement(`/capi-scripts/10-disable-default-cni.sh`)) - }) - } - }) } diff --git a/controllers/cloudinit/controlplane_init.go b/controllers/cloudinit/controlplane_init.go index c08f83b..461a6a2 100644 --- a/controllers/cloudinit/controlplane_init.go +++ b/controllers/cloudinit/controlplane_init.go @@ -143,17 +143,10 @@ func NewInitControlPlane(input *ControlPlaneInitInput) (*CloudConfig, error) { fmt.Sprintf("%s %q %q", scriptPath(snapstoreHTTPProxyScript), input.SnapstoreHTTPProxy, input.SnapstoreHTTPSProxy), fmt.Sprintf("%s %q %q", scriptPath(snapstoreProxyScript), input.SnapstoreProxyDomain, input.SnapstoreProxyId), scriptPath(disableHostServicesScript), - fmt.Sprintf("%s %q", scriptPath(installMicroK8sScript), installArgs), + fmt.Sprintf("%s %q %v", scriptPath(installMicroK8sScript), installArgs, input.DisableDefaultCNI), fmt.Sprintf("%s %q %q %q", scriptPath(configureContainerdProxyScript), input.ContainerdHTTPProxy, input.ContainerdHTTPSProxy, input.ContainerdNoProxy), scriptPath(configureKubeletScript), scriptPath(waitAPIServerScript), - ) - - if input.DisableDefaultCNI { - cloudConfig.RunCommands = append(cloudConfig.RunCommands, scriptPath(disableDefaultCNIScript)) - } - - cloudConfig.RunCommands = append(cloudConfig.RunCommands, "microk8s refresh-certs /var/tmp", fmt.Sprintf("%s %v", scriptPath(configureCalicoIPIPScript), input.IPinIP), fmt.Sprintf("%s %q", scriptPath(configureClusterAgentPortScript), input.ClusterAgentPort), diff --git a/controllers/cloudinit/controlplane_init_test.go b/controllers/cloudinit/controlplane_init_test.go index f1eabd7..67535f9 100644 --- a/controllers/cloudinit/controlplane_init_test.go +++ b/controllers/cloudinit/controlplane_init_test.go @@ -48,11 +48,10 @@ func TestControlPlaneInit(t *testing.T) { `/capi-scripts/00-configure-snapstore-http-proxy.sh "" ""`, `/capi-scripts/00-configure-snapstore-proxy.sh "" ""`, `/capi-scripts/00-disable-host-services.sh`, - `/capi-scripts/00-install-microk8s.sh "--channel 1.25 --classic"`, + `/capi-scripts/00-install-microk8s.sh "--channel 1.25 --classic" true`, `/capi-scripts/10-configure-containerd-proxy.sh "" "" ""`, `/capi-scripts/10-configure-kubelet.sh`, `/capi-scripts/50-wait-apiserver.sh`, - `/capi-scripts/10-disable-default-cni.sh`, `microk8s refresh-certs /var/tmp`, `/capi-scripts/10-configure-calico-ipip.sh true`, `/capi-scripts/10-configure-cluster-agent-port.sh "30000"`, diff --git a/controllers/cloudinit/controlplane_join.go b/controllers/cloudinit/controlplane_join.go index b08e1cb..3dcd0c6 100644 --- a/controllers/cloudinit/controlplane_join.go +++ b/controllers/cloudinit/controlplane_join.go @@ -125,17 +125,10 @@ func NewJoinControlPlane(input *ControlPlaneJoinInput) (*CloudConfig, error) { fmt.Sprintf("%s %q %q", scriptPath(snapstoreHTTPProxyScript), input.SnapstoreHTTPProxy, input.SnapstoreHTTPSProxy), fmt.Sprintf("%s %q %q", scriptPath(snapstoreProxyScript), input.SnapstoreProxyDomain, input.SnapstoreProxyId), scriptPath(disableHostServicesScript), - fmt.Sprintf("%s %q", scriptPath(installMicroK8sScript), installArgs), + fmt.Sprintf("%s %q %v", scriptPath(installMicroK8sScript), installArgs, input.DisableDefaultCNI), fmt.Sprintf("%s %q %q %q", scriptPath(configureContainerdProxyScript), input.ContainerdHTTPProxy, input.ContainerdHTTPSProxy, input.ContainerdNoProxy), scriptPath(configureKubeletScript), scriptPath(waitAPIServerScript), - ) - - if input.DisableDefaultCNI { - cloudConfig.RunCommands = append(cloudConfig.RunCommands, scriptPath(disableDefaultCNIScript)) - } - - cloudConfig.RunCommands = append(cloudConfig.RunCommands, fmt.Sprintf("%s %v", scriptPath(configureCalicoIPIPScript), input.IPinIP), fmt.Sprintf("%s %q", scriptPath(configureClusterAgentPortScript), input.ClusterAgentPort), fmt.Sprintf("%s %q", scriptPath(configureDqlitePortScript), input.DqlitePort), diff --git a/controllers/cloudinit/controlplane_join_test.go b/controllers/cloudinit/controlplane_join_test.go index 826a43f..21ad7d5 100644 --- a/controllers/cloudinit/controlplane_join_test.go +++ b/controllers/cloudinit/controlplane_join_test.go @@ -46,11 +46,10 @@ func TestControlPlaneJoin(t *testing.T) { `/capi-scripts/00-configure-snapstore-http-proxy.sh "" ""`, `/capi-scripts/00-configure-snapstore-proxy.sh "" ""`, `/capi-scripts/00-disable-host-services.sh`, - `/capi-scripts/00-install-microk8s.sh "--channel 1.25 --classic"`, + `/capi-scripts/00-install-microk8s.sh "--channel 1.25 --classic" true`, `/capi-scripts/10-configure-containerd-proxy.sh "" "" ""`, `/capi-scripts/10-configure-kubelet.sh`, `/capi-scripts/50-wait-apiserver.sh`, - `/capi-scripts/10-disable-default-cni.sh`, `/capi-scripts/10-configure-calico-ipip.sh true`, `/capi-scripts/10-configure-cluster-agent-port.sh "30000"`, `/capi-scripts/10-configure-dqlite-port.sh "2379"`, diff --git a/controllers/cloudinit/embed.go b/controllers/cloudinit/embed.go index 449f041..39fa77e 100644 --- a/controllers/cloudinit/embed.go +++ b/controllers/cloudinit/embed.go @@ -43,9 +43,6 @@ const ( // disableHostServicesScript disables services like containerd or kubelet from the host OS image. disableHostServicesScript script = "00-disable-host-services.sh" - // disableDefaultCNIScript disables the default CNI plugin. - disableDefaultCNIScript script = "10-disable-default-cni.sh" - // installMicroK8sScript installs MicroK8s on the host. installMicroK8sScript script = "00-install-microk8s.sh" @@ -88,7 +85,6 @@ var allScripts = []script{ snapstoreHTTPProxyScript, disableHostServicesScript, installMicroK8sScript, - disableDefaultCNIScript, configureCertLB, configureAPIServerScript, configureCalicoIPIPScript, diff --git a/controllers/cloudinit/scripts/00-install-microk8s.sh b/controllers/cloudinit/scripts/00-install-microk8s.sh old mode 100644 new mode 100755 index 45b9c8e..578270d --- a/controllers/cloudinit/scripts/00-install-microk8s.sh +++ b/controllers/cloudinit/scripts/00-install-microk8s.sh @@ -1,7 +1,11 @@ #!/bin/bash -xe # Usage: -# $0 $microk8s_snap_args +# $0 $microk8s_snap_args $disable_default_cni +# +# Arguments: +# $microk8s_snap_args Arguments to pass to snap install. +# $disable_default_cni Boolean flag (true or false) to disable the default CNI. # # Assumptions: # - snapd is installed @@ -15,3 +19,7 @@ while ! snap install microk8s ${1}; do echo "Failed to install MicroK8s snap, will retry" sleep 5 done + +if [ "${2}" == "true" ]; then + mv /var/snap/microk8s/current/args/cni-network/cni.yaml /var/snap/microk8s/current/args/cni-network/cni.yaml.old +fi diff --git a/controllers/cloudinit/scripts/10-disable-default-cni.sh b/controllers/cloudinit/scripts/10-disable-default-cni.sh deleted file mode 100644 index 8d6be83..0000000 --- a/controllers/cloudinit/scripts/10-disable-default-cni.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash -xe - -CNI_YAML="/var/snap/microk8s/current/args/cni-network/cni.yaml" -CNI_DIR="/var/snap/microk8s/current/args/cni-network" - -if [ ! -f "${CNI_YAML}" ]; then - echo "will not disable default CNI, missing cni.yaml" - exit 0 -fi - -/capi-scripts/50-wait-apiserver.sh - -while ! microk8s kubectl get -f "${CNI_YAML}" > /dev/null; do - echo "Waiting for CNI objects to be created..." - sleep 5 -done - -microk8s kubectl delete -f "${CNI_YAML}" - -for file in "${CNI_DIR}"/*; do - mv "$file" "$file.old" -done diff --git a/controllers/cloudinit/worker_join.go b/controllers/cloudinit/worker_join.go index 10efcc9..515abb3 100644 --- a/controllers/cloudinit/worker_join.go +++ b/controllers/cloudinit/worker_join.go @@ -112,7 +112,7 @@ func NewJoinWorker(input *WorkerInput) (*CloudConfig, error) { fmt.Sprintf("%s %q %q", scriptPath(snapstoreHTTPProxyScript), input.SnapstoreHTTPProxy, input.SnapstoreHTTPSProxy), fmt.Sprintf("%s %q %q", scriptPath(snapstoreProxyScript), input.SnapstoreProxyDomain, input.SnapstoreProxyId), scriptPath(disableHostServicesScript), - fmt.Sprintf("%s %q", scriptPath(installMicroK8sScript), installArgs), + fmt.Sprintf("%s %q %v", scriptPath(installMicroK8sScript), installArgs, false), fmt.Sprintf("%s %q %q %q", scriptPath(configureContainerdProxyScript), input.ContainerdHTTPProxy, input.ContainerdHTTPSProxy, input.ContainerdNoProxy), scriptPath(configureKubeletScript), scriptPath(waitAPIServerScript), diff --git a/controllers/cloudinit/worker_join_test.go b/controllers/cloudinit/worker_join_test.go index bc57e86..43a2450 100644 --- a/controllers/cloudinit/worker_join_test.go +++ b/controllers/cloudinit/worker_join_test.go @@ -42,7 +42,7 @@ func TestWorkerJoin(t *testing.T) { `/capi-scripts/00-configure-snapstore-http-proxy.sh "" ""`, `/capi-scripts/00-configure-snapstore-proxy.sh "" ""`, `/capi-scripts/00-disable-host-services.sh`, - `/capi-scripts/00-install-microk8s.sh "--channel 1.24 --classic"`, + `/capi-scripts/00-install-microk8s.sh "--channel 1.24 --classic" false`, `/capi-scripts/10-configure-containerd-proxy.sh "" "" ""`, `/capi-scripts/10-configure-kubelet.sh`, `/capi-scripts/50-wait-apiserver.sh`,