Skip to content

Commit

Permalink
simplify impl. of disabledefaultcni
Browse files Browse the repository at this point in the history
  • Loading branch information
eaudetcobello committed Jul 31, 2024
1 parent 21373e2 commit 869ed51
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 127 deletions.
122 changes: 44 additions & 78 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 All @@ -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",
Expand Down Expand Up @@ -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`))
})
}
})
}
9 changes: 1 addition & 8 deletions controllers/cloudinit/controlplane_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 1 addition & 2 deletions controllers/cloudinit/controlplane_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`,
Expand Down
9 changes: 1 addition & 8 deletions controllers/cloudinit/controlplane_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 1 addition & 2 deletions controllers/cloudinit/controlplane_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`,
Expand Down
4 changes: 0 additions & 4 deletions controllers/cloudinit/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -88,7 +85,6 @@ var allScripts = []script{
snapstoreHTTPProxyScript,
disableHostServicesScript,
installMicroK8sScript,
disableDefaultCNIScript,
configureCertLB,
configureAPIServerScript,
configureCalicoIPIPScript,
Expand Down
10 changes: 9 additions & 1 deletion controllers/cloudinit/scripts/00-install-microk8s.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
22 changes: 0 additions & 22 deletions controllers/cloudinit/scripts/10-disable-default-cni.sh

This file was deleted.

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", 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),
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 869ed51

Please sign in to comment.