Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
eaudetcobello committed Jul 30, 2024
1 parent 4ab5950 commit 037caa1
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 35 deletions.
6 changes: 3 additions & 3 deletions controllers/cloudinit/cloudinit_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestCloudConfigInput(t *testing.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" "--disable-default-cni"`))
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())
})
Expand Down
5 changes: 1 addition & 4 deletions controllers/cloudinit/controlplane_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ func NewInitControlPlane(input *ControlPlaneInitInput) (*CloudConfig, error) {
}
installArgs := createInstallArgs(input.Confinement, input.RiskLevel, kubernetesVersion)

// extra arguments passed to 00-install-microk8s.sh
extraArgs := createExtraArgs(input.DisableDefaultCNI)

cloudConfig := NewBaseCloudConfig()
cloudConfig.WriteFiles = append(
cloudConfig.WriteFiles,
Expand All @@ -146,7 +143,7 @@ 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 %q", scriptPath(installMicroK8sScript), installArgs, extraArgs),
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),
Expand Down
2 changes: 1 addition & 1 deletion controllers/cloudinit/controlplane_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ 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" "--disable-default-cni"`,
`/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`,
Expand Down
5 changes: 1 addition & 4 deletions controllers/cloudinit/controlplane_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ func NewJoinControlPlane(input *ControlPlaneJoinInput) (*CloudConfig, error) {
}
installArgs := createInstallArgs(input.Confinement, input.RiskLevel, kubernetesVersion)

// extra arguments passed to 00-install-microk8s.sh
extraArgs := createExtraArgs(input.DisableDefaultCNI)

cloudConfig := NewBaseCloudConfig()
cloudConfig.WriteFiles = append(cloudConfig.WriteFiles, input.ExtraWriteFiles...)
if args := input.ExtraKubeletArgs; len(args) > 0 {
Expand All @@ -128,7 +125,7 @@ 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 %q", scriptPath(installMicroK8sScript), installArgs, extraArgs),
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),
Expand Down
2 changes: 1 addition & 1 deletion controllers/cloudinit/controlplane_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ 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" "--disable-default-cni"`,
`/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`,
Expand Down
18 changes: 7 additions & 11 deletions controllers/cloudinit/scripts/00-install-microk8s.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
#!/bin/bash -xe

# Usage:
# $0 $microk8s_snap_args [--disable-default-cni]
# $0 $microk8s_snap_args $disable_default_cni
#
# Arguments:
# $microk8s_snap_args Arguments to pass to snap install
# $disable_default_cni Boolean flag to disable the default CNI
#
# Assumptions:
# - snapd is installed

DISABLE_DEFAULT_CNI=false

for arg in "$@"; do
if [ "$arg" == "--disable-default-cni" ]; then
DISABLE_DEFAULT_CNI=true
fi
done

if snap list microk8s; then
echo "MicroK8s is already installed, will not install"
exit 0
Expand All @@ -24,6 +20,6 @@ while ! snap install microk8s ${1}; do
sleep 5
done

if [ "$DISABLE_DEFAULT_CNI" == "true" ]; then
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
fi
9 changes: 0 additions & 9 deletions controllers/cloudinit/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ func createInstallArgs(confinement string, riskLevel string, kubernetesVersion *
return installArgs
}

func createExtraArgs(disableDefaultCNI bool) string {
var extraArgs string
if disableDefaultCNI {
extraArgs = "--disable-default-cni"
}

return extraArgs
}

func WriteFilesFromAPI(files []bootstrapclusterxk8siov1beta1.CloudInitWriteFile) []File {
if len(files) == 0 {
return nil
Expand Down
2 changes: 1 addition & 1 deletion controllers/cloudinit/worker_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 %q", scriptPath(installMicroK8sScript), installArgs, ""), // no extra args
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),
Expand Down
2 changes: 1 addition & 1 deletion controllers/cloudinit/worker_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down

0 comments on commit 037caa1

Please sign in to comment.