diff --git a/examples/kind/.circleci/config.yml b/examples/kind/.circleci/config.yml index db723ac..a217835 100644 --- a/examples/kind/.circleci/config.yml +++ b/examples/kind/.circleci/config.yml @@ -1,4 +1,4 @@ -version: 2 +version: 2.1 jobs: lint-scripts: docker: @@ -6,42 +6,27 @@ jobs: steps: - checkout - run: - name: lint - command: | - shellcheck -x test/e2e-kind.sh + command: shellcheck -x test/e2e-kind.sh + lint-charts: docker: - image: quay.io/helmpack/chart-testing:v2.2.0 steps: - checkout - run: - name: lint - command: | - git remote add k8s https://github.com/your_git_repo/charts - git fetch k8s master - ct lint --config test/ct.yaml + command: ct lint --config test/ct.yaml + install-charts: machine: true - environment: - CHART_TESTING_IMAGE: quay.io/helmpack/chart-testing - CHART_TESTING_TAG: v2.2.0 - CHARTS_REPO: https://github.com/your_git_repo/charts - K8S_VERSION: "v1.12.3" steps: - checkout - run: - name: install - command: | - test/e2e-kind.sh - no_output_timeout: 3600 + command: test/e2e-kind.sh + workflows: version: 2 - lint_and_install: + untagged-build: jobs: - lint-scripts - - lint-charts: - requires: - - lint-scripts - - install-charts: - requires: - - lint-charts + - lint-charts + - install-charts diff --git a/examples/kind/README.md b/examples/kind/README.md index 28ceb62..66e9ee9 100644 --- a/examples/kind/README.md +++ b/examples/kind/README.md @@ -1,5 +1,7 @@ -# Chart testing example with CircleCi and kind - `k`ubernetes `in` `d`ocker +# Chart testing example with CircleCi and kind - `K`ubernetes `in` `D`ocker + +`kind` is a tool for running local Kubernetes clusters using Docker container "nodes". This example shows how to lint and test charts using CircleCi and [kind](https://github.com/kubernetes-sigs/kind). - -`kind` is a tool for running local/CI pipelines Kubernetes clusters using Docker container "nodes". +It creates a cluster with a single master node and three worker nodes. +The cluster configuration can be adjusted in [kind-config.yaml](test/kind-config.yaml). diff --git a/examples/kind/test/ct.yaml b/examples/kind/test/ct.yaml index 59a39dc..d40aa57 100644 --- a/examples/kind/test/ct.yaml +++ b/examples/kind/test/ct.yaml @@ -1,7 +1 @@ -remote: k8s -target-branch: master -chart-dirs: - - stable -excluded-charts: - - common helm-extra-args: --timeout 800 diff --git a/examples/kind/test/e2e-kind.sh b/examples/kind/test/e2e-kind.sh index 44f9d77..cc1068c 100755 --- a/examples/kind/test/e2e-kind.sh +++ b/examples/kind/test/e2e-kind.sh @@ -4,93 +4,104 @@ set -o errexit set -o nounset set -o pipefail -readonly REPO_ROOT="${REPO_ROOT:-$(git rev-parse --show-toplevel)}" +readonly CT_VERSION=v2.2.0 +readonly KIND_VERSION=0.1.0 +readonly CLUSTER_NAME=chart-testing +readonly K8S_VERSION=v1.13.2 -run_kind() { +run_ct_container() { + echo 'Running ct container...' + docker run --rm --interactive --detach --network host --name ct \ + --volume "$(pwd)/test/ct.yaml:/etc/ct/ct.yaml" \ + --volume "$(pwd):/workdir" \ + --workdir /workdir \ + "quay.io/helmpack/chart-testing:$CT_VERSION" \ + cat + echo +} - echo "Download kind binary..." - docker run --rm -it -v "$(pwd)":/go/bin golang go get sigs.k8s.io/kind && sudo mv kind /usr/local/bin/ +cleanup() { + echo 'Removing ct container...' + docker kill ct > /dev/null 2>&1 - echo "Download kubectl..." - 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/ + echo 'Done!' +} + +docker_exec() { + docker exec --interactive ct "$@" +} + +create_kind_cluster() { + echo 'Installing kind...' + + curl -sSLo kind "https://github.com/kubernetes-sigs/kind/releases/download/$KIND_VERSION/kind-linux-amd64" + chmod +x kind + sudo mv kind /usr/local/bin/kind + + kind create cluster --name "$CLUSTER_NAME" --config test/kind-config.yaml --image "kindest/node:$K8S_VERSION" + + docker_exec mkdir -p /root/.kube + + echo 'Copying kubeconfig to container...' + local kubeconfig + kubeconfig="$(kind get kubeconfig-path --name "$CLUSTER_NAME")" + docker cp "$kubeconfig" ct:/root/.kube/config + + docker_exec kubectl cluster-info echo - echo "Create Kubernetes cluster with kind..." - kind create cluster --image=kindest/node:"$K8S_VERSION" + echo -n 'Waiting for cluster to be ready...' + until ! grep --quiet 'NotReady' <(docker_exec kubectl get nodes --no-headers); do + printf '.' + sleep 1 + done - echo "Export kubeconfig..." - # shellcheck disable=SC2155 - export KUBECONFIG="$(kind get kubeconfig-path)" + echo '✔︎' echo - echo "Ensure the apiserver is responding..." - kubectl cluster-info + docker_exec kubectl get nodes echo - echo "Wait for Kubernetes to be up and ready..." - 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 + echo 'Cluster ready!' echo } install_tiller() { - # Install Tiller with RBAC - kubectl -n kube-system create sa tiller - kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller - docker exec "$config_container_id" helm init --service-account tiller - echo "Wait for Tiller to be up and ready..." - until kubectl -n kube-system get pods 2>&1 | grep -w "tiller-deploy" | grep -w "1/1"; do sleep 1; done + echo 'Installing Tiller...' + docker_exec kubectl --namespace kube-system create sa tiller + docker_exec kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller + docker_exec helm init --service-account tiller --upgrade --wait echo } install_hostpath-provisioner() { - # kind doesn't support Dynamic PVC provisioning yet, this one of ways to get it working - # https://github.com/rimusz/charts/tree/master/stable/hostpath-provisioner + # kind doesn't support Dynamic PVC provisioning yet, this is one way to get it working + # https://github.com/rimusz/charts/tree/master/stable/hostpath-provisioner - # delete the default storage class - kubectl delete storageclass standard + echo 'Installing hostpath-provisioner...' - echo "Install Hostpath Provisioner..." - docker exec "$config_container_id" helm repo add rimusz https://charts.rimusz.net - docker exec "$config_container_id" helm repo update - docker exec "$config_container_id" helm upgrade --install hostpath-provisioner --namespace kube-system rimusz/hostpath-provisioner - echo + # Remove default storage class. Will be recreated by hostpath -provisioner + docker_exec kubectl delete storageclass standard + + docker_exec helm repo add rimusz https://charts.rimusz.net + docker_exec helm repo update + docker_exec helm install rimusz/hostpath-provisioner --name hostpath-provisioner --namespace kube-system --wait + echo +} + +install_charts() { + docker_exec ct install + echo } main() { + run_ct_container + trap cleanup EXIT - echo "Starting kind ..." - echo - run_kind - - local config_container_id - config_container_id=$(docker run -it -d -v "$REPO_ROOT:/workdir" --workdir /workdir "$CHART_TESTING_IMAGE:$CHART_TESTING_TAG" cat) - - # shellcheck disable=SC2064 - trap "docker rm -f $config_container_id > /dev/null" EXIT - - # Get kind container IP - kind_container_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' kind-1-control-plane) - # Copy kubeconfig file - docker exec "$config_container_id" mkdir /root/.kube - docker cp "$KUBECONFIG" "$config_container_id:/root/.kube/config" - # Update in kubeconfig from localhost to kind container IP - docker exec "$config_container_id" sed -i "s/localhost:.*/$kind_container_ip:6443/g" /root/.kube/config - - echo "Add git remote k8s ${CHARTS_REPO}" - git remote add k8s "${CHARTS_REPO}" &> /dev/null || true - git fetch k8s master - echo - - # Install Tiller with RBAC + create_kind_cluster install_tiller - - # Install hostpath-provisioner for Dynammic PVC provisioning install_hostpath-provisioner - - # shellcheck disable=SC2086 - docker exec "$config_container_id" ct install --config /workdir/test/ct.yaml - - echo "Done Testing!" + install_charts } main diff --git a/examples/kind/test/kind-config.yaml b/examples/kind/test/kind-config.yaml new file mode 100644 index 0000000..b6a4d5d --- /dev/null +++ b/examples/kind/test/kind-config.yaml @@ -0,0 +1,6 @@ +kind: Config +apiVersion: kind.sigs.k8s.io/v1alpha2 +nodes: + - role: control-plane + - role: worker + replicas: 3