Skip to content

Commit 300d90e

Browse files
authored
Create local kind cluster with specified name (#987)
* Create local kind cluster with specified name Improved `scripts/dev/setup_kind_cluster.sh`: * added switch: -n <cluster_name> to specify kind cluster's name * added switch: -e to export kind credentials after creating cluster Improved docs. * Update sonar commit hash in requirements.txt * Empty commit
1 parent 91aaeec commit 300d90e

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
lines changed

docs/build_operator_locally.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This document contains a quickstart guide to build and deploy the operator local
44

55

66
## Prerequisites
7-
This guide assumes that you have already installed the following tools:
7+
This guide assumes that you have already prepared [Python virtual env](contributing.md#python-environment) and installed the following tools:
88

99
* [Kind](https://kind.sigs.k8s.io/)
1010
* [Docker](https://www.docker.com/)
@@ -16,20 +16,22 @@ This guide assumes that you have already installed the following tools:
1616
1. Create a local kubernetes cluster and start a local registry by running
1717

1818
```sh
19-
./scripts/dev/setup_kind_cluster.sh
19+
./scripts/dev/setup_kind_cluster.sh -n test-cluster
2020
```
2121

22-
2. Set the kind kubernetes context if you have not already done so:
23-
```bash
24-
export KUBECONFIG=~/.kube/kind
22+
Alternatively create a local cluster and set current kubectl context to it.
23+
```sh
24+
./scripts/dev/setup_kind_cluster.sh -en test-cluster
2525
```
2626

27-
3. Run the following to get kind credentials:
27+
3. Run the following to get kind credentials and switch current context to the newly created cluster:
2828

2929
```sh
30-
kind export kubeconfig
31-
# check it worked by running:
32-
kubectl cluster-info --context kind-kind --kubeconfig $KUBECONFIG
30+
kind export kubeconfig --name test-cluster
31+
# should return kind-test-cluster
32+
kubectl config current-context
33+
# should have test-cluster-control-plane node listed
34+
kubectl get nodes
3335
```
3436

3537
4. Build and deploy the operator:
@@ -39,7 +41,6 @@ kubectl cluster-info --context kind-kind --kubeconfig $KUBECONFIG
3941
make all-images deploy
4042
```
4143

42-
4344
Note: this will build and push the operator at `repo_url/mongodb-kubernetes-operator`, where `repo_url` is extracted from the [dev config file](./contributing.md#developing-locally)
4445

4546
5. Change the `image` field in the [manager.yaml](../config/manager/manager.yaml) file to have the image you just built

docs/contributing.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ The build process consists of multiple Docker images being built. You need to sp
9090
where you want the locally built images to be pushed. The Docker registry needs to be
9191
accessible from your Kubernetes cluster.
9292

93+
### Local kind cluster
94+
For local testing you can use a [local Kind cluster](build_operator_locally.md#steps).
95+
9396
## Test Namespace
9497

9598
You can change the namespace used for tests, if you are using `Kind`, for
@@ -99,6 +102,7 @@ instance, you can leave this as `mongodb`.
99102

100103
The test runner is a Python script, in order to use it a virtualenv needs to be
101104
created.
105+
**Python 3.9 is not supported yet. Please use Python 3.8.**
102106

103107
### Pip
104108
```sh
@@ -200,15 +204,12 @@ replica_set_scale
200204
The tests should run individually using the runner like this:
201205

202206
```sh
203-
make e2e-k8s test=<test-name>
207+
make e2e-k8s test=replica_set
204208
```
205209

206210
This will run the `replica_set` E2E test which is a simple test which installs a
207211
MongoDB Replica Set and asserts that the deployed server can be connected to.
208212

209-
210-
211-
212213
### Run the test locally with go test & Telepresence
213214
```sh
214215
make e2e-telepresence test=<test-name>

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
git+https://github.com/mongodb/sonar@96d13ff76a29349d07d2d6baa95e057495f00adf
1+
git+https://github.com/mongodb/sonar@94ed8a31e3f28c1d8a4b2ce96a341662c3f74c62
22
github-action-templates==0.0.4
33
docker==4.3.1
44
kubernetes==11.0.0

scripts/dev/setup_kind_cluster.sh

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
#!/usr/bin/env bash
22
set -Eeou pipefail
33

4+
function usage() {
5+
echo "Deploy local registry and create kind cluster configured to use this registry. Local Docker registry is deployed at localhost:5000.
6+
7+
Usage:
8+
setup_kind_cluster.sh [-n <cluster_name>]
9+
setup_kind_cluster.sh [-h]
10+
setup_kind_cluster.sh [-n <cluster_name>] [-e]
11+
12+
Options:
13+
-n <cluster_name> (optional) Set kind cluster name to <cluster_name>. Creates kubeconfig in ~/.kube/<cluster_name>. The default name is 'kind' if not set.
14+
-e (optional) Export newly created kind cluster's credentials to ~/.kube/<cluster_name> and set current kubectl context.
15+
-h (optional) Shows this screen.
16+
"
17+
exit 0
18+
}
19+
20+
cluster_name=${CLUSTER_NAME:-"kind"}
21+
export_kubeconfig=0
22+
while getopts ':n:he' opt; do
23+
case $opt in
24+
(n) cluster_name=$OPTARG;;
25+
(e) export_kubeconfig=1;;
26+
(h) usage;;
27+
(*) usage;;
28+
esac
29+
done
30+
shift "$((OPTIND-1))"
31+
32+
kubeconfig_path="$HOME/.kube/${cluster_name}"
33+
434
# create the kind network early unless it already exists.
535
# it would normally be created automatically by kind but we
636
# need it earlier to get the IP address of our registry.
@@ -21,7 +51,7 @@ fi
2151
ip="$(docker inspect kind-registry -f '{{.NetworkSettings.Networks.kind.IPAddress}}')"
2252

2353
# create a cluster with the local registry enabled in containerd
24-
cat <<EOF | kind create cluster --kubeconfig ~/.kube/kind --config=-
54+
cat <<EOF | kind create cluster --name "${cluster_name}" --kubeconfig "${kubeconfig_path}" --config=-
2555
kind: Cluster
2656
apiVersion: kind.x-k8s.io/v1alpha4
2757
containerdConfigPatches:
@@ -32,7 +62,7 @@ EOF
3262

3363
# Document the local registry (from https://kind.sigs.k8s.io/docs/user/local-registry/)
3464
# https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry
35-
cat <<EOF | kubectl apply -f -
65+
cat <<EOF | kubectl apply --kubeconfig "${kubeconfig_path}" -f -
3666
apiVersion: v1
3767
kind: ConfigMap
3868
metadata:
@@ -43,3 +73,7 @@ data:
4373
host: "localhost:${reg_port}"
4474
help: "https://kind.sigs.k8s.io/docs/user/local-registry/"
4575
EOF
76+
77+
if [[ "${export_kubeconfig}" == "1" ]]; then
78+
kind export kubeconfig --name "${cluster_name}"
79+
fi

0 commit comments

Comments
 (0)