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
This commit is contained in:
Mohammed Naser 2020-01-09 20:06:22 -05:00
parent 228033e78a
commit 8ae49163f3
4 changed files with 58 additions and 0 deletions

View File

@ -4,3 +4,4 @@ Helm Roles
.. zuul:autorole:: ensure-helm
.. zuul:autorole:: ensure-chart-testing
.. zuul:autorole:: chart-testing
.. zuul:autorole:: helm-template

View File

@ -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.

View File

@ -0,0 +1 @@
zuul_work_dir: "{{ zuul.project.src_dir }}"

View File

@ -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