Move spec to functions and better format the structure for workload

This commit is contained in:
Waqar Ahmed 2020-10-08 18:41:12 +05:00
parent e174f013dc
commit 6716877fa7
6 changed files with 230 additions and 135 deletions

View File

@ -0,0 +1,50 @@
{{/*
Container Command
*/}}
{{- define "containerCommand" }}
{{- if .Values.containerCommand }}
command:
{{- range .Values.containerCommand }}
- {{ . | quote}}
{{- end }}
{{- end }}
{{- end }}
{{/*
Container Args
*/}}
{{- define "containerArgs" }}
{{- if .Values.containerArgs }}
args:
{{- range .Values.containerArgs }}
- {{ . | quote}}
{{- end }}
{{- end }}
{{- end }}
{{/*
Container Environment Variables
*/}}
{{- define "containerEnvVariables" }}
{{- if .Values.containerEnvironmentVariables }}
env:
{{- range .Values.containerEnvironmentVariables }}
- name: {{ .name | quote }}
value: {{ .value | quote }}
{{- end }}
{{- end }}
{{- end }}
{{/*
Container Liveness Probe
*/}}
{{- define "containerLivenssProbe" }}
{{- if .Values.livenessProbe }}
livenessProbe:
exec:
command:
{{ toYaml .Values.livenessProbe.command | indent 16 }}
initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
periodSeconds: {{ .Values.periodSeconds }}
{{- end }}
{{- end }}

View File

@ -61,50 +61,3 @@ Create the name of the service account to use
{{- default "default" .Values.serviceAccount.name }} {{- default "default" .Values.serviceAccount.name }}
{{- end }} {{- end }}
{{- end }} {{- end }}
{{/*
Check if workload type is a deployment
*/}}
{{- define "workloadIsDeployment" }}
{{- if eq .Values.workloadType "Deployment" }}
{{- true -}}
{{- else }}
{{- false -}}
{{- end }}
{{- end }}
{{/*
Check if workload type is a cronjob
*/}}
{{- define "workloadIsCronJob" }}
{{- if eq .Values.workloadType "CronJob" }}
{{- true -}}
{{- else }}
{{- false -}}
{{- end }}
{{- end }}
{{/*
Get API Version based on workload type
*/}}
{{- define "apiVersion" -}}
{{- if eq (include "workloadIsDeployment" .) "true" }}
{{- printf "apps/v1" }}
{{- else if eq (include "workloadIsCronJob" .) "true" }}
{{- printf "batch/v1beta1" }}
{{- else }}
{{- printf "batch/v1" }}
{{- end }}
{{- end }}
{{/*
Get Restart policy based on workload type
*/}}
{{- define "restartPolicy" -}}
{{- if eq (include "workloadIsDeployment" .) "true" }}
{{- printf "%s" .Values.restartPolicy }}
{{- else }}
{{- printf "%s" .Values.jobRestartPolicy }}
{{- end }}
{{- end }}

View File

@ -0,0 +1,36 @@
{{/*
Volumes Configuration
*/}}
{{- define "volumeConfiguration" }}
{{- if or .Values.persistentVolumeClaims .Values.hostPathVolumes }}
volumes:
{{- range $index, $hostPathConfiguration := .Values.hostPathVolumes }}
- name: ix-host-path-{{ $.Release.Name }}-{{ $index }}
hostPath:
path: {{ $hostPathConfiguration.hostPath }}
{{- end }}
{{- range $index, $claim := .Values.persistentVolumeClaims }}
- name: ix-pv-{{ $.Release.Name }}-{{ $index }}
persistentVolumeClaim:
claimName: ix-pv-claim-{{ $.Release.Name }}-{{ $index }}
{{- end }}
{{- end }}
{{- end }}
{{/*
Volume Mounts Configuration
*/}}
{{- define "volumeMountsConfiguration" }}
{{- if or .Values.hostPathVolumes .Values.persistentVolumeClaims }}
volumeMounts:
{{- range $index, $hostPathConfiguration := .Values.hostPathVolumes }}
- mountPath: {{ $hostPathConfiguration.mountPath }}
name: ix-host-path-{{ $.Release.Name }}-{{ $index }}
readOnly: {{ $hostPathConfiguration.readOnly }}
{{- end }}
{{- range $index, $claim := .Values.persistentVolumeClaims }}
- mountPath: {{ $claim.mountPath }}
name: ix-pv-{{ $.Release.Name }}-{{ $index }}
{{- end }}
{{- end }}
{{- end }}

View File

@ -0,0 +1,130 @@
{{/*
Check if workload type is a deployment
*/}}
{{- define "workloadIsDeployment" }}
{{- if eq .Values.workloadType "Deployment" }}
{{- true -}}
{{- else }}
{{- false -}}
{{- end }}
{{- end }}
{{/*
Check if workload type is a cronjob
*/}}
{{- define "workloadIsCronJob" }}
{{- if eq .Values.workloadType "CronJob" }}
{{- true -}}
{{- else }}
{{- false -}}
{{- end }}
{{- end }}
{{/*
Get API Version based on workload type
*/}}
{{- define "apiVersion" -}}
{{- if eq (include "workloadIsDeployment" .) "true" }}
{{- printf "apps/v1" }}
{{- else if eq (include "workloadIsCronJob" .) "true" }}
{{- printf "batch/v1beta1" }}
{{- else }}
{{- printf "batch/v1" }}
{{- end }}
{{- end }}
{{/*
Get Restart policy based on workload type
*/}}
{{- define "restartPolicy" -}}
{{- if eq (include "workloadIsDeployment" .) "true" }}
{{- printf "%s" .Values.restartPolicy }}
{{- else }}
{{- printf "%s" .Values.jobRestartPolicy }}
{{- end }}
{{- end }}
{{/*
Pod specification
*/}}
{{- define "podSepc" }}
restartPolicy: {{ template "restartPolicy" . }}
containers:
- name: {{ .Chart.Name }}
{{- include "volumeMountsConfiguration" . | indent 2}}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default "latest" }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- include "containerCommand" | indent 2 }}
{{- include "containerArgs" | indent 2 }}
{{- include "containerEnvVariables" | indent 2 }}
{{- include "containerLivenssProbe" | indent 2 }}
{{- include "volumeConfiguration" . }}
{{- end }}
{{/*
Annotations for workload
*/}}
{{- define "workloadAnnotations" }}
rollme: {{ randAlphaNum 5 | quote }}
{{- if .Values.ixExternalInterfacesConfigurationNames }}
k8s.v1.cni.cncf.io/networks: {{ join ", " .Values.ixExternalInterfacesConfigurationNames }}
{{- end }}
{{- end }}
{{/*
Deployment Spec
*/}}
{{- define "deploymentSpec" }}
strategy:
type: {{ .Values.updateStrategy }}
selector:
matchLabels:
{{- include "ix-chart.selectorLabels" . | nindent 2 }}
template:
metadata:
labels:
{{- include "ix-chart.selectorLabels" . | nindent 6 }}
annotations:
{{- include "workloadAnnotations" . | nindent 6 }}
spec:
{{- include "podSepc" . | indent 4 }}
{{- end }}
{{/*
Job Spec Common
*/}}
{{- define "jobSpecCommon" }}
metadata:
labels:
{{- include "ix-chart.selectorLabels" . | nindent 4 }}
annotations:
{{- include "workloadAnnotations" . | nindent 4 }}
spec:
{{- include "podSepc" . | indent 2 }}
{{- end }}
{{/*
Job Spec
*/}}
{{- define "jobSpec" }}
template:
{{ include "jobSpecCommon" . | nindent 2 }}
{{- end }}
{{/*
CronJob Spec
*/}}
{{- define "cronJobSpec" }}
schedule: {{ .Values.cronSchedule | quote }}
jobTemplate:
spec:
{{ include "jobSpec" . | nindent 4 }}
{{- end }}

View File

@ -1,88 +0,0 @@
apiVersion: {{ template "apiVersion" . }}
kind: {{ .Values.workloadType }}
metadata:
name: {{ include "ix-chart.fullname" . }}
labels:
{{- include "ix-chart.labels" . | nindent 4 }}
spec:
{{- if eq (include "workloadIsDeployment" .) "true" }}
strategy:
type: {{ .Values.updateStrategy }}
selector:
matchLabels:
{{- include "ix-chart.selectorLabels" . | nindent 6 }}
{{- end }}
{{- if ne (include "workloadIsCronJob" .) "true" }}
template:
{{- else }}
schedule: {{ .Values.cronSchedule | quote }}
jobTemplate:
{{- end }}
metadata:
labels:
{{- include "ix-chart.selectorLabels" . | nindent 8 }}
annotations:
rollme: {{ randAlphaNum 5 | quote }}
{{- if .Values.ixExternalInterfacesConfigurationNames }}
k8s.v1.cni.cncf.io/networks: {{ join ", " .Values.ixExternalInterfacesConfigurationNames }}
{{- end }}
spec:
restartPolicy: {{ template "restartPolicy" . }}
containers:
- name: {{ .Chart.Name }}
{{- if or .Values.hostPathVolumes .Values.persistentVolumeClaims }}
volumeMounts:
{{- range $index, $hostPathConfiguration := .Values.hostPathVolumes }}
- mountPath: {{ $hostPathConfiguration.mountPath }}
name: ix-host-path-{{ $.Release.Name }}-{{ $index }}
readOnly: {{ $hostPathConfiguration.readOnly }}
{{- end }}
{{- range $index, $claim := .Values.persistentVolumeClaims }}
- mountPath: {{ $claim.mountPath }}
name: ix-pv-{{ $.Release.Name }}-{{ $index }}
{{- end }}
{{- end }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default "latest" }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- if .Values.containerCommand }}
command:
{{- range .Values.containerCommand }}
- {{ . | quote}}
{{- end }}
{{- end }}
{{- if .Values.containerArgs }}
args:
{{- range .Values.containerArgs }}
- {{ . | quote}}
{{- end }}
{{- end }}
{{- if .Values.containerEnvironmentVariables }}
env:
{{- range .Values.containerEnvironmentVariables }}
- name: {{ .name | quote }}
value: {{ .value | quote }}
{{- end }}
{{- end }}
{{- if .Values.livenessProbe }}
livenessProbe:
exec:
command:
{{ toYaml .Values.livenessProbe.command | indent 16 }}
initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
periodSeconds: {{ .Values.periodSeconds }}
{{- end }}
{{- if or .Values.persistentVolumeClaims .Values.hostPathVolumes }}
volumes:
{{- range $index, $hostPathConfiguration := .Values.hostPathVolumes }}
- name: ix-host-path-{{ $.Release.Name }}-{{ $index }}
hostPath:
path: {{ $hostPathConfiguration.hostPath }}
{{- end }}
{{- range $index, $claim := .Values.persistentVolumeClaims }}
- name: ix-pv-{{ $.Release.Name }}-{{ $index }}
persistentVolumeClaim:
claimName: ix-pv-claim-{{ $.Release.Name }}-{{ $index }}
{{- end }}
{{- end }}

View File

@ -0,0 +1,14 @@
apiVersion: {{ template "apiVersion" . }}
kind: {{ .Values.workloadType }}
metadata:
name: {{ include "ix-chart.fullname" . }}
labels:
{{- include "ix-chart.labels" . | nindent 4 }}
spec:
{{- if eq (include "workloadIsDeployment" .) "true" }}
{{ include "deploymentSpec" | nindent 2 }}
{{- else if ne (include "workloadIsCronJob" .) "true" }}
{{ include "cronJobSpec" . | nindent 2 }}
{{- else }}}
{{ include "jobSpec" . | nindent 2 }}
{{- end }}