Skip to content

Commit 4c78045

Browse files
committed
Update kind example
* Use kind release from GitHub instead of 'go get sigs.k8s.io/kind' * Run ct container with '--network host' to avoid patching kubeconfig * Create multi-node cluster with one master and three worker nodes so pods with anti-affinity can be tested * Improve wait logic to cater for multiple nodes * Add log statements * Remote unnecessary 'k8s' remote to avoid confusion * Extract function for 'docker exec' calls Signed-off-by: Reinhard Nägele <[email protected]>
1 parent 868038e commit 4c78045

File tree

5 files changed

+92
-94
lines changed

5 files changed

+92
-94
lines changed

examples/kind/.circleci/config.yml

+10-25
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,32 @@
1-
version: 2
1+
version: 2.1
22
jobs:
33
lint-scripts:
44
docker:
55
- image: koalaman/shellcheck-alpine
66
steps:
77
- checkout
88
- run:
9-
name: lint
10-
command: |
11-
shellcheck -x test/e2e-kind.sh
9+
command: shellcheck -x test/e2e-kind.sh
10+
1211
lint-charts:
1312
docker:
1413
- image: quay.io/helmpack/chart-testing:v2.2.0
1514
steps:
1615
- checkout
1716
- run:
18-
name: lint
19-
command: |
20-
git remote add k8s https://github.com/your_git_repo/charts
21-
git fetch k8s master
22-
ct lint --config test/ct.yaml
17+
command: ct lint --config test/ct.yaml
18+
2319
install-charts:
2420
machine: true
25-
environment:
26-
CHART_TESTING_IMAGE: quay.io/helmpack/chart-testing
27-
CHART_TESTING_TAG: v2.2.0
28-
CHARTS_REPO: https://github.com/your_git_repo/charts
29-
K8S_VERSION: "v1.12.3"
3021
steps:
3122
- checkout
3223
- run:
33-
name: install
34-
command: |
35-
test/e2e-kind.sh
36-
no_output_timeout: 3600
24+
command: test/e2e-kind.sh
25+
3726
workflows:
3827
version: 2
39-
lint_and_install:
28+
untagged-build:
4029
jobs:
4130
- lint-scripts
42-
- lint-charts:
43-
requires:
44-
- lint-scripts
45-
- install-charts:
46-
requires:
47-
- lint-charts
31+
- lint-charts
32+
- install-charts

examples/kind/README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
# Chart testing example with CircleCi and kind - `k`ubernetes `in` `d`ocker
1+
# Chart testing example with CircleCi and kind - `K`ubernetes `in` `D`ocker
22

