Skip to content

Commit a05ba9c

Browse files
author
Maciej Golaszewski
committed
Add CI test for disabling the default CNI in Microk8s CAPI provider
KU-1226
1 parent 7f6bc64 commit a05ba9c

File tree

4 files changed

+97
-40
lines changed

4 files changed

+97
-40
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ This project offers a cluster API bootstrap provider controller that manages the
99
### Prerequisites
1010

1111
* Install clusterctl following the [upstream instructions](https://cluster-api.sigs.k8s.io/user/quick-start.html#install-clusterctl)
12-
```
12+
```bash
1313
curl -L https://github.com/kubernetes-sigs/cluster-api/releases/download/v1.1.3/clusterctl-linux-amd64 -o clusterctl
1414
```
1515

1616
* Install a bootstrap Kubernetes cluster. To use MicroK8s as a bootstrap cluster:
17-
```
17+
```bash
1818
sudo snap install microk8s --classic
1919
sudo microk8s.config > ~/.kube/config
2020
sudo microk8s enable dns
@@ -24,7 +24,7 @@ sudo microk8s enable dns
2424

2525
To to configure clusterctl with the two MicroK8s providers edit `~/.cluster-api/clusterctl.yaml`
2626
and add the following:
27-
```
27+
```yaml
2828
providers:
2929
- name: "microk8s"
3030
url: "https://github.com/canonical/cluster-api-bootstrap-provider-microk8s/releases/latest/bootstrap-components.yaml"
@@ -44,21 +44,21 @@ Alternatively, you can build the providers manually as described in the followin
4444

4545

4646

47-
### Building from source
47+
### Building from sourcebssdfsdfsdfsdfsdfsdsdfsdfsfdsfdsfsdfsdfsd
4848

4949
* Install the cluster provider of your choice. Have a look at the [cluster API book](https://cluster-api.sigs.k8s.io/user/quick-start.html#initialization-for-common-providers) for your options at this step. You should deploy only the infrastructure controller leaving the bootstrap and control plane ones empty. For example assuming we want to provision a MicroK8s cluster on AWS:
50-
```
50+
```bash
5151
clusterctl init --infrastructure aws --bootstrap "-" --control-plane "-"
5252
```
5353

5454
* Clone the two cluster API MicroK8s specific repositories and start the controllers on two separate terminals:
55-
```
55+
```bash
5656
cd $GOPATH/src/github.com/canonical/cluster-api-bootstrap-provider-microk8s/
5757
make install
5858
make run
5959
```
6060
And:
61-
```
61+
```bash
6262
cd $GOPATH/src/github.com/canonical/cluster-api-control-plane-provider-microk8s/
6363
make install
6464
make run

integration/README.md

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,43 @@
44

55
The integration/e2e tests have the following prerequisites:
66

7-
* an environment variable `CLUSTER_MANIFEST_FILE` pointing to the cluster manifest. Cluster manifests can be produced with the help of the templates found under `templates`. For example:
7+
* an environment variable `CLUSTER_MANIFEST_FILE` pointing to the cluster manifest. Cluster manifests can be produced with the help of the templates found under [`templates`](../templates). For example:
8+
```bash
9+
export AWS_REGION=us-east-1
10+
export AWS_SSH_KEY_NAME=capi
11+
export CONTROL_PLANE_MACHINE_COUNT=3
12+
export WORKER_MACHINE_COUNT=3
13+
export AWS_CREATE_BASTION=false
14+
export AWS_PUBLIC_IP=false
15+
export AWS_CONTROL_PLANE_MACHINE_FLAVOR=t3.large
16+
export AWS_NODE_MACHINE_FLAVOR=t3.large
17+
export CLUSTER_NAME=test-ci-cluster
18+
clusterctl generate cluster ${CLUSTER_NAME} --from "templates/cluster-template-aws.yaml" --kubernetes-version 1.27.0 > cluster.yaml
19+
export CLUSTER_MANIFEST_FILE=$PWD/cluster.yaml
820
```
9-
export AWS_REGION=us-east-1
10-
export AWS_SSH_KEY_NAME=capi
11-
export CONTROL_PLANE_MACHINE_COUNT=3
12-
export WORKER_MACHINE_COUNT=3
13-
export AWS_CREATE_BASTION=false
14-
export AWS_PUBLIC_IP=false
15-
export AWS_CONTROL_PLANE_MACHINE_FLAVOR=t3.large
16-
export AWS_NODE_MACHINE_FLAVOR=t3.large
17-
export CLUSTER_NAME=test-ci-cluster
18-
clusterctl generate cluster ${CLUSTER_NAME} --from "templates/cluster-template-aws.yaml" --kubernetes-version 1.25.0 > cluster.yaml
19-
export CLUSTER_MANIFEST_FILE=$PWD/cluster.yaml
20-
```
21-
21+
> NOTE: AWS_SSH_KEY_NAME is name of ssh key in AWS that you are plan to use, if you don't have one yet refer
22+
> to capi on [aws prerequisites documentation](https://cluster-api-aws.sigs.k8s.io/topics/using-clusterawsadm-to-fulfill-prerequisites#ssh-key-pair)
2223
* Additional environment variables when testing cluster upgrades:
23-
```
24-
export CAPI_UPGRADE_VERSION=v1.26.0
25-
export CAPI_UPGRADE_MD_NAME=${CLUSTER_NAME}-md-0
26-
export CAPI_UPGRADE_MD_TYPE=machinedeployments.cluster.x-k8s.io
27-
export CAPI_UPGRADE_CP_NAME=${CLUSTER_NAME}-control-plane
28-
export CAPI_UPGRADE_CP_TYPE=microk8scontrolplanes.controlplane.cluster.x-k8s.io
29-
30-
# Change the control plane and worker machine count to desired values for in-place upgrades tests and create a new cluster manifest.
31-
CONTROL_PLANE_MACHINE_COUNT=1
32-
WORKER_MACHINE_COUNT=1
33-
clusterctl generate cluster ${CLUSTER_NAME} --from "templates/cluster-template-aws.yaml" --kubernetes-version 1.25.0 > cluster-inplace.yaml
34-
export CLUSTER_INPLACE_MANIFEST_FILE=$PWD/cluster-inplace.yaml
24+
```bash
25+
export CAPI_UPGRADE_VERSION=v1.28.0
26+
export CAPI_UPGRADE_MD_NAME=${CLUSTER_NAME}-md-0
27+
export CAPI_UPGRADE_MD_TYPE=machinedeployments.cluster.x-k8s.io
28+
export CAPI_UPGRADE_CP_NAME=${CLUSTER_NAME}-control-plane
29+
export CAPI_UPGRADE_CP_TYPE=microk8scontrolplanes.controlplane.cluster.x-k8s.io
30+
# Change the control plane and worker machine count to desired values for in-place upgrades tests and create a new cluster manifest.
31+
CONTROL_PLANE_MACHINE_COUNT=1
32+
WORKER_MACHINE_COUNT=1
33+
clusterctl generate cluster ${CLUSTER_NAME} --from "templates/cluster-template-aws.yaml" --kubernetes-version 1.27.0 > cluster-inplace.yaml
34+
export CLUSTER_INPLACE_MANIFEST_FILE=$PWD/cluster-inplace.yaml
35+
```
3536

36-
```
37+
* Additional environment variables when testing disable default CNI flag:
38+
```bash
39+
export DISABLE_DEFAULT_CNI=true
40+
export POST_RUN_COMMANDS='["helm install cilium cilium/cilium --namespace kube-system --set cni.confPath=/var/snap/microk8s/current/args/cni-network --set cni.binPath=/var/snap/microk8s/current/opt/cni/bin --set daemon.runPath=/var/snap/microk8s/current/var/run/cilium --set operator.replicas=1 --set ipam.operator.clusterPoolIPv4PodCIDRList=\"10.1.0.0/16\" --set nodePort.enabled=true"]' # install Calico in place default CNI
41+
clusterctl generate cluster ${CLUSTER_NAME} --from "templates/cluster-template-aws.yaml" --kubernetes-version 1.27.0 > cluster_disable_default_cni.yaml
42+
export CLUSTER_DISABLE_DEFAULT_CNI_MANIFEST_FILE=$PWD/cluster_disable_default_cni.yaml
43+
```
3744

3845
* `clusterctl` available in the PATH
3946

@@ -67,10 +74,10 @@ microk8s config > ~/.kube/config
6774

6875
#### Initialize infrastructure provider
6976

70-
Visit [here](https://cluster-api.sigs.k8s.io/user/quick-start.html#initialization-for-common-providers) for a list of common infrasturture providers.
77+
Visit [here](https://cluster-api.sigs.k8s.io/user/quick-start.html#initialization-for-common-providers) for a list of common infrastructure providers.
7178

7279
```bash
73-
clusterctl init --infrastructure <infra> --bootstrap - --control-plane -
80+
clusterctl init --infrastructure <infra> --bootstrap - --control-plane -
7481
```
7582

7683
#### Build Docker images and release manifests from the checked out source code
@@ -83,7 +90,7 @@ docker push <username>/capi-bootstrap-provider-microk8s:<tag>
8390
sed "s,docker.io/cdkbot/capi-bootstrap-provider-microk8s:latest,docker.io/<username>/capi-bootstrap-provider-microk8s:<tag>," -i bootstrap-components.yaml
8491
```
8592

86-
Similarly for control-plane provider
93+
Similarly, for control-plane provider
8794
```bash
8895
cd control-plane
8996
docker build -t <username>/capi-control-plane-provider-microk8s:<tag> .

integration/e2e_test.go

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func init() {
4545

4646
// TestBasic waits for the target cluster to deploy and start a 30 pod deployment.
4747
// The CLUSTER_MANIFEST_FILE environment variable should point to a manifest with the target cluster
48-
// kubectl and clusterctl have to be avaibale in the caller's path.
48+
// kubectl and clusterctl have to be available in the caller's path.
4949
// kubectl should be setup so it uses the kubeconfig of the management cluster by default.
5050
func TestBasic(t *testing.T) {
5151
cluster_manifest_file := os.Getenv("CLUSTER_MANIFEST_FILE")
@@ -88,7 +88,29 @@ func TestInPlaceUpgrade(t *testing.T) {
8888
// Important: the cluster is deleted in the Cleanup function
8989
// which is called after all subtests are finished.
9090
t.Logf("Deleting the cluster")
91+
}
92+
93+
// TestDisableDefaultCNI deploys cluster disabled defalut CNI .
94+
// The CLUSTER_DISABLE_DEFAULT_CNI_MANIFEST_FILE environment variable should point to a manifest with the target cluster
95+
// With post actions calico will be installed.
96+
func TestDisableDefaultCNI(t *testing.T) {
97+
cluster_manifest_file := os.Getenv("CLUSTER_DISABLE_DEFAULT_CNI_MANIFEST_FILE")
98+
if cluster_manifest_file == "" {
99+
t.Fatalf("Environment variable CLUSTER_DISABLE_DEFAULT_CNI_MANIFEST_FILE is not set. " +
100+
"CLUSTER_DISABLE_DEFAULT_CNI_MANIFEST_FILE is expected to hold the PATH to a cluster manifest.")
101+
}
102+
t.Logf("Cluster to setup is in %s", cluster_manifest_file)
91103

104+
setupCheck(t)
105+
t.Cleanup(teardownCluster)
106+
107+
t.Run("DeployCluster", func(t *testing.T) { deployCluster(t, os.Getenv("CLUSTER_DISABLE_DEFAULT_CNI_MANIFEST_FILE")) })
108+
t.Run("ValidateCalico", func(t *testing.T) { validateCalico(t) })
109+
t.Run("DeployMicrobot", func(t *testing.T) { deployMicrobot(t) })
110+
t.Run("UpgradeClusterRollout", func(t *testing.T) { upgradeCluster(t, "RollingUpgrade") })
111+
// Important: the cluster is deleted in the Cleanup function
112+
// which is called after all subtests are finished.
113+
t.Logf("Deleting the cluster")
92114
}
93115

94116
// setupCheck checks that the environment is ready to run the tests.
@@ -149,7 +171,7 @@ func teardownCluster() {
149171

150172
// deployCluster deploys a cluster using the manifest in CLUSTER_MANIFEST_FILE.
151173
func deployCluster(t testing.TB, cluster_manifest_file string) {
152-
t.Log("Setting up the cluster")
174+
t.Logf("Setting up the cluster using %s", cluster_manifest_file)
153175
command := []string{"kubectl", "apply", "-f", cluster_manifest_file}
154176
cmd := exec.Command(command[0], command[1:]...)
155177
outputBytes, err := cmd.CombinedOutput()
@@ -182,7 +204,7 @@ func deployCluster(t testing.TB, cluster_manifest_file string) {
182204
t.Fatal(err)
183205
} else {
184206
attempt++
185-
t.Log("Failed to get the target's kubeconfig, retrying.")
207+
t.Logf("Failed to get the target's kubeconfig for %s, retrying.", cluster)
186208
time.Sleep(20 * time.Second)
187209
}
188210
} else {
@@ -307,6 +329,32 @@ func deployMicrobot(t testing.TB) {
307329
command = []string{"kubectl", "--kubeconfig=" + KUBECONFIG, "wait", "deploy/bot", "--for=jsonpath={.status.readyReplicas}=30"}
308330
for {
309331
cmd = exec.Command(command[0], command[1:]...)
332+
outputBytes, err = cmd.CombinedOutput()
333+
if err != nil {
334+
t.Log(string(outputBytes))
335+
if attempt >= maxAttempts {
336+
t.Fatal(err)
337+
} else {
338+
attempt++
339+
t.Log("Retrying")
340+
time.Sleep(10 * time.Second)
341+
}
342+
} else {
343+
break
344+
}
345+
}
346+
}
347+
348+
// validateCalico checks a deployment of calico demonset.
349+
func validateCalico(t testing.TB) {
350+
t.Log("Validate Calico")
351+
// Make sure we have as many nodes as machines
352+
attempt := 0
353+
maxAttempts := 60
354+
t.Log("Waiting for the deployment to complete")
355+
command := []string{"kubectl", "--kubeconfig=" + KUBECONFIG, "-n", "kube-system", "wait", "ds/calico-node", "--for=jsonpath={.status.numberAvailable}=6"}
356+
for {
357+
cmd := exec.Command(command[0], command[1:]...)
310358
outputBytes, err := cmd.CombinedOutput()
311359
if err != nil {
312360
t.Log(string(outputBytes))

templates/cluster-template-aws.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,5 @@ spec:
108108
initConfiguration:
109109
riskLevel: "${SNAP_RISKLEVEL:=}"
110110
confinement: "${SNAP_CONFINEMENT:=}"
111+
disableDefaultCNI: ${DISABLE_DEFAULT_CNI:=false}
112+
postRunCommands: ${POST_RUN_COMMANDS:=[]}

0 commit comments

Comments
 (0)