{{- $storageName := "cache-volume" }}
{{- $app := merge (deepCopy .Values.vmselect) (deepCopy .Values.common) -}}
{{- $pvc := $app.persistentVolume }}
{{- $_ := set .Values "vmselect" $app }}
{{- $mode := $app.mode }}
{{- if and $mode $app.enabled (hasKey $app $mode) -}}
{{- $modeOpts := index $app $mode }}
{{- $ctx := dict "helm" . "appKey" "vmselect" }}
{{- $fullname := include "vm.plain.fullname" $ctx }}
{{- $sa := include "vm.fullname" . }}
{{- $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 }}
  {{- if not $app.horizontalPodAutoscaler.enabled }}
  replicas: {{ $app.replicaCount }}
  {{- end }}
  {{- 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.podLabels" $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: {{ tpl ((.Values.serviceAccount).name | default $sa) $ctx }}
      automountServiceAccountToken: {{ .Values.serviceAccount.automountToken }}
      {{- end }}
      {{- if $app.podSecurityContext.enabled }}
      securityContext: {{ include "vm.securityContext" (dict "securityContext" $app.podSecurityContext "helm" .) | nindent 8 }}
      {{- end }}
      {{- with $app.initContainers }}
      initContainers: {{ toYaml . | nindent 8 }}
      {{- end }}
      {{- with ($app.imagePullSecrets | default .Values.global.imagePullSecrets) }}
      imagePullSecrets: {{ toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: vmselect
          {{- if $app.securityContext.enabled }}
          securityContext: {{ include "vm.securityContext" (dict "securityContext" $app.securityContext "helm" .) | nindent 12 }}
          {{- end }}
          image: {{ include "vm.image" $ctx }}
          imagePullPolicy: {{ $app.image.pullPolicy }}
          {{- with $app.lifecycle }}
          lifecycle: {{ . | toYaml | nindent 12 }}
          {{- end }}
          {{- with $app.containerWorkingDir }}
          workingDir: {{ . }}
          {{- end }}
          args: {{ include "vmselect.args" $ctx | nindent 12 }}
          {{- with $app.envFrom }}
          envFrom: {{ toYaml . | nindent 12 }}
          {{- end }}
          {{- with $app.env }}
          env: {{ toYaml . | nindent 12 }}
          {{- end }}
          ports:
            - name: {{ $app.ports.name | default "http" }}
              containerPort: {{ include "vm.port.from.flag" (dict "flag" $app.extraArgs.httpListenAddr "default" "8481") }}
            {{- with $app.extraArgs.clusternativeListenAddr }}
            - name: cluster-tcp
              protocol: TCP
              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:
            - mountPath: {{ $app.cacheMountPath }}
              name: {{ $storageName }}
            {{- range $app.extraHostPathMounts }}
            - name: {{ tpl .name $ctx }}
              mountPath: {{ .mountPath }}
              {{- with .subPath }}
              subPath: {{ . }}
              {{- end }}
              {{- with .readOnly }}
              readOnly: {{ . }}
              {{- end }}
            {{- end }}
            {{- with $app.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
            {{- include "vm.license.mount" . | nindent 12 }}
        {{- 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:
        {{- 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: {{ $pvc.existingClaim | default $fullname }}
          {{- 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 $ctx | default $storageName }}
        {{- 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 }}
        {{- with $pvc.volumeAttributeClassName }}
        volumeAttributesClassName: {{ . }}
        {{- end }}
        resources:
          requests:
            storage: {{ $pvc.size }}
        {{- with $pvc.storageClassName }}
        storageClassName: {{ ternary "" . (eq "-" .) }}
        {{- end }}
  {{- end }}
{{- if and $pvc.enabled (not $pvc.existingClaim) (eq $app.mode "deployment") }}
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: {{ tpl $pvc.name $ctx | default $fullname }}
  namespace: {{ $ns }}
  {{- $_ := set $ctx "extraLabels" $pvc.extraLabels }}
  labels: {{ include "vm.labels" $ctx | nindent 4 }}
  {{- $_ := unset $ctx "extraLabels" }}
  {{- with $pvc.annotations  }}
  annotations: {{ toYaml . | nindent 4 }}
  {{- end }}
spec:
  {{- with $pvc.accessModes }}
  accessModes: {{ toYaml . | nindent 4 }}
  {{- end }}
  {{- with $pvc.volumeAttributeClassName }}
  volumeAttributesClassName: {{ . }}
  {{- end }}
  resources:
    requests:
      storage: {{ $pvc.size }}
  {{- with $pvc.storageClassName }}
  storageClassName: {{ . }}
  {{- end }}
  {{- with $pvc.matchLabels }}
  selector:
    matchLabels: {{ toYaml . | nindent 6 }}
  {{- end }}
{{- end }}
{{- end }}
