{{- define "storage-values" }}

{{- with index .Values "storage" "lvm-localpv" }}
lvm-localpv:
  {{- toYaml . | nindent 2 }}
  prometheus:
    enabled: {{ $.Values.metrics.enabled }}
{{- end }}

{{- with .Values.storage.gemini }}
gemini:
  {{- toYaml . | nindent 2 }}
{{- end }}

{{- with .Values.storage.k8up }}
k8up:
  {{- toYaml . | nindent 2 }}
{{- end }}

snapshotController:
  # install controller if any capable storage driver is enabled
  enabled: {{ or (index .Values "storage" "lvm-localpv" "enabled") (index .Values "storage" "aws-ebs-csi-driver" "enabled")}}

{{- if eq .Values.global.platform "aws" }}
aws-ebs-csi-driver:
  enabled: {{ default false (index .Values "storage" "aws-ebs-csi-driver" "enabled")}}
  controller:
    replicaCount: {{ ternary 2 1 .Values.global.highAvailable }}
    {{- if not .Values.global.highAvailable }}
    podDisruptionBudget:
      enabled: false
    {{- end }}
    k8sTagClusterId: {{ .Values.global.clusterName }}
    enableMetrics: {{ .Values.metrics.enabled }}
    region: {{ .Values.global.aws.region }}
    env:
    - name: AWS_ROLE_ARN
      value: {{ index .Values "storage" "aws-ebs-csi-driver" "IamArn" | quote }}
    - name: AWS_WEB_IDENTITY_TOKEN_FILE
      value: "/var/run/secrets/sts.amazonaws.com/serviceaccount/token"
    - name: AWS_STS_REGIONAL_ENDPOINTS
      value: regional

  node:
    enableMetrics: {{ .Values.metrics.enabled }}

  {{- if not .Values.global.highAvailable }}
  sidecars:
    provisioner:
      leaderElection:
        enabled: false
    attacher:
      leaderElection:
        enabled: false
    resizer:
      leaderElection:
        enabled: false
    volumemodifier:
      leaderElection:
        enabled: false
  {{- end }}

aws-efs-csi-driver:
  enabled: {{ default false (index .Values "storage" "aws-efs-csi-driver" "enabled")}}
  controller:
    replicaCount: {{ ternary 2 1 .Values.global.highAvailable }}
    {{- if .Values.global.highAvailable }}
    podDisruptionBudget:
      enabled: true
    {{- end }}

    {{- with index .Values "storage" "aws-efs-csi-driver" "IamArn" }}
    extraEnv:
    - name: AWS_ROLE_ARN
      value: {{ . | quote }}
    - name: AWS_WEB_IDENTITY_TOKEN_FILE
      value: "/var/run/secrets/sts.amazonaws.com/serviceaccount/token"
    {{- end }}

    # The EFS controller steel needs hostnetwork and cannot update on single node control planes otherwise
    {{- if not .Values.global.highAvailable }}
    updateStrategy:
      type: Recreate
    {{- else }}
    affinity:
      podAntiAffinity:
        requiredDuringSchedulingIgnoredDuringExecution:
        - labelSelector:
            matchExpressions:
            - key: app
              operator: In
              values:
              - efs-csi-controller
          topologyKey: "kubernetes.io/hostname"
    {{- end }}

  # Dynamic Provisioning
  {{- with index .Values "storage" "aws-efs-csi-driver" "storageClasses" }}
  storageClasses:
  {{- toYaml . | nindent 4 }}
  {{- end }}
  # Legacy static provisioning
  {{- with index .Values "storage" "aws-efs-csi-driver" "PersistentVolumes" }}
  PersistentVolumes:
  {{- toYaml . | nindent 4 }}
  {{- end }}

{{- end }}

{{- end }}

{{- define "storage-argo" }}
{{- end }}

{{ include "kubezero-app.app" . }}
