{{- $storageName := "server-volume" }}
{{- $app := .Values.server }}
{{- $pvc := $app.persistentVolume }}
{{- $mode := $app.mode }}
{{- if and $mode $app.enabled (hasKey $app $mode) }}
{{- $modeOpts := index $app $mode }}
{{- $ctx := dict "helm" . "appKey" "server" }}
{{- $fullname := include "vm.plain.fullname" $ctx }}
{{- $ns := include "vm.namespace" $ctx }}
apiVersion: apps/v1
kind: {{ title $mode }}
metadata:
  name: {{ $fullname }}
  namespace: {{ $ns }}
  {{- $_ := set $ctx "extraLabels" $app.extraLabels }}
  labels: {{ include "vm.labels" $ctx | nindent 4 }}
  {{- $_ := unset $ctx "extraLabels" }}
  {{- with $app.annotations }}
  annotations: {{ toYaml . | nindent 4 }}
  {{- end }}
spec:
  {{- with $modeOpts.spec }}
  {{- toYaml . | nindent 2 }}
  {{- end }}
  replicas: {{ $app.replicaCount }}
  {{- if eq $mode "statefulSet" }}
  serviceName: {{ $fullname }}
  {{- end }}
  selector:
    matchLabels: {{ include "vm.selectorLabels" $ctx | nindent 6 }}
  template:
    metadata:
      {{- $_ := set $ctx "extraLabels" $app.podLabels }}
      labels: {{ include "vm.labels" $ctx | nindent 8 }}
      {{- $_ := unset $ctx "extraLabels" }}
      {{- with $app.podAnnotations }}
      annotations: {{ toYaml . | nindent 8 }}
      {{- end }}
    spec:
      {{- with $app.priorityClassName }}
      priorityClassName: {{ . }}
      {{- end }}
      {{- with $app.schedulerName }}
      schedulerName: {{ . }}
      {{- end }}
      {{- if or (.Values.serviceAccount).name (.Values.serviceAccount).create }}
      serviceAccountName: {{ (.Values.serviceAccount).name | default $fullname }}
      automountServiceAccountToken: {{ .Values.serviceAccount.automountToken }}
      {{- end }}
      {{- if $app.podSecurityContext.enabled }}
      securityContext: {{ include "vm.securityContext" (dict "securityContext" $app.podSecurityContext "helm" .) | nindent 8 }}
      {{- end }}
      {{- with ($app.imagePullSecrets | default .Values.global.imagePullSecrets) }}
      imagePullSecrets: {{ toYaml . | nindent 8 }}
      {{- end }}
      {{- $manager := $app.vmbackupmanager }}
      {{- if or $app.initContainers $manager.restore.onStart.enabled }}
      initContainers:
      {{- with $app.initContainers -}}
      {{- toYaml . | nindent 6 }}
      {{- end -}}
      {{- if $manager.restore.onStart.enabled }}
      {{- include "vm.enterprise.only" . }}
      - name: vmbackupmanager-restore
        {{- $_ := set $ctx "appKey" (list "server" "vmbackupmanager") }}
        image: {{ include "vm.image" $ctx }}
        imagePullPolicy: {{ $app.image.pullPolicy }}
        {{- with $app.podSecurityContext }}
        securityContext:  {{ toYaml . | nindent 10 }}
        {{- end }}
        args: {{ include "vmbackupmanager.restore.args" $ctx | nindent 12 }}
        {{- with $manager.resources }}
        resources: {{ toYaml . | nindent 10 }}
        {{- end }}
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        {{- with $manager.env }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        ports:
        - name: manager-http
          containerPort: 8300
        volumeMounts:
        - name: vmstorage-volume
          mountPath: {{ $pvc.mountPath }}
          subPath: {{ $pvc.subPath }}
        {{- range $manager.extraSecretMounts }}
        - name: {{ .name }}
          mountPath: {{ .mountPath }}
          subPath: {{ .subPath }}
        {{- end }}
      {{- end }}
      {{- end }}
      containers:
        - name: vmsingle
          {{- if $app.securityContext.enabled }}
          securityContext: {{ include "vm.securityContext" (dict "securityContext" $app.securityContext "helm" .) | nindent 12 }}
          {{- end }}
          {{- $_ := set $ctx "appKey" "server" }}
          image: {{ include "vm.image" $ctx }}
          imagePullPolicy: {{ $app.image.pullPolicy }}
          {{- if $app.containerWorkingDir }}
          workingDir: {{ $app.containerWorkingDir }}
          {{- end }}
          args: {{ include "vmsingle.args" $ctx | nindent 12 }}
          {{- with $app.envFrom }}
          envFrom: {{ toYaml . | nindent 12 }}
          {{- end }}
          {{- with $app.env }}
          env: {{ toYaml . | nindent 12 }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ include "vm.port.from.flag" (dict "flag" $app.extraArgs.httpListenAddr "default" "8428") }}
            {{- with $app.extraArgs.graphiteListenAddr }}
            - name: graphite-tcp
              protocol: TCP
              containerPort: {{ include "vm.port.from.flag" (dict "flag" .) }}
            - name: graphite-udp
              protocol: UDP
              containerPort: {{ include "vm.port.from.flag" (dict "flag" .) }}
            {{- end }}
            {{- with $app.extraArgs.influxListenAddr }}
            - name: influx-tcp
              protocol: TCP
              containerPort: {{ include "vm.port.from.flag" (dict "flag" .) }}
            - name: influx-udp
              protocol: UDP
              containerPort: {{ include "vm.port.from.flag" (dict "flag" .) }}
            {{- end }}
            {{- with $app.extraArgs.opentsdbHTTPListenAddr }}
            - name: opentsdbhttp
              protocol: TCP
              containerPort: {{ include "vm.port.from.flag" (dict "flag" .) }}
            {{- end }}
            {{- with $app.extraArgs.opentsdbListenAddr }}
            - name: opentsdb-tcp
              protocol: TCP
              containerPort: {{ include "vm.port.from.flag" (dict "flag" .) }}
            - name: opentsdb-udp
              protocol: UDP
              containerPort: {{ include "vm.port.from.flag" (dict "flag" .) }}
            {{- end }}
          {{- with (fromYaml (include "vm.probe" (dict "app" $app "type" "readiness"))) }}
          readinessProbe: {{ toYaml . | nindent 12 }}
          {{- end }}
          {{- with (fromYaml (include "vm.probe" (dict "app" $app "type" "liveness"))) }}
          livenessProbe: {{ toYaml . | nindent 12 }}
          {{- end }}
          {{- with (fromYaml (include "vm.probe" (dict "app" $app "type" "startup"))) }}
          startupProbe: {{ toYaml . | nindent 12 }}
          {{- end }}
          {{- with $app.resources }}
          resources: {{ toYaml . | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- with $pvc }}
            - name: {{ $storageName }}
              mountPath: {{ .mountPath }}
              {{- with .subPath }}
              subPath: {{ . }}
              {{- end }}
            {{- end }}
            {{- if $app.scrape.enabled }}
            - name: scrapeconfig
              mountPath: /scrapeconfig
            {{- end }}
            {{- if $app.relabel.enabled }}
            - name: relabelconfig
              mountPath: /relabelconfig
            {{- end }}
            {{- range $app.extraHostPathMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              {{- with .subPath }}
              subPath: {{ . }}
              {{- end }}
              {{- with .readOnly }}
              readOnly: {{ . }}
              {{- end }}
            {{- end }}
            {{- range $app.extraConfigmapMounts }}
            - name: {{ $app.name }}-{{ .name }}
              mountPath: {{ .mountPath }}
              {{- with .subPath }}
              subPath: {{ . }}
              {{- end }}
              {{- with .readOnly }}
              readOnly: {{ . }}
              {{- end }}
            {{- end }}
            {{- range $app.extraSecretMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              {{- with .subPath }}
              subPath: {{ . }}
              {{- end }}
              {{- with .readOnly }}
              readOnly: {{ . }}
              {{- end }}
            {{- end }}
            {{- include "vm.license.mount" . | nindent 12 }}
            {{- with $app.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
        {{- if $manager.enabled }}
        {{- include "vm.enterprise.only" . }}
        - name: vmbackupmanager
          {{- $_ := set $ctx "appKey" (list "server" "vmbackupmanager") }}
          image: {{ include "vm.image" $ctx }}
          imagePullPolicy: {{ $app.image.pullPolicy }}
          args: {{ include "vmbackupmanager.args" $ctx | nindent 12 }}
          {{- with (fromYaml (include "vm.probe" (dict "app" $manager "type" "readiness"))) }}
          readinessProbe: {{ toYaml . | nindent 12 }}
          {{- end }}
          {{- with (fromYaml (include "vm.probe" (dict "app" $manager "type" "liveness"))) }}
          livenessProbe: {{ toYaml . | nindent 12 }}
          {{- end }}
          {{- with (fromYaml (include "vm.probe" (dict "app" $manager "type" "startup"))) }}
          startupProbe: {{ toYaml . | nindent 12 }}
          {{- end }}
          {{- with $manager.resources }}
          resources: {{ toYaml . | nindent 12 }}
          {{- end }}
          {{- with $manager.env }}
          env: {{ toYaml . | nindent 12 }}
          {{- end }}
          ports:
            - name: manager-http
              containerPort: 8300
          volumeMounts:
            {{- with $pvc }}
            - name: {{ $storageName }}
              mountPath: {{ .mountPath }}
              {{- with .subPath }}
              subPath: {{ . }}
              {{- end }}
            {{- end }}
            {{- with $manager.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
            {{- include "vm.license.mount" . | nindent 12 }}
        {{- end }}
        {{- with $app.extraContainers }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- with $app.nodeSelector }}
      nodeSelector: {{ toYaml . | nindent 8 }}
      {{- end }}
      {{- with $app.tolerations }}
      tolerations: {{ toYaml . | nindent 8 }}
      {{- end }}
      {{- with $app.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- range $constraint := . }}
        - {{ toYaml $constraint | nindent 10 | trim }}
          {{- if not $constraint.labelSelector }}
          labelSelector:
            matchLabels: {{ include "vm.selectorLabels" $ctx | nindent 14 }}
          {{- end }}
        {{- end }}
      {{- end }}
      {{- with $app.affinity }}
      affinity: {{ toYaml . | nindent 8 }}
      {{- end }}
      terminationGracePeriodSeconds: {{ $app.terminationGracePeriodSeconds }}
      volumes:
        {{- if $app.scrape.enabled }}
        - name: scrapeconfig
          configMap:
            name: {{ include "vmsingle.scrape.config.name" $ctx }}
        {{- end }}
        {{- if $app.relabel.enabled }}
        - name: relabelconfig
          configMap:
            name: {{ include "vmsingle.relabel.config.name" $ctx }}
        {{- end }}
        {{- with $app.extraVolumes }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- if or (eq $mode "deployment") (not $pvc.enabled) $pvc.existingClaim }}
        - name: {{ $storageName }}
          {{- if or (and (eq $mode "deployment") $pvc.enabled) $pvc.existingClaim }}
          persistentVolumeClaim:
            claimName: {{ tpl ($pvc.existingClaim | default $pvc.name | default $fullname) $ctx }}
          {{- else }}
          emptyDir: {{ toYaml $app.emptyDir | nindent 12 }}
          {{- end }}
        {{- end }}
        {{- include "vm.license.volume" . | nindent 8 }}
  {{- if and (eq $mode "statefulSet") $pvc.enabled (not $pvc.existingClaim) }}
  volumeClaimTemplates:
    - apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: {{ tpl ($pvc.name | default $storageName) $ctx }}
        {{- with $pvc.annotations }}
        annotations: {{ toYaml . | nindent 10 }}
        {{- end }}
        {{- with $pvc.extraLabels }}
        labels: {{ toYaml . | nindent 10 }}
        {{- end }}
      spec:
        {{- with $pvc.accessModes }}
        accessModes: {{ toYaml . | nindent 10 }}
        {{- end }}
        resources:
          requests:
            storage: {{ $pvc.size }}
        {{- with $pvc.storageClassName }}
        storageClassName: {{ ternary "" . (eq "-" .) }}
        {{- end }}
        {{- with $pvc.matchLabels }}
        selector:
          matchLabels: {{ toYaml . | nindent 12 }}
        {{- end }}
  {{- end }}
{{- end }}
