#
# 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.
#

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: celeborn-master
  labels:
    helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    app.kubernetes.io/name: {{ .Chart.Name }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/role: master
    {{- include "celeborn.labels" . | nindent 4 }}
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ .Chart.Name }}
      app.kubernetes.io/instance: {{ .Release.Name }}
      app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
      app.kubernetes.io/role: master
      {{- include "celeborn.selectorLabels" . | nindent 6 }}
  serviceName: celeborn-master-svc
  replicas: {{ .Values.masterReplicas }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        app.kubernetes.io/name: {{ .Chart.Name }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
        app.kubernetes.io/role: master
        app.kubernetes.io/tag: {{ .Values.image.tag }}
        {{- include "celeborn.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.affinity.master }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.securityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      hostNetwork: {{ .Values.hostNetwork | default false }}
      dnsPolicy: {{ .Values.dnsPolicy | default "ClusterFirst" }}
      initContainers:
      {{- $dirs := .Values.volumes.master }}
      {{- if eq "hostPath" (index $dirs 0).type }}
      - name: chown-celeborn-master-volume
        image: alpine:3.18
        imagePullPolicy: {{ .Values.image.pullPolicy | default "IfNotPresent" }}
        securityContext:
          runAsUser: 0
        command:
        - chown
        - {{ .Values.securityContext.runAsUser | default 10006 }}:{{ .Values.securityContext.runAsGroup | default 10006 }}
        - {{ (index $dirs 0).mountPath }}
        volumeMounts:
          - name: celeborn-master-vol-0
            mountPath: {{ (index $dirs 0).mountPath }}
      {{- end }}
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.image.pullPolicy | default "IfNotPresent"}}
        command:
          - "/usr/bin/tini"
          - "--"
          - "/bin/sh"
          - '-c'
          {{- $namespace := .Release.Namespace }}
          - "until {{ range until (.Values.masterReplicas |int) }}nslookup celeborn-master-{{ . }}.celeborn-master-svc.{{ $namespace }}.svc.{{ $.Values.cluster.name }}.local && {{ end }}true; do echo waiting for master; sleep 2; done && exec /opt/celeborn/sbin/start-master.sh"
        resources:
          {{- toYaml .Values.resources.master | nindent 12 }}
        ports:
          - containerPort: {{ .Values.service.port }}
          - containerPort: {{ get .Values.celeborn "celeborn.master.metrics.prometheus.port" | default 9098 }}
            name: metrics
            protocol: TCP
        volumeMounts:
          - mountPath: /opt/celeborn/conf
            name: {{ include "celeborn.fullname" . }}-volume
            readOnly: true
          {{- range $index, $volume := .Values.volumes.master }}
          - name: celeborn-master-vol-{{ $index }}
            mountPath: {{ .mountPath }}
          {{- end }}
        env:
          {{- range $key, $val := .Values.environments }}
          - name: {{ $key }}
            value: {{ $val | quote }}
          {{- end}}
      terminationGracePeriodSeconds: 30 
      volumes:
        - configMap:
            name: {{ .Values.configmap }}
          name: {{ include "celeborn.fullname" . }}-volume
        {{- range $index, $volume := .Values.volumes.master }}
        - name: celeborn-master-vol-{{ $index }}
        {{- if eq "emptyDir" $volume.type }}
          emptyDir:
            sizeLimit: {{ $volume.size }}
        {{- else if eq "hostPath" $volume.type }}
          hostPath:
            path: {{ $volume.hostPath | default $volume.mountPath }}/master
            type: DirectoryOrCreate
        {{- else }}
        {{ fail "For now Celeborn Helm only support emptyDir or hostPath volume types" }}
        {{- end }}
        {{- end }}