3-
This example shows how to lint and test charts using CircleCi and [kind](https://github.com/kubernetes-sigs/kind).
3+
`kind` is a tool for running local Kubernetes clusters using Docker container "nodes".
44

5-
`kind` is a tool for running local/CI pipelines Kubernetes clusters using Docker container "nodes".
5+
This example shows how to lint and test charts using CircleCi and [kind](https://github.com/kubernetes-sigs/kind).
6+
It creates a cluster with a single master node and three worker nodes.
7+
The cluster configuration can be adjusted in [kind-config.yaml](test/kind-config.yaml).

examples/kind/test/ct.yaml

-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
remote: k8s
2-
target-branch: master
3-
chart-dirs:
4-
- stable
5-
excluded-charts:
6-
- common
71
helm-extra-args: --timeout 800

examples/kind/test/e2e-kind.sh

+71-60
Original file line numberDiff line numberDiff line change
@@ -4,93 +4,104 @@ set -o errexit
44
set -o nounset
55
set -o pipefail
66

7-
readonly REPO_ROOT="${REPO_ROOT:-$(git rev-parse --show-toplevel)}"
7+
readonly CT_VERSION=v2.2.0
8+
readonly KIND_VERSION=0.1.0
9+
readonly CLUSTER_NAME=chart-testing
10+
readonly K8S_VERSION=v1.13.2
11+
12+
run_ct_container() {
13+
echo 'Running ct container...'
14+
docker run --rm --interactive --detach --network host --name ct \
15+
--volume "$(pwd)/test/ct.yaml:/etc/ct/ct.yaml" \
16+
--volume "$(pwd):/workdir" \
17+
--workdir /workdir \
18+
"quay.io/helmpack/chart-testing:$CT_VERSION" \
19+
cat
20+
echo
21+
}
22+
23+
cleanup() {
24+
echo 'Removing ct container...'
25+
docker kill ct > /dev/null 2>&1
26+
27+
echo 'Done!'
28+
}
29+
30+
docker_exec() {
31+
docker exec --interactive ct "$@"
32+
}
33+
34+
create_kind_cluster() {
35+
echo 'Installing kind...'
36+
37+
curl -sSLo kind "https://github.com/kubernetes-sigs/kind/releases/download/$KIND_VERSION/kind-linux-amd64"
38+
chmod +x kind
39+
sudo mv kind /usr/local/bin/kind
840

9-
run_kind() {
41+
kind create cluster --name "$CLUSTER_NAME" --config test/kind-config.yaml --image "kindest/node:$K8S_VERSION"
1042

11-
echo "Download kind binary..."
12-
docker run --rm -it -v "$(pwd)":/go/bin golang go get sigs.k8s.io/kind && sudo mv kind /usr/local/bin/
43+
docker_exec mkdir -p /root/.kube
1344

14-
echo "Download kubectl..."
15-
curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/"${K8S_VERSION}"/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/
45+
echo 'Copying kubeconfig to container...'
46+
local kubeconfig
47+
kubeconfig="$(kind get kubeconfig-path --name "$CLUSTER_NAME")"
48+
docker cp "$kubeconfig" ct:/root/.kube/config
49+
50+
docker_exec kubectl cluster-info
1651
echo
1752

18-
echo "Create Kubernetes cluster with kind..."
19-
kind create cluster --image=kindest/node:"$K8S_VERSION"
53+
echo -n 'Waiting for cluster to be ready...'
54+
until ! grep --quiet 'NotReady' <(docker_exec kubectl get nodes --no-headers); do
55+
printf '.'
56+
sleep 1
57+
done
2058

21-
echo "Export kubeconfig..."
22-
# shellcheck disable=SC2155
23-
export KUBECONFIG="$(kind get kubeconfig-path)"
59+
echo '✔︎'
2460
echo
2561

26-
echo "Ensure the apiserver is responding..."
27-
kubectl cluster-info
62+
docker_exec kubectl get nodes
2863
echo
2964

30-
echo "Wait for Kubernetes to be up and ready..."
31-
JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}'; until kubectl get nodes -o jsonpath="$JSONPATH" 2>&1 | grep -q "Ready=True"; do sleep 1; done
65+
echo 'Cluster ready!'
3266
echo
3367
}
3468

3569
install_tiller() {
36-
# Install Tiller with RBAC
37-
kubectl -n kube-system create sa tiller
38-
kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
39-
docker exec "$config_container_id" helm init --service-account tiller
40-
echo "Wait for Tiller to be up and ready..."
41-
until kubectl -n kube-system get pods 2>&1 | grep -w "tiller-deploy" | grep -w "1/1"; do sleep 1; done
70+
echo 'Installing Tiller...'
71+
docker_exec kubectl --namespace kube-system create sa tiller
72+
docker_exec kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
73+
docker_exec helm init --service-account tiller --upgrade --wait
4274
echo
4375
}
4476

4577
install_hostpath-provisioner() {
46-
# kind doesn't support Dynamic PVC provisioning yet, this one of ways to get it working
47-
# https://github.com/rimusz/charts/tree/master/stable/hostpath-provisioner
78+
# kind doesn't support Dynamic PVC provisioning yet, this is one way to get it working
79+
# https://github.com/rimusz/charts/tree/master/stable/hostpath-provisioner
4880

49-
# delete the default storage class
50-
kubectl delete storageclass standard
81+
echo 'Installing hostpath-provisioner...'
5182

52-
echo "Install Hostpath Provisioner..."
53-
docker exec "$config_container_id" helm repo add rimusz https://charts.rimusz.net
54-
docker exec "$config_container_id" helm repo update
55-
docker exec "$config_container_id" helm upgrade --install hostpath-provisioner --namespace kube-system rimusz/hostpath-provisioner
56-
echo
57-
}
83+
# Remove default storage class. Will be recreated by hostpath -provisioner
84+
docker_exec kubectl delete storageclass standard
5885

59-
main() {
60-
61-
echo "Starting kind ..."
86+
docker_exec helm repo add rimusz https://charts.rimusz.net
87+
docker_exec helm repo update
88+
docker_exec helm install rimusz/hostpath-provisioner --name hostpath-provisioner --namespace kube-system --wait
6289
echo
63-
run_kind
64-
65-
local config_container_id
66-
config_container_id=$(docker run -it -d -v "$REPO_ROOT:/workdir" --workdir /workdir "$CHART_TESTING_IMAGE:$CHART_TESTING_TAG" cat)
67-
68-
# shellcheck disable=SC2064
69-
trap "docker rm -f $config_container_id > /dev/null" EXIT
70-
71-
# Get kind container IP
72-
kind_container_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' kind-1-control-plane)
73-
# Copy kubeconfig file
74-
docker exec "$config_container_id" mkdir /root/.kube
75-
docker cp "$KUBECONFIG" "$config_container_id:/root/.kube/config"
76-
# Update in kubeconfig from localhost to kind container IP
77-
docker exec "$config_container_id" sed -i "s/localhost:.*/$kind_container_ip:6443/g" /root/.kube/config
90+
}
7891

79-
echo "Add git remote k8s ${CHARTS_REPO}"
80-
git remote add k8s "${CHARTS_REPO}" &> /dev/null || true
81-
git fetch k8s master
92+
install_charts() {
93+
docker_exec ct install
8294
echo
95+
}
8396

84-
# Install Tiller with RBAC
85-
install_tiller
97+
main() {
98+
run_ct_container
99+
trap cleanup EXIT
86100

87-
# Install hostpath-provisioner for Dynammic PVC provisioning
101+
create_kind_cluster
102+
install_tiller
88103
install_hostpath-provisioner
89-
90-
# shellcheck disable=SC2086
91-
docker exec "$config_container_id" ct install --config /workdir/test/ct.yaml
92-
93-
echo "Done Testing!"
104+
install_charts
94105
}
95106

96107
main

examples/kind/test/kind-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Config
2+
apiVersion: kind.sigs.k8s.io/v1alpha2
3+
nodes:
4+
- role: control-plane
5+
- role: worker
6+
replicas: 3

0 commit comments

Comments
 (0)