{{- if .Values.localpv.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "localpv.fullname" . }}
  {{- with .Values.localpv.annotations }}
  annotations: {{ toYaml . | nindent 4 }}
  {{- end }}
  labels:
    {{- include "localpv.labels" . | nindent 4 }}
    {{- if .Values.extraLabels -}}
      {{- toYaml .Values.extraLabels | nindent 4 }}
    {{- end }}
spec:
  replicas: {{ .Values.localpv.replicas }}
  strategy:
    type: "Recreate"
    rollingUpdate: null
  selector:
    matchLabels:
      {{- include "localpv.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.localpv.podAnnotations }}
      annotations: {{ toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "localpv.labels" . | nindent 8 }}
        {{- if .Values.extraLabels -}}
          {{- toYaml .Values.extraLabels | nindent 8 }}
        {{- end }}
        {{- with .Values.localpv.podLabels }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.loggingLabels}}
          {{ toYaml . | nindent 8 -}}
        {{- end}}
    spec:
    {{- if .Values.localpv.priorityClassName }}
      priorityClassName: {{ tpl .Values.localpv.priorityClassName . }}
    {{- end }}
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ template "localpv.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
      - name: {{ template "localpv.fullname" . }}
        image: "{{ with .Values.localpv.image.registry | default .Values.global.imageRegistry | trimSuffix "/" }}{{ . }}/{{ end }}{{ .Values.localpv.image.repository }}:{{ .Values.localpv.image.tag }}"
        imagePullPolicy: {{ .Values.localpv.image.pullPolicy }}
        resources:
{{ toYaml .Values.localpv.resources | indent 10 }}
        env:
        # OPENEBS_IO_K8S_MASTER enables openebs provisioner to connect to K8s
        # based on this address. This is ignored if empty.
        # This is supported for openebs provisioner version 0.5.2 onwards
        #- name: OPENEBS_IO_K8S_MASTER
        #  value: "http://10.128.0.12:8080"
        # OPENEBS_IO_KUBE_CONFIG enables openebs provisioner to connect to K8s
        # based on this config. This is ignored if empty.
        # This is supported for openebs provisioner version 0.5.2 onwards
        #- name: OPENEBS_IO_KUBE_CONFIG
        #  value: "/home/ubuntu/.kube/config"
        - name: OPENEBS_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        # OPENEBS_SERVICE_ACCOUNT provides the service account of this pod as
        # environment variable
        - name: OPENEBS_SERVICE_ACCOUNT
          valueFrom:
            fieldRef:
              fieldPath: spec.serviceAccountName
        # OPENEBS_IO_BASE_PATH is the environment variable that provides the
        # default base path on the node where host-path PVs will be provisioned.
        - name: OPENEBS_IO_ENABLE_ANALYTICS
          value: "{{ .Values.analytics.enabled }}"
        {{- if .Values.analytics.gaId }}
        - name: GA_ID
          value: {{ .Values.analytics.gaId | quote }}
        {{- end }}
        {{- if .Values.analytics.gaKey }}
        - name: GA_KEY
          value: {{ .Values.analytics.gaKey | quote }}
        {{- end }}
        - name: OPENEBS_IO_BASE_PATH
          value: "{{ .Values.localpv.basePath }}"
        - name: OPENEBS_IO_HELPER_IMAGE
          value: "{{ with .Values.helperPod.image.registry | default .Values.global.imageRegistry | trimSuffix "/" }}{{ . }}/{{ end }}{{ .Values.helperPod.image.repository }}:{{ .Values.helperPod.image.tag }}"
        - name: OPENEBS_IO_HELPER_POD_HOST_NETWORK
          value: "{{ .Values.helperPod.hostNetwork }}"
        - name: OPENEBS_IO_INSTALLER_TYPE
          value: "localpv-charts-helm"
        # LEADER_ELECTION_ENABLED is used to enable/disable leader election. By default
        # leader election is enabled.
        - name: LEADER_ELECTION_ENABLED
          value: "{{ .Values.localpv.enableLeaderElection }}"
{{- if .Values.imagePullSecrets }}
        - name: OPENEBS_IO_IMAGE_PULL_SECRETS
          value: "{{- range $index, $secret := .Values.imagePullSecrets}}{{if $index}},{{end}}{{ $secret.name }}{{- end}}"
{{- end }}
        # Process name used for matching is limited to the 15 characters
        # present in the pgrep output.
        # So fullname can't be used here with pgrep (>15 chars).A regular expression
        # that matches the entire command name has to specified.
        # Anchor `^` : matches any string that starts with `provisioner-loc`
        # `.*`: matches any string that has `provisioner-loc` followed by zero or more char
        livenessProbe:
          exec:
            command:
            - sh
            - -c
            - test `pgrep -c "^provisioner-loc.*"` = 1
          initialDelaySeconds: {{ .Values.localpv.healthCheck.initialDelaySeconds }}
          periodSeconds: {{ .Values.localpv.healthCheck.periodSeconds }}
{{- if .Values.localpv.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.localpv.nodeSelector | indent 8 }}
{{- end }}
{{- if $tolerations := include "tolerations_with_early_eviction" . }}
      tolerations: {{ $tolerations }}
{{- end }}
{{- if .Values.localpv.affinity }}
      affinity:
{{ toYaml .Values.localpv.affinity | indent 8 }}
{{- end }}
{{- end }}
