{{/*
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.process_monitor }}
{{- $envAll := . }}
{{- $daemonset := "masakari-process-monitor" }}

{{- $mounts_masakari_process_monitor := .Values.pod.mounts.masakari_process_monitor.masakari_process_monitor }}
{{- $mounts_masakari_process_monitor_init := .Values.pod.mounts.masakari_process_monitor.init_container }}

{{- $serviceAccountName := "masakari-process-monitor" }}
{{- tuple $envAll "masakari_process_monitor" $serviceAccountName | include "helm-toolkit.snippets.kubernetes_pod_rbac_serviceaccount" }}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: masakari-process-monitor
  annotations:
    {{ tuple $envAll | include "helm-toolkit.snippets.release_uuid" }}
  labels:
{{ tuple $envAll .Chart.Name $daemonset | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 4 }}
spec:
  selector:
    matchLabels:
{{ tuple $envAll .Chart.Name $daemonset | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 6 }}
{{ tuple $envAll $daemonset | include "helm-toolkit.snippets.kubernetes_upgrades_daemonset" | indent 2 }}
  template:
    metadata:
      labels:
{{ tuple $envAll .Chart.Name $daemonset | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 8 }}
      annotations:
{{- dict "envAll" $envAll "podName" "masakari-process-monitor" "containerNames" (list "masakari-monitor") | include "helm-toolkit.snippets.kubernetes_mandatory_access_control_annotation" | indent 8 }}
{{ 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" }}
    spec:
{{ dict "envAll" $envAll "application" "masakari-process-monitor" | include "helm-toolkit.snippets.kubernetes_pod_security_context" | indent 6 }}
      serviceAccountName: {{ $serviceAccountName }}
      nodeSelector:
        {{ .Values.labels.monitors.node_selector_key }}: {{ .Values.labels.monitors.node_selector_value }}
      initContainers:
{{ tuple $envAll "masakari_process_monitor" $mounts_masakari_process_monitor_init | include "helm-toolkit.snippets.kubernetes_entrypoint_init_container" | indent 8 }}
        - name: procressemonitor-init
{{ tuple $envAll "masakari_instance_monitor" | include "helm-toolkit.snippets.image" | indent 10 }}
{{ tuple $envAll $envAll.Values.pod.resources.masakari_instance_monitor | include "helm-toolkit.snippets.kubernetes_resources" | indent 10 }}
{{ dict "envAll" $envAll "application" "masakari" "container" "masakari_process_monitor" | include "helm-toolkit.snippets.kubernetes_container_security_context" | indent 10 }}
          command:
            - /tmp/masakari-monitors-init.sh
          env:
            - name: COMPUTE_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
          volumeMounts:
            - name: pod-tmp
              mountPath: /tmp
            - name: pod-shared
              mountPath: /tmp/pod-shared
            - name: masakari-bin
              mountPath: /tmp/masakari-monitors-init.sh
              subPath: masakari-monitors-init.sh
              readOnly: true
      containers:
        - name: masakari-process-monitor
{{ tuple $envAll "masakari_process_monitor" | include "helm-toolkit.snippets.image" | indent 10 }}
{{ tuple $envAll $envAll.Values.pod.resources.masakari_process_monitor | include "helm-toolkit.snippets.kubernetes_resources" | indent 10 }}
{{ dict "envAll" $envAll "application" "masakari" "container" "masakari_process_monitor" | include "helm-toolkit.snippets.kubernetes_container_security_context" | indent 10 }}
          command:
            - /tmp/masakari-process-monitor.sh
            - start
          env:
            - name: COMPUTE_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
          lifecycle:
            preStop:
              exec:
                command:
                  - /tmp/masakari-process-monitor.sh
                  - stop
          volumeMounts:
            - name: pod-tmp
              mountPath: /tmp
            - name: etcmasakari
              mountPath: /etc/masakari
            - name: pod-shared
              mountPath: /tmp/pod-shared
            - name: masakari-bin
              mountPath: /tmp/masakari-process-monitor.sh
              subPath: masakari-process-monitor.sh
            - name: masakari-etc
              mountPath: /etc/masakari/masakarimonitors.conf
              subPath: masakarimonitors.conf
            - name: varrun
              mountPath: /var/run
            - name: run
              mountPath: /run
      volumes:
        - name: pod-tmp
          emptyDir: {}
        - name: etcmasakari
          emptyDir: {}
        - name: pod-shared
          emptyDir: {}
        - name: varrun
          hostPath:
            path: /var/run
        - name: run
          hostPath:
            path: /run
        - name: masakari-bin
          configMap:
            name: masakari-bin
            defaultMode: 0555
        - name: masakari-etc
          secret:
            secretName: masakari-etc
            defaultMode: 0444
{{- end }}