From 8ae49163f3225d09910f7d44e6208c18f34a64fa Mon Sep 17 00:00:00 2001 From: Mohammed Naser Date: Thu, 9 Jan 2020 20:06:22 -0500 Subject: [PATCH] helm-template: Add role to run 'helm template' This role runs Helm template of a specific chart and then makes sure that all resources become ready afterwards. Change-Id: I62e160eac673d13bd1018b58fd36cae54e1482b4 --- doc/source/helm-roles.rst | 1 + roles/helm-template/README.rst | 17 +++++++++++ roles/helm-template/defaults/main.yaml | 1 + roles/helm-template/tasks/main.yaml | 39 ++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 roles/helm-template/README.rst create mode 100644 roles/helm-template/defaults/main.yaml create mode 100644 roles/helm-template/tasks/main.yaml diff --git a/doc/source/helm-roles.rst b/doc/source/helm-roles.rst index 107681c93..0066faf7c 100644 --- a/doc/source/helm-roles.rst +++ b/doc/source/helm-roles.rst @@ -4,3 +4,4 @@ Helm Roles .. zuul:autorole:: ensure-helm .. zuul:autorole:: ensure-chart-testing .. zuul:autorole:: chart-testing +.. zuul:autorole:: helm-template diff --git a/roles/helm-template/README.rst b/roles/helm-template/README.rst new file mode 100644 index 000000000..811c84700 --- /dev/null +++ b/roles/helm-template/README.rst @@ -0,0 +1,17 @@ +Run Helm by templating the chart, it assumes that a Kubernetes cluster is +already setup and the Helm executable is installed. + +**Role Variables** + +.. zuul:rolevar:: helm_release_name + + Helm release name (mandatory) + +.. zuul:rolevar:: helm_chart + + Directory of the Helm chart. + +.. zuul:rolevar:: zuul_work_dir + :default: {{ zuul.project.src_dir }} + + Directory to run go in. diff --git a/roles/helm-template/defaults/main.yaml b/roles/helm-template/defaults/main.yaml new file mode 100644 index 000000000..9739eb171 --- /dev/null +++ b/roles/helm-template/defaults/main.yaml @@ -0,0 +1 @@ +zuul_work_dir: "{{ zuul.project.src_dir }}" diff --git a/roles/helm-template/tasks/main.yaml b/roles/helm-template/tasks/main.yaml new file mode 100644 index 000000000..77e04437f --- /dev/null +++ b/roles/helm-template/tasks/main.yaml @@ -0,0 +1,39 @@ +- name: Install dependencies + command: "helm dep up {{ helm_chart }}" + args: + chdir: "{{ zuul_work_dir }}" + +- name: Print templated charts + command: "helm template -n zuul {{ helm_chart }}" + args: + chdir: "{{ zuul_work_dir }}" + +- name: Deploy templated charts + shell: | + set -o pipefail + helm template -n {{ helm_release_name }} {{ helm_chart }} | kubectl apply -f- + args: + executable: /bin/bash + chdir: "{{ zuul_work_dir }}" + +# NOTE(mnaser): When a StatefulSet is deployed, it creates the pods one +# by one, which means the `kubectl wait` can race if it +# is ran before the other pods are created. We instead +# check for all the StatefulSets here manually instead +# and then use the second check below to do a "confirmation" +- name: Wait for all StatefulSets to become ready + block: + - name: Retrieve all StatefulSets + command: kubectl get statefulset -o name + register: _statefulsets + + - name: Ensure the number of ready replicas matches the replicas + shell: kubectl get {{ item }} -ogo-template='{{ '{{' }}eq .status.replicas .status.readyReplicas{{ '}}' }}' + register: _is_ready + until: _is_ready.stdout == 'true' + retries: 60 + delay: 5 + loop: "{{ _statefulsets.stdout_lines }}" + +- name: Wait for all pods to become ready + command: kubectl wait --for=condition=Ready --timeout=120s pod --all