{{- /*
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: {{ include "celeborn.workerStatefulSetName" . }}
  labels:
    {{- include "celeborn.labels" . | nindent 4 }}
    app.kubernetes.io/role: worker
spec:
  serviceName: {{ include "celeborn.workerServiceName" . }}
  selector:
    matchLabels:
      {{- include "celeborn.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/role: worker
  template:
    metadata:
      labels:
        {{- include "celeborn.selectorLabels" . | nindent 8 }}
        app.kubernetes.io/role: worker
        app.kubernetes.io/tag: {{ .Values.image.tag | quote }}
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      initContainers:
      {{- $dirs := .Values.volumes.worker }}
      {{- if eq "hostPath" (index $dirs 0).type }}
      - name: chown-{{ $.Release.Name }}-worker-volume
        image: {{ .Values.image.initContainerImage }}
        {{- with .Values.image.pullPolicy }}
        imagePullPolicy: {{ . }}
        {{- end }}
        command:
        - chown
        - {{ .Values.securityContext.runAsUser | default 10006 }}:{{ .Values.securityContext.runAsGroup | default 10006 }}
        {{- range $dir := $dirs }}
        - {{ $dir.mountPath }}
        {{- end}}
        volumeMounts:
        {{- range $index, $dir := $dirs }}
        - name: {{ $.Release.Name }}-worker-vol-{{ $index }}
          mountPath: {{ $dir.mountPath }}
        {{- end}}
        {{- with .Values.resources.worker }}
        resources:
          {{- toYaml . | nindent 10 }}
        {{- end }}
        securityContext:
          runAsUser: 0
      {{- end }}
      containers:
      - name: {{ .Chart.Name }}
        image: {{ include "celeborn.image" . }}
        {{- with .Values.image.pullPolicy }}
        imagePullPolicy: {{ . }}
        {{- end }}
        command:
        - /usr/bin/tini
        - --
        - /bin/sh
        - -c
        {{- $namespace := .Release.Namespace }}
        - >
          until {{ range until (.Values.masterReplicas | int) }}
          nslookup {{ include "celeborn.masterStatefulSetName" $ }}-{{ . }}.{{ include "celeborn.masterServiceName" $ }}.{{ $namespace }}.svc.{{ $.Values.cluster.name }}.local && 
          {{- end }}
          true; do
            echo "waiting for master";
            sleep 2;
          done && exec /opt/celeborn/sbin/start-worker.sh
        resources:
          {{- toYaml .Values.resources.worker | nindent 10 }}
        ports:
        - containerPort: {{ get .Values.celeborn "celeborn.worker.http.port" | default 9096 }}
          name: metrics
          protocol: TCP
        env:
        {{- range $key, $val := .Values.environments }}
        - name: {{ $key }}
          value: {{ $val | quote }}
        {{- end}}
        volumeMounts:
        - mountPath: /opt/celeborn/conf
          name: {{ include "celeborn.fullname" . }}-volume
          readOnly: true
        {{- range $index, $volume := .Values.volumes.worker }}
        - name: {{ $.Release.Name }}-worker-vol-{{ $index }}
          mountPath: {{ .mountPath }}
        {{- end }}
        {{- with .Values.resources.worker }}
        resources:
          {{- toYaml . | nindent 10 }}
        {{- end }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
      - name: {{ include "celeborn.fullname" . }}-volume
        configMap:
          name: {{ include "celeborn.configMapName" . }}
      {{- range $index, $volume := .Values.volumes.worker }}
      - name: {{ $.Release.Name }}-worker-vol-{{ $index }}
      {{- if eq "emptyDir" $volume.type }}
        emptyDir:
          sizeLimit: {{ $volume.capacity }}
      {{- else if eq "hostPath" $volume.type }}
        hostPath:
          path: {{ $volume.hostPath | default $volume.mountPath }}/worker
          type: DirectoryOrCreate
      {{- else }}
      {{ fail "Currently, Celeborn chart only supports 'emptyDir' and 'hostPath' volume types" }}
      {{- end }}
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity.worker }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if or .Values.priorityClass.worker.name .Values.priorityClass.worker.create }}
      priorityClassName: {{ include "celeborn.workerPriorityClassName" . }}
      {{- end }}
      {{- with .Values.dnsPolicy }}
      dnsPolicy: {{ . }}
      {{- end }}
      {{- with .Values.hostNetwork }}
      hostNetwork: {{ . }}
      {{- end }}
      {{- with .Values.securityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      terminationGracePeriodSeconds: 30
  replicas: {{ .Values.workerReplicas }}
