|
| 1 | +{{/* |
| 2 | +Copyright Drycc Community. |
| 3 | +SPDX-License-Identifier: APACHE-2.0 |
| 4 | +*/}} |
| 5 | + |
| 6 | +{{/* vim: set filetype=mustache: */}} |
| 7 | +{{/* |
| 8 | +Returns the name that will identify the repository internally and it will be used to create folders or |
| 9 | +volume names |
| 10 | +*/}} |
| 11 | +{{- define "airflow.git.repository.name" -}} |
| 12 | + {{- $defaultName := regexFind "/.*$" .repository | replace "//" "" | replace "/" "-" | replace "." "-" -}} |
| 13 | + {{- .name | default $defaultName | kebabcase -}} |
| 14 | +{{- end -}} |
| 15 | + |
| 16 | +{{/* |
| 17 | +Returns the volume mounts that will be used by git containers (clone and sync) |
| 18 | +*/}} |
| 19 | +{{- define "airflow.git.volumeMounts" -}} |
| 20 | +{{- if .Values.git.dags.enabled }} |
| 21 | +- name: git-cloned-dags |
| 22 | + mountPath: /dags |
| 23 | +{{- end }} |
| 24 | +{{- if .Values.git.plugins.enabled }} |
| 25 | +- name: git-cloned-plugins |
| 26 | + mountPath: /plugins |
| 27 | +{{- end }} |
| 28 | +{{- end -}} |
| 29 | + |
| 30 | +{{/* |
| 31 | +Returns the volume mounts that will be used by the main container |
| 32 | +*/}} |
| 33 | +{{- define "airflow.git.maincontainer.volumeMounts" -}} |
| 34 | +{{- if .Values.git.dags.enabled }} |
| 35 | + {{- range .Values.git.dags.repositories }} |
| 36 | +- name: git-cloned-dags |
| 37 | + mountPath: /opt/drycc/airflow/dags/git_{{ include "airflow.git.repository.name" . }} |
| 38 | + {{- if .path }} |
| 39 | + subPath: {{ include "airflow.git.repository.name" . }}/{{ .path }} |
| 40 | + {{- else }} |
| 41 | + subPath: {{ include "airflow.git.repository.name" . }} |
| 42 | + {{- end }} |
| 43 | + {{- end }} |
| 44 | +{{- end }} |
| 45 | +{{- if .Values.git.plugins.enabled }} |
| 46 | + {{- range .Values.git.plugins.repositories }} |
| 47 | +- name: git-cloned-plugins |
| 48 | + mountPath: /opt/drycc/airflow/plugins/git_{{ include "airflow.git.repository.name" . }} |
| 49 | + {{- if .path }} |
| 50 | + subPath: {{ include "airflow.git.repository.name" . }}/{{ .path }} |
| 51 | + {{- else }} |
| 52 | + subPath: {{ include "airflow.git.repository.name" . }} |
| 53 | + {{- end }} |
| 54 | + {{- end }} |
| 55 | +{{- end }} |
| 56 | +{{- end -}} |
| 57 | + |
| 58 | +{{/* |
| 59 | +Returns the volumes that will be attached to the workload resources (deployment, statefulset, etc) |
| 60 | +*/}} |
| 61 | +{{- define "airflow.git.volumes" -}} |
| 62 | +{{- if .Values.git.dags.enabled }} |
| 63 | +- name: git-cloned-dags |
| 64 | + emptyDir: {} |
| 65 | +{{- end }} |
| 66 | +{{- if .Values.git.plugins.enabled }} |
| 67 | +- name: git-cloned-plugins |
| 68 | + emptyDir: {} |
| 69 | +{{- end }} |
| 70 | +{{- end -}} |
| 71 | + |
| 72 | +{{/* |
| 73 | +Returns the init container that will clone repositories files from a given list of git repositories |
| 74 | +Usage: |
| 75 | +{{ include "airflow.git.containers.clone" ( dict "securityContext" .Values.path.to.the.component.securityContext "context" $ ) }} |
| 76 | +*/}} |
| 77 | +{{- define "airflow.git.containers.clone" -}} |
| 78 | +{{- if or .context.Values.git.dags.enabled .context.Values.git.plugins.enabled }} |
| 79 | +- name: clone-repositories |
| 80 | + image: {{ include "git.image" .context | quote }} |
| 81 | + imagePullPolicy: {{ .context.Values.git.image.pullPolicy | quote }} |
| 82 | +{{- if .securityContext.enabled }} |
| 83 | + securityContext: {{- omit .securityContext "enabled" | toYaml | nindent 4 }} |
| 84 | +{{- end }} |
| 85 | +{{- if .context.Values.git.clone.resources }} |
| 86 | + resources: {{- toYaml .context.Values.git.clone.resources | nindent 4 }} |
| 87 | +{{- end }} |
| 88 | +{{- if .context.Values.git.clone.command }} |
| 89 | + command: {{- include "common.tplvalues.render" (dict "value" .context.Values.git.clone.command "context" .context) | nindent 4 }} |
| 90 | +{{- else }} |
| 91 | + command: |
| 92 | + - /bin/bash |
| 93 | +{{- end }} |
| 94 | +{{- if .context.Values.git.clone.args }} |
| 95 | + args: {{- include "common.tplvalues.render" (dict "value" .context.Values.git.clone.args "context" .context) | nindent 4 }} |
| 96 | +{{- else }} |
| 97 | + args: |
| 98 | + - -ec |
| 99 | + - | |
| 100 | + . /opt/drycc/scripts/libfs.sh |
| 101 | + {{- if .context.Values.git.dags.enabled }} |
| 102 | + {{- range .context.Values.git.dags.repositories }} |
| 103 | + is_dir_empty "/dags/{{ include "airflow.git.repository.name" . }}" && git clone {{ .repository }} --branch {{ .branch }} /dags/{{ include "airflow.git.repository.name" . }} |
| 104 | + {{- end }} |
| 105 | + {{- end }} |
| 106 | + {{- if .context.Values.git.plugins.enabled }} |
| 107 | + {{- range .context.Values.git.plugins.repositories }} |
| 108 | + is_dir_empty "/plugins/{{ include "airflow.git.repository.name" . }}" && git clone {{ .repository }} --branch {{ .branch }} /plugins/{{ include "airflow.git.repository.name" . }} |
| 109 | + {{- end }} |
| 110 | + {{- end }} |
| 111 | +{{- end }} |
| 112 | + volumeMounts: |
| 113 | + {{- include "airflow.git.volumeMounts" .context | trim | nindent 4 }} |
| 114 | + {{- if .context.Values.git.clone.extraVolumeMounts }} |
| 115 | + {{- include "common.tplvalues.render" (dict "value" .context.Values.git.clone.extraVolumeMounts "context" .context) | nindent 4 }} |
| 116 | + {{- end }} |
| 117 | +{{- if .context.Values.git.clone.extraEnvVars }} |
| 118 | + env: {{- include "common.tplvalues.render" (dict "value" .context.Values.git.clone.extraEnvVars "context" .context) | nindent 4 }} |
| 119 | +{{- end }} |
| 120 | +{{- if or .context.Values.git.clone.extraEnvVarsCM .context.Values.git.clone.extraEnvVarsSecret }} |
| 121 | + envFrom: |
| 122 | + {{- if .context.Values.git.clone.extraEnvVarsCM }} |
| 123 | + - configMapRef: |
| 124 | + name: {{ .context.Values.git.clone.extraEnvVarsCM }} |
| 125 | + {{- end }} |
| 126 | + {{- if .context.Values.git.clone.extraEnvVarsSecret }} |
| 127 | + - secretRef: |
| 128 | + name: {{ .context.Values.git.clone.extraEnvVarsSecret }} |
| 129 | + {{- end }} |
| 130 | +{{- end }} |
| 131 | +{{- end }} |
| 132 | +{{- end -}} |
| 133 | + |
| 134 | +{{/* |
| 135 | +Returns the container that will pull and sync repositories files from a given list of git repositories |
| 136 | +Usage: |
| 137 | +{{ include "airflow.git.containers.sync" ( dict "securityContext" .Values.path.to.the.component.securityContext "context" $ ) }} |
| 138 | +*/}} |
| 139 | +{{- define "airflow.git.containers.sync" -}} |
| 140 | +{{- if or .context.Values.git.dags.enabled .context.Values.git.plugins.enabled }} |
| 141 | +- name: sync-repositories |
| 142 | + image: {{ include "git.image" .context | quote }} |
| 143 | + imagePullPolicy: {{ .context.Values.git.image.pullPolicy | quote }} |
| 144 | +{{- if .securityContext.enabled }} |
| 145 | + securityContext: {{- omit .securityContext "enabled" | toYaml | nindent 4 }} |
| 146 | +{{- end }} |
| 147 | +{{- if .context.Values.git.sync.resources }} |
| 148 | + resources: {{- toYaml .context.Values.git.sync.resources | nindent 4 }} |
| 149 | +{{- end }} |
| 150 | +{{- if .context.Values.git.sync.command }} |
| 151 | + command: {{- include "common.tplvalues.render" (dict "value" .context.Values.git.sync.command "context" .context) | nindent 4 }} |
| 152 | +{{- else }} |
| 153 | + command: |
| 154 | + - /bin/bash |
| 155 | +{{- end }} |
| 156 | +{{- if .context.Values.git.sync.args }} |
| 157 | + args: {{- include "common.tplvalues.render" (dict "value" .context.Values.git.sync.args "context" .context) | nindent 4 }} |
| 158 | +{{- else }} |
| 159 | + args: |
| 160 | + - -ec |
| 161 | + - | |
| 162 | + while true; do |
| 163 | + {{- if .context.Values.git.dags.enabled }} |
| 164 | + {{- range .context.Values.git.dags.repositories }} |
| 165 | + cd /dags/{{ include "airflow.git.repository.name" . }} && git pull origin {{ .branch }} || true |
| 166 | + {{- end }} |
| 167 | + {{- end }} |
| 168 | + {{- if .context.Values.git.plugins.enabled }} |
| 169 | + {{- range .context.Values.git.plugins.repositories }} |
| 170 | + cd /plugins/{{ include "airflow.git.repository.name" . }} && git pull origin {{ .branch }} || true |
| 171 | + {{- end }} |
| 172 | + {{- end }} |
| 173 | + sleep {{ default "60" .context.Values.git.sync.interval }} |
| 174 | + done |
| 175 | +{{- end }} |
| 176 | + volumeMounts: |
| 177 | + {{- include "airflow.git.volumeMounts" .context | trim | nindent 4 }} |
| 178 | + {{- if .context.Values.git.sync.extraVolumeMounts }} |
| 179 | + {{- include "common.tplvalues.render" (dict "value" .context.Values.git.sync.extraVolumeMounts "context" .context) | nindent 4 }} |
| 180 | + {{- end }} |
| 181 | +{{- if .context.Values.git.sync.extraEnvVars }} |
| 182 | + env: {{- include "common.tplvalues.render" (dict "value" .context.Values.git.sync.extraEnvVars "context" .context) | nindent 4 }} |
| 183 | +{{- end }} |
| 184 | +{{- if or .context.Values.git.sync.extraEnvVarsCM .context.Values.git.sync.extraEnvVarsSecret }} |
| 185 | + envFrom: |
| 186 | + {{- if .context.Values.git.sync.extraEnvVarsCM }} |
| 187 | + - configMapRef: |
| 188 | + name: {{ .context.Values.git.sync.extraEnvVarsCM }} |
| 189 | + {{- end }} |
| 190 | + {{- if .context.Values.git.sync.extraEnvVarsSecret }} |
| 191 | + - secretRef: |
| 192 | + name: {{ .context.Values.git.sync.extraEnvVarsSecret }} |
| 193 | + {{- end }} |
| 194 | +{{- end }} |
| 195 | +{{- end }} |
| 196 | +{{- end -}} |
0 commit comments