{{/*
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you 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.
*/}}

{{- $env := concat .Values.env .Values.om.env }}
{{- $envFrom := concat .Values.envFrom .Values.om.envFrom }}
{{- $nodeSelector := or .Values.om.nodeSelector .Values.nodeSelector }}
{{- $affinity := or .Values.om.affinity .Values.affinity }}
{{- $tolerations := or .Values.om.tolerations .Values.tolerations }}
{{- $securityContext := or .Values.om.securityContext .Values.securityContext }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ .Release.Name }}-om
  labels:
    {{- include "ozone.labels" . | nindent 4 }}
    app.kubernetes.io/component: om
spec:
  replicas: {{ .Values.om.replicas }}
  serviceName: {{ .Release.Name }}-om-headless
  selector:
    matchLabels:
      {{- include "ozone.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: om
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/ozone-configmap.yaml") . | sha256sum }}
      labels:
        {{- include "ozone.selectorLabels" . | nindent 8 }}
        app.kubernetes.io/component: om
    spec:
      containers:
        - name: om
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- with .Values.om.command }}
          command: {{- tpl (toYaml .) $ | nindent 12 }}
          {{- end }}
          {{- with .Values.om.args }}
          args: {{- tpl (toYaml .) $ | nindent 12 }}
          {{- end }}
          env:
            {{- include "ozone.configuration.env" . | nindent 12 }}
            - name: WAITFOR
              value: {{ $.Release.Name }}-scm-0.{{ $.Release.Name }}-scm-headless:9876
            - name: ENSURE_OM_INITIALIZED
              value: /data/metadata/om/current/VERSION
            {{- with $env }}
              {{- tpl (toYaml .) $ | nindent 12 }}
            {{- end }}
          {{- with $envFrom }}
          envFrom: {{- tpl (toYaml .) $ | nindent 12 }}
          {{- end }}
          ports:
            - name: rpc
              containerPort: 9862
            - name: ui
              containerPort: {{ .Values.om.service.port }}
          livenessProbe:
            httpGet:
              path: /
              port: ui
            initialDelaySeconds: 60
          {{- with .Values.om.resources }}
          resources: {{- toYaml . | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: config
              mountPath: {{ .Values.configuration.dir }}
            - name: {{ .Release.Name }}-om
              mountPath: {{ .Values.om.persistence.path }}
      {{- with $nodeSelector }}
      nodeSelector: {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with $affinity }}
      affinity: {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with $tolerations }}
      tolerations: {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with $securityContext }}
      securityContext: {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        - name: config
          projected:
            sources:
              - configMap:
                  name: {{ .Release.Name }}-ozone
              {{- with .Values.configuration.filesFrom }}
                {{- tpl (toYaml .) $ | nindent 14 }}
              {{- end }}
        {{- if not .Values.om.persistence.enabled }}
        - name: {{ .Release.Name }}-om
          emptyDir: {}
        {{- end }}
  {{- if .Values.om.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: {{ .Release.Name }}-om
        labels:
          {{- include "ozone.labels" . | nindent 10 }}
          app.kubernetes.io/component: om
      spec:
        {{- with .Values.om.persistence.accessModes }}
        accessModes: {{- toYaml . | nindent 10 }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.om.persistence.size }}
        {{- with .Values.scm.persistence.storageClassName }}
        storageClassName: {{ . }}
        {{- end }}
  {{- end }}