{{/*
  * Bootstrap a deployment
  * It should always receive a dict as an argument
*/}}
{{- define "lib.core.deployment" -}} {{- /* define[0] */ -}}
{{- include "lib.error.noCtx" . -}}
{{- include "lib.error.noKey" (dict "ctx" . "key" "securityContext") -}}
{{- include "lib.error.noKey" (dict "ctx" . "key" "metadata") -}}
---
# ---------------------------------------------------------------------
# -- This resource is managed by the allanger's helm library
# ---------------------------------------------------------------------
apiVersion: apps/v1
kind: Deployment
metadata:
{{ .metadata | indent 2 }}
spec:
{{- 
  include "lib.core.deployment.spec" 
  (dict 
    "ctx" .ctx
    "securityContext" .securityContext
    "metadata" .metadata
    "containers" .containers
    "volumes" .volumes
  ) 
  | indent 2 
}}
{{- end -}} {{- /* /define[0] */ -}}

{{- define "lib.core.deployment.spec" -}} {{- /* define[0] */ -}}
{{- include "lib.error.noCtx" . }}
{{- include "lib.error.noKey" (dict "ctx" . "key" "securityContext") -}}
{{- include "lib.error.noKey" (dict "ctx" . "key" "metadata") }}
replicas: {{ .ctx.Values.base.workload.replicas | default 1 }}
selector:
  matchLabels:
    {{- include "lib.chart.selectorLabels" (dict "ctx" .ctx) | nindent 6 }}
{{
  include "lib.core.deployment.template" 
  (dict 
    "ctx" .ctx
    "securityContext" .securityContext
    "metadata" .metadata
    "containers" .containers
    "volumes" .volumes
  ) 
}}
{{- end -}} {{- /* /define[0] */ -}}

{{- /* A deployment temopalte goes here */ -}}
{{- define "lib.core.deployment.template" -}} {{- /* define[0] */ -}}
{{- include "lib.error.noCtx" . -}}
{{- include "lib.error.noKey" (dict "ctx" . "key" "securityContext") -}}
{{- include "lib.error.noKey" (dict "ctx" . "key" "metadata") -}}
{{- include "lib.error.noKey" (dict "ctx" . "key" "containers") -}}
{{- $labels := toYaml (index (fromYaml .metadata) "labels") -}}
{{- $annotations := toYaml (index (fromYaml .metadata) "annotations") -}}
template:
  metadata:
    labels:
      {{- $labels | nindent 6 }}
    annotations:
    {{- if not (eq $annotations "null") }}
    {{- $annotations | nindent 6 }}
    {{- end }}
    {{
      include "lib.helpers.hashes" 
      (dict 
        "env" (.ctx.Values.config).env 
        "files" (.ctx.Values.config).files) 
        | nindent 6 
    }}
  spec:
    {{- .securityContext | nindent 4 }}
    {{ .containers  | nindent 4 }}
    {{ .volumes | nindent 4 }}
{{- end -}} {{- /* /define[0] */ -}}
