{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
  name:  {{ include "flink.taskmanager.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: flink
    app.kubernetes.io/component: taskmanager
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.taskmanager.replicaCount }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.taskmanager.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: flink
      app.kubernetes.io/component: taskmanager
  podManagementPolicy: {{ .Values.taskmanager.podManagementPolicy }}
  serviceName: {{ printf "%s-headless" (include "flink.taskmanager.fullname" .) | trunc 63 | trimSuffix "-" }}
  updateStrategy: {{- toYaml .Values.taskmanager.updateStrategy | nindent 4 }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: flink
        app.kubernetes.io/component: taskmanager
      {{- if or .Values.taskmanager.podAnnotations .Values.commonAnnotations }}
      {{- $podAnnotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.taskmanager.podAnnotations .Values.commonAnnotations ) "context" . ) }}
      annotations: {{- include "common.tplvalues.render" (dict "value" $podAnnotations "context" $) | nindent 8 }}
      {{- end }}
    spec:
      {{- include "flink.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.taskmanager.automountServiceAccountToken }}
      {{- if .Values.taskmanager.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.taskmanager.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.taskmanager.schedulerName }}
      schedulerName: {{ .Values.taskmanager.schedulerName }}
      {{- end }}
      priorityClassName: {{ .Values.taskmanager.priorityClassName | quote }}
      {{- if .Values.taskmanager.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.taskmanager.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.taskmanager.podAffinityPreset "component" "taskmanager" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.taskmanager.podAntiAffinityPreset "component" "taskmanager" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.taskmanager.nodeAffinityPreset.type "key" .Values.taskmanager.nodeAffinityPreset.key "values" .Values.taskmanager.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.taskmanager.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.taskmanager.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.taskmanager.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.taskmanager.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.taskmanager.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.taskmanager.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.taskmanager.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.taskmanager.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
      {{- if .Values.taskmanager.initContainers }}
      {{- include "common.tplvalues.render" (dict "value" .Values.taskmanager.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: flink-taskmanager
          image: {{ include "flink.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.taskmanager.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.taskmanager.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.taskmanager.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.taskmanager.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.taskmanager.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.taskmanager.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.taskmanager.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.taskmanager.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          ports:
            - name: data
              containerPort: {{ .Values.taskmanager.containerPorts.data }}
            - name: rpc
              containerPort: {{ .Values.taskmanager.containerPorts.rpc }}
            - name: int-metrics
              containerPort: {{ .Values.taskmanager.containerPorts.internalMetrics }}
          env:
            - name: MY_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: FLINK_MODE
              value: taskmanager
            # Hosts and addresses are determined by the service full name, which maps to flink.*§.fullname
            - name: FLINK_CFG_JOBMANAGER_RPC_ADDRESS
              value: {{ include "flink.jobmanager.fullname" . | quote }}
            - name: FLINK_CFG_JOBMANAGER_RPC_PORT
              value: {{ .Values.jobmanager.service.ports.rpc | quote }}
            - name: FLINK_CFG_JOBMANAGER_BIND__HOST
              value: 0.0.0.0
            - name: FLINK_CFG_TASKMANAGER_DATA_PORT
              value: {{ .Values.taskmanager.containerPorts.data | quote }}
            - name: FLINK_CFG_TASKMANAGER_RPC_PORT
              value: {{ .Values.taskmanager.containerPorts.rpc | quote }}
            - name: FLINK_CFG_METRICS_INTERNAL_QUERY__SERVICE_PORT
              value: {{ .Values.taskmanager.containerPorts.internalMetrics | quote }}
            {{- $releaseNamespace := include "common.names.namespace" . }}
            {{- $headlessServiceName := printf "%s-headless" (include "flink.taskmanager.fullname" .) | trunc 63 | trimSuffix "-" }}
            {{- $clusterDomain := .Values.clusterDomain }}
            - name: FLINK_CFG_TASKMANAGER_HOST
              value: "$(MY_POD_NAME).{{ $headlessServiceName }}.{{ $releaseNamespace }}.svc.{{ $clusterDomain }}"
            - name: FLINK_CFG_TASKMANAGER_BIND__HOST
              value: 0.0.0.0
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.image.debug | quote }}
            {{- if .Values.taskmanager.extraEnvVars }}
              {{- include "common.tplvalues.render" (dict "value" .Values.taskmanager.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.taskmanager.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.taskmanager.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.taskmanager.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.taskmanager.extraEnvVarsSecret }}
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.taskmanager.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.taskmanager.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.taskmanager.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.taskmanager.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: rpc
          {{- end }}
          {{- if .Values.taskmanager.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.taskmanager.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.taskmanager.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.taskmanager.livenessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
              - pgrep
              - -f
              - taskmanager
          {{- end }}
          {{- if .Values.taskmanager.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.taskmanager.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.taskmanager.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.taskmanager.readinessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: rpc
          {{- end }}
          {{- end }}
          {{- if .Values.taskmanager.resources }}
          resources: {{- toYaml .Values.taskmanager.resources | nindent 12 }}
          {{- else if ne .Values.taskmanager.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.taskmanager.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/flink/conf
              subPath: app-conf-dir
            - name: empty-dir
              mountPath: /opt/bitnami/flink/log
              subPath: app-logs-dir
            # HACK: Workaround to bypass the libflink.sh persist_app logic
            - name: empty-dir
              mountPath: /bitnami/flink/conf
              subPath: app-conf-dir
            {{- if .Values.taskmanager.extraVolumeMounts }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.taskmanager.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.taskmanager.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.taskmanager.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.taskmanager.extraVolumes }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.taskmanager.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
