{{/*
  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.datanode.env }}
{{- $envFrom := concat .Values.envFrom .Values.datanode.envFrom }}
{{- $nodeSelector := or .Values.datanode.nodeSelector .Values.nodeSelector }}
{{- $affinity := or .Values.datanode.affinity .Values.affinity }}
{{- $tolerations := or .Values.datanode.tolerations .Values.tolerations }}
{{- $securityContext := or .Values.datanode.securityContext .Values.securityContext }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ .Release.Name }}-datanode
  labels:
    {{- include "ozone.labels" . | nindent 4 }}
    app.kubernetes.io/component: datanode
spec:
  replicas: {{ .Values.datanode.replicas }}
  serviceName: {{ .Release.Name }}-datanode-headless
  selector:
    matchLabels:
      {{- include "ozone.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: datanode
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/ozone-configmap.yaml") . | sha256sum }}
      labels:
        {{- include "ozone.selectorLabels" . | nindent 8 }}
        app.kubernetes.io/component: datanode
    spec:
      containers:
        - name: datanode
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- with .Values.datanode.command }}
          command: {{- tpl (toYaml .) $ | nindent 12 }}
          {{- end }}
          {{- with .Values.datanode.args }}
          args: {{- tpl (toYaml .) $ | nindent 12 }}
          {{- end }}
          env:
            {{- include "ozone.configuration.env" . | nindent 12 }}
            {{- with $env }}
              {{- tpl (toYaml .) $ | nindent 12 }}
            {{- end }}
          {{- with $envFrom }}
          envFrom: {{- tpl (toYaml .) $ | nindent 12 }}
          {{- end }}
          ports:
            - name: ui
              containerPort: {{ .Values.datanode.service.port }}
          livenessProbe:
            httpGet:
              path: /
              port: ui
            initialDelaySeconds: 30
          {{- with .Values.datanode.resources }}
          resources: {{- toYaml . | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: config
              mountPath: {{ .Values.configuration.dir }}
            - name: {{ .Release.Name }}-datanode
              mountPath: {{ .Values.datanode.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.datanode.persistence.enabled }}
        - name: {{ .Release.Name }}-datanode
          emptyDir: {}
        {{- end }}
  {{- if .Values.datanode.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: {{ .Release.Name }}-datanode
        labels:
          {{- include "ozone.labels" . | nindent 10 }}
          app.kubernetes.io/component: datanode
      spec:
        {{- with .Values.datanode.persistence.accessModes }}
        accessModes: {{- toYaml . | nindent 10 }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.datanode.persistence.size }}
        {{- with .Values.scm.persistence.storageClassName }}
        storageClassName: {{ . }}
        {{- end }}
  {{- end }}
