_workload.tpl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. {{/*
  2. Check if workload type is a deployment
  3. */}}
  4. {{- define "workloadIsDeployment" }}
  5. {{- if eq .Values.workloadType "Deployment" }}
  6. {{- true -}}
  7. {{- else }}
  8. {{- false -}}
  9. {{- end }}
  10. {{- end }}
  11. {{/*
  12. Check if workload type is a cronjob
  13. */}}
  14. {{- define "workloadIsCronJob" }}
  15. {{- if eq .Values.workloadType "CronJob" }}
  16. {{- true -}}
  17. {{- else }}
  18. {{- false -}}
  19. {{- end }}
  20. {{- end }}
  21. {{/*
  22. Get API Version based on workload type
  23. */}}
  24. {{- define "apiVersion" -}}
  25. {{- if eq (include "workloadIsDeployment" .) "true" }}
  26. {{- printf "apps/v1" }}
  27. {{- else if eq (include "workloadIsCronJob" .) "true" }}
  28. {{- printf "batch/v1beta1" }}
  29. {{- else }}
  30. {{- printf "batch/v1" }}
  31. {{- end }}
  32. {{- end }}
  33. {{/*
  34. Get Restart policy based on workload type
  35. */}}
  36. {{- define "restartPolicy" -}}
  37. {{- if eq (include "workloadIsDeployment" .) "true" }}
  38. {{- print "Always" }}
  39. {{- else }}
  40. {{- printf "%s" .Values.jobRestartPolicy }}
  41. {{- end }}
  42. {{- end }}
  43. {{/*
  44. Pod specification
  45. */}}
  46. {{- define "podSepc" }}
  47. restartPolicy: {{ template "restartPolicy" . }}
  48. hostNetwork: {{ template "hostNetworkingConfiguration" . }}
  49. containers:
  50. - name: {{ .Chart.Name }}
  51. {{ include "common.resources.limitaion" . | nindent 2 }}
  52. {{- include "volumeMountsConfiguration" . | indent 2}}
  53. tty: {{ .Values.tty }}
  54. stdin: {{ .Values.stdin }}
  55. securityContext:
  56. privileged: {{ .Values.securityContext.privileged }}
  57. {{ if .Values.securityContext.capabilities }}
  58. capabilities:
  59. add: {{ toYaml .Values.securityContext.capabilities | nindent 8 }}
  60. {{ end }}
  61. image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default "latest" }}"
  62. imagePullPolicy: {{ .Values.image.pullPolicy }}
  63. {{- include "containerCommand" . | indent 2 }}
  64. {{- include "containerArgs" . | indent 2 }}
  65. {{- include "containerEnvVariables" . | indent 2 }}
  66. {{- include "containerLivenssProbe" . | indent 2 }}
  67. {{- include "containerPorts" . | indent 2 }}
  68. {{- include "containerResourceConfiguration" . | indent 2 }}
  69. {{- include "volumeConfiguration" . }}
  70. {{- include "dnsConfiguration" . }}
  71. {{- end }}
  72. {{/*
  73. Annotations for workload
  74. */}}
  75. {{- define "workloadAnnotations" }}
  76. rollme: {{ randAlphaNum 5 | quote }}
  77. {{- if .Values.ixExternalInterfacesConfigurationNames }}
  78. k8s.v1.cni.cncf.io/networks: {{ join ", " .Values.ixExternalInterfacesConfigurationNames }}
  79. {{- end }}
  80. {{- end }}
  81. {{/*
  82. Metadata for workload
  83. */}}
  84. {{- define "commonMetadataWorkload" }}
  85. labels:
  86. {{- include "ix-chart.selectorLabels" . | nindent 2 }}
  87. annotations:
  88. {{- include "workloadAnnotations" . | nindent 2 }}
  89. {{- end }}
  90. {{/*
  91. Deployment Spec
  92. */}}
  93. {{- define "deploymentSpec" }}
  94. strategy:
  95. {{- if and (eq .Values.updateStrategy "RollingUpdate") .Values.hostPortsList }}
  96. {{- fail "RollingUpdate is not allowed when host ports are specified" }}
  97. {{- else if and (eq .Values.updateStrategy "RollingUpdate") .Values.hostNetwork }}
  98. {{- fail "RollingUpdate is not allowed when host network is enabled" }}
  99. {{- end }}
  100. type: {{ .Values.updateStrategy }}
  101. selector:
  102. matchLabels:
  103. {{- include "ix-chart.selectorLabels" . | nindent 4 }}
  104. template:
  105. metadata:
  106. {{ include "commonMetadataWorkload" . | nindent 4 }}
  107. spec:
  108. {{- include "podSepc" . | indent 4 }}
  109. {{- end }}
  110. {{/*
  111. Job Spec Common
  112. */}}
  113. {{- define "jobSpecCommon" }}
  114. metadata:
  115. {{ include "commonMetadataWorkload" . | nindent 4 }}
  116. spec:
  117. {{- include "podSepc" . | indent 2 }}
  118. {{- end }}
  119. {{/*
  120. Job Spec
  121. */}}
  122. {{- define "jobSpec" }}
  123. template:
  124. {{ include "jobSpecCommon" . | nindent 2 }}
  125. {{- end }}
  126. {{/*
  127. CronJob Spec
  128. */}}
  129. {{- define "cronJobSpec" }}
  130. schedule: {{ include "cronExpression" .Values.cronSchedule | quote }}
  131. jobTemplate:
  132. spec:
  133. {{ include "jobSpec" . | nindent 4 }}
  134. {{- end }}