{{/*
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.cronjob_checkPGs }}
{{- $envAll := . }}

{{- $serviceAccountName := "ceph-pool-checkpgs" }}
{{ tuple $envAll "pool_checkpgs" $serviceAccountName | include "helm-toolkit.snippets.kubernetes_pod_rbac_serviceaccount" }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: {{ $serviceAccountName }}
rules:
  - apiGroups:
      - ""
    resources:
      - pods
      - pods/exec
    verbs:
      - get
      - list
      - watch
      - create
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ $serviceAccountName }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ $serviceAccountName }}
subjects:
  - kind: ServiceAccount
    name: {{ $serviceAccountName }}
    namespace: {{ $envAll.Release.Namespace }}
---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ $serviceAccountName }}
  annotations:
    {{ tuple $envAll | include "helm-toolkit.snippets.release_uuid" }}
  labels:
{{ tuple $envAll "ceph" "pool-checkpgs" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 4 }}
spec:
  schedule: {{ .Values.jobs.pool_checkPGs.cron | quote }}
  successfulJobsHistoryLimit: {{ .Values.jobs.pool_checkPGs.history.successJob }}
  failedJobsHistoryLimit: {{ .Values.jobs.pool_checkPGs.history.failJob }}
  concurrencyPolicy: {{ .Values.jobs.pool_checkPGs.concurrency.execPolicy }}
  startingDeadlineSeconds: {{ .Values.jobs.pool_checkPGs.startingDeadlineSecs }}
  jobTemplate:
    metadata:
      labels:
{{ tuple $envAll "ceph" "pool-checkpgs" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 8 }}
    spec:
      template:
        metadata:
          labels:
{{ tuple $envAll "ceph" "pool-checkpgs" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 12 }}
        spec:
          serviceAccountName: {{ $serviceAccountName }}
          nodeSelector:
            {{ .Values.labels.mgr.node_selector_key }}: {{ .Values.labels.mgr.node_selector_value }}
          initContainers:
{{ tuple $envAll "pool_checkpgs" list | include "helm-toolkit.snippets.kubernetes_entrypoint_init_container" | indent 12 }}
          containers:
          - name: {{ $serviceAccountName }}
{{ tuple $envAll "ceph_config_helper" | include "helm-toolkit.snippets.image" | indent 12 }}
            env:
              - name: DEPLOYMENT_NAMESPACE
                valueFrom:
                  fieldRef:
                    fieldPath: metadata.namespace
            command:
              - /tmp/utils-checkPGs.sh
            volumeMounts:
              - name: pod-tmp
                mountPath: /tmp
              - name: pod-etc-ceph
                mountPath: /etc/ceph
              - name: ceph-client-bin
                mountPath: /tmp/utils-checkPGs.sh
                subPath: utils-checkPGs.sh
                readOnly: true
              - name: ceph-client-etc
                mountPath: /etc/ceph/ceph.conf
                subPath: ceph.conf
                readOnly: true
              - mountPath: /etc/ceph/ceph.client.admin.keyring
                name: ceph-client-admin-keyring
                readOnly: true
                subPath: ceph.client.admin.keyring
              - mountPath: /etc/ceph/ceph.mon.keyring.seed
                name: ceph-mon-keyring
                readOnly: true
                subPath: ceph.mon.keyring
              - mountPath: /var/lib/ceph/bootstrap-osd/ceph.keyring
                name: ceph-bootstrap-osd-keyring
                readOnly: true
                subPath: ceph.keyring
              - mountPath: /var/lib/ceph/bootstrap-mds/ceph.keyring
                name: ceph-bootstrap-mds-keyring
                readOnly: true
                subPath: ceph.keyring
          restartPolicy: Never
          hostNetwork: true
          volumes:
            - name: pod-tmp
              emptyDir: {}
            - name: pod-etc-ceph
              emptyDir: {}
            - name: ceph-client-bin
              configMap:
                name: ceph-client-bin
                defaultMode: 0555
            - name: ceph-client-etc
              configMap:
                name: ceph-client-etc
                defaultMode: 0444
            - name: ceph-client-admin-keyring
              secret:
                defaultMode: 420
                secretName: ceph-client-admin-keyring
            - name: ceph-mon-keyring
              secret:
                defaultMode: 420
                secretName: ceph-mon-keyring
            - name: ceph-bootstrap-osd-keyring
              secret:
                defaultMode: 420
                secretName: ceph-bootstrap-osd-keyring
            - name: ceph-bootstrap-mds-keyring
              secret:
                defaultMode: 420
                secretName: ceph-bootstrap-mds-keyring

{{- end }}
