{{/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}

{{- if .Values.manifests.deployment_manager }}
{{- $envAll := . }}

{{- $mounts := .Values.pod.mounts.blazar_manager.blazar_manager }}
{{- $mounts_init := .Values.pod.mounts.blazar_manager.init_container }}

{{- $serviceAccountName := "blazar-manager" }}
{{ tuple $envAll "manager" $serviceAccountName | include "helm-toolkit.snippets.kubernetes_pod_rbac_serviceaccount" }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: blazar-manager
  annotations:
    {{ tuple $envAll | include "helm-toolkit.snippets.release_uuid" }}
  labels:
{{ tuple $envAll "blazar" "manager" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 4 }}
spec:
  replicas: {{ .Values.pod.replicas.manager }}
  selector:
    matchLabels:
{{ tuple $envAll "blazar" "manager" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 6 }}
{{ tuple $envAll | include "helm-toolkit.snippets.kubernetes_upgrades_deployment" | indent 2 }}
  template:
    metadata:
      labels:
{{ tuple $envAll "blazar" "manager" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 8 }}
      annotations:
{{ tuple $envAll | include "helm-toolkit.snippets.release_uuid" | indent 8 }}
        configmap-bin-hash: {{ tuple "configmap-bin.yaml" . | include "helm-toolkit.utils.hash" }}
        configmap-etc-hash: {{ tuple "configmap-etc.yaml" . | include "helm-toolkit.utils.hash" }}
{{ dict "envAll" $envAll "podName" "blazar-manager" "containerNames" (list "blazar-manager" "init") | include "helm-toolkit.snippets.kubernetes_mandatory_access_control_annotation" | indent 8 }}
    spec:
      serviceAccountName: {{ $serviceAccountName }}
{{ dict "envAll" $envAll "application" "blazar" | include "helm-toolkit.snippets.kubernetes_pod_security_context" | indent 6 }}
      affinity:
{{ tuple $envAll "blazar" "manager" | include "helm-toolkit.snippets.kubernetes_pod_anti_affinity" | indent 8 }}
      nodeSelector:
        {{ .Values.labels.manager.node_selector_key }}: {{ .Values.labels.manager.node_selector_value | quote }}
      terminationGracePeriodSeconds: {{ .Values.pod.lifecycle.termination_grace_period.manager.timeout | default "30" }}
      initContainers:
{{ tuple $envAll "manager" $mounts_init | include "helm-toolkit.snippets.kubernetes_entrypoint_init_container" | indent 8 }}
      containers:
        - name: blazar-manager
{{ tuple $envAll "blazar_manager" | include "helm-toolkit.snippets.image" | indent 10 }}
{{ tuple $envAll $envAll.Values.pod.resources.manager | include "helm-toolkit.snippets.kubernetes_resources" | indent 10 }}
{{ dict "envAll" $envAll "application" "blazar" "container" "blazar_manager" | include "helm-toolkit.snippets.kubernetes_container_security_context" | indent 10 }}
          command:
            - /tmp/blazar-manager.sh
          env:
{{- with $env := dict "ksUserSecret" .Values.secrets.identity.blazar }}
{{- include "helm-toolkit.snippets.keystone_openrc_env_vars" $env | indent 12 }}
{{- end }}
          volumeMounts:
            - name: pod-tmp
              mountPath: /tmp
            - name: pod-etc-blazar
              mountPath: /etc/blazar
            - name: blazar-bin
              mountPath: /tmp/blazar-manager.sh
              subPath: blazar-manager.sh
              readOnly: true
            - name: blazar-etc
              mountPath: /etc/blazar/blazar.conf
              subPath: blazar.conf
              readOnly: true
            {{- if .Values.conf.blazar.DEFAULT.log_config_append }}
            - name: blazar-etc
              mountPath: {{ .Values.conf.blazar.DEFAULT.log_config_append }}
              subPath: {{ base .Values.conf.blazar.DEFAULT.log_config_append }}
              readOnly: true
            {{- end }}
            - name: blazar-etc
              mountPath: /etc/blazar/policy.yaml
              subPath: policy.yaml
              readOnly: true
{{- if $mounts.volumeMounts }}
{{ toYaml $mounts.volumeMounts | indent 12 }}
{{- end }}
      volumes:
        - name: pod-tmp
          emptyDir: {}
        - name: pod-etc-blazar
          emptyDir: {}
        - name: blazar-bin
          configMap:
            name: blazar-bin
            defaultMode: 0555
        - name: blazar-etc
          secret:
            secretName: blazar-etc
            defaultMode: 0444
{{- if $mounts.volumes }}
{{ toYaml $mounts.volumes | indent 8 }}
{{- end }}
{{- end }}
...
