Skip to content

Commit

Permalink
add new disableDefaultCNI field
Browse files Browse the repository at this point in the history
  • Loading branch information
eaudetcobello committed Jul 26, 2024
1 parent 8295d78 commit 1f381fe
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 2 deletions.
4 changes: 4 additions & 0 deletions apis/v1beta1/microk8sconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ type InitConfiguration struct {
// +kubebuilder:default:=stable
RiskLevel string `json:"riskLevel,omitempty"`

// Whether or not to use the default CNI
// +optional
DisableDefaultCNI bool `json:"useDefaultCNI,omitempty"`

// The snap store proxy domain
// +optional
SnapstoreProxyDomain string `json:"snapstoreProxyDomain,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ spec:
snapstoreProxyId:
description: The snap store proxy ID
type: string
useDefaultCNI:
description: Whether or not to use the default CNI
type: boolean
type: object
type: object
status:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ spec:
snapstoreProxyId:
description: The snap store proxy ID
type: string
useDefaultCNI:
description: Whether or not to use the default CNI
type: boolean
type: object
type: object
type: object
Expand Down
76 changes: 76 additions & 0 deletions controllers/cloudinit/cloudinit_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,80 @@ 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/00-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/00-disable-default-cni.sh`))
})
}
})
}
10 changes: 9 additions & 1 deletion controllers/cloudinit/controlplane_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type ControlPlaneInitInput struct {
Confinement string
// RiskLevel specifies the risk level (strict, candidate, beta, edge) for the snap channels.
RiskLevel string
// DisableDefaultCNI specifies whether to disable the default CNI plugin.
DisableDefaultCNI bool
// SnapstoreProxyDomain specifies the domain of the snapstore proxy if one is to be used.
SnapstoreProxyDomain string
// SnapstoreProxyId specifies the snapstore proxy ID if one is to be used.
Expand Down Expand Up @@ -141,7 +143,13 @@ 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", scriptPath(installMicroK8sScript), installArgs))

if input.DisableDefaultCNI {
cloudConfig.RunCommands = append(cloudConfig.RunCommands, scriptPath(disableDefaultCNIScript))
}

cloudConfig.RunCommands = append(cloudConfig.RunCommands,
fmt.Sprintf("%s %q %q %q", scriptPath(configureContainerdProxyScript), input.ContainerdHTTPProxy, input.ContainerdHTTPSProxy, input.ContainerdNoProxy),
scriptPath(configureKubeletScript),
scriptPath(waitAPIServerScript),
Expand Down
2 changes: 2 additions & 0 deletions controllers/cloudinit/controlplane_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestControlPlaneInit(t *testing.T) {
IPinIP: true,
Token: strings.Repeat("a", 32),
TokenTTL: 10000,
DisableDefaultCNI: true,
Confinement: "classic",
})
g.Expect(err).NotTo(HaveOccurred())
Expand All @@ -48,6 +49,7 @@ func TestControlPlaneInit(t *testing.T) {
`/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-disable-default-cni.sh`,
`/capi-scripts/10-configure-containerd-proxy.sh "" "" ""`,
`/capi-scripts/10-configure-kubelet.sh`,
`/capi-scripts/50-wait-apiserver.sh`,
Expand Down
10 changes: 9 additions & 1 deletion controllers/cloudinit/controlplane_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type ControlPlaneJoinInput struct {
Confinement string
// RiskLevel specifies the risk level (strict, candidate, beta, edge) for the snap channels.
RiskLevel string
// DisableDefaultCNI specifies whether to use the default CNI plugin.
DisableDefaultCNI bool
// SnapstoreProxyDomain specifies the domain of the snapstore proxy if one is to be used.
SnapstoreProxyDomain string
// SnapstoreProxyId specifies the snapstore proxy ID if one is to be used.
Expand Down Expand Up @@ -123,7 +125,13 @@ 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", scriptPath(installMicroK8sScript), installArgs))

if input.DisableDefaultCNI {
cloudConfig.RunCommands = append(cloudConfig.RunCommands, scriptPath(disableDefaultCNIScript))
}

cloudConfig.RunCommands = append(cloudConfig.RunCommands,
fmt.Sprintf("%s %q %q %q", scriptPath(configureContainerdProxyScript), input.ContainerdHTTPProxy, input.ContainerdHTTPSProxy, input.ContainerdNoProxy),
scriptPath(configureKubeletScript),
scriptPath(waitAPIServerScript),
Expand Down
3 changes: 3 additions & 0 deletions controllers/cloudinit/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ 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 = "00-disable-default-cni.sh"

// installMicroK8sScript installs MicroK8s on the host.
installMicroK8sScript script = "00-install-microk8s.sh"

Expand Down
4 changes: 4 additions & 0 deletions controllers/cloudinit/scripts/00-disable-default-cni.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash -xe

microk8s kubectl delete -f /var/snap/microk8s/current/args/cni-network/cni.yaml
mv /var/snap/microk8s/current/args/cni-network/cni.yaml /var/snap/microk8s/current/args/cni-network/calico.yaml.old
2 changes: 2 additions & 0 deletions controllers/microk8sconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ func (r *MicroK8sConfigReconciler) handleClusterNotInitialized(ctx context.Conte
SnapstoreProxyId: microk8sConfig.Spec.InitConfiguration.SnapstoreProxyId,
Confinement: microk8sConfig.Spec.InitConfiguration.Confinement,
RiskLevel: microk8sConfig.Spec.InitConfiguration.RiskLevel,
DisableDefaultCNI: microk8sConfig.Spec.InitConfiguration.DisableDefaultCNI,
ExtraWriteFiles: cloudinit.WriteFilesFromAPI(microk8sConfig.Spec.InitConfiguration.ExtraWriteFiles),
ExtraKubeletArgs: microk8sConfig.Spec.InitConfiguration.ExtraKubeletArgs,
SnapstoreHTTPProxy: microk8sConfig.Spec.InitConfiguration.SnapstoreHTTPProxy,
Expand Down Expand Up @@ -416,6 +417,7 @@ func (r *MicroK8sConfigReconciler) handleJoiningControlPlaneNode(ctx context.Con
SnapstoreProxyDomain: microk8sConfig.Spec.InitConfiguration.SnapstoreProxyDomain,
SnapstoreProxyId: microk8sConfig.Spec.InitConfiguration.SnapstoreProxyId,
RiskLevel: microk8sConfig.Spec.InitConfiguration.RiskLevel,
DisableDefaultCNI: microk8sConfig.Spec.InitConfiguration.DisableDefaultCNI,
Confinement: microk8sConfig.Spec.InitConfiguration.Confinement,
ExtraWriteFiles: cloudinit.WriteFilesFromAPI(microk8sConfig.Spec.InitConfiguration.ExtraWriteFiles),
ExtraKubeletArgs: microk8sConfig.Spec.InitConfiguration.ExtraKubeletArgs,
Expand Down

0 comments on commit 1f381fe

Please sign in to comment.