apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "headlamp.name" . }}
  labels:
    {{- include "headlamp.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "headlamp.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "headlamp.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "headlamp.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ include "headlamp.name" . }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{ if .Values.config.oidc.enabled }}
          env:
          {{- if .Values.extraVolumeMounts }}
          - name: NODE_EXTRA_CA_CERTS
            value: {{ (index $.Values.extraVolumeMounts 0).mountPath }}
          {{- end }}
          - name: CLIENT_SECRET
            valueFrom:
              secretKeyRef:
                name: {{ .Values.config.oidc.clientSecretName }}
                key: {{ .Values.config.oidc.clientSecretKey }}
          {{ end }}
          args:
            - "-in-cluster"
            - "-plugins-dir=/headlamp/plugins"
            {{ if .Values.config.oidc.enabled }}
            {{- with .Values.config.oidc.clientID }}
            - "-oidc-client-id={{ . }}"
            {{- end }}
            - "-oidc-client-secret=$(CLIENT_SECRET)"
            - "-oidc-idp-issuer-url={{ .Values.config.oidc.issuerUrl }}"
            {{- with .Values.config.oidc.scopes }}
            - "-oidc-scopes={{ . }}"
            {{- end }}
            {{ end }}
            {{- with .Values.config.baseURL }}
            - "-base-url={{ . }}"
            {{- end }}
          ports:
            - name: http
              containerPort: 4466
              protocol: TCP
          {{- if .Values.livenessProbe }}
          livenessProbe:
{{ toYaml .Values.livenessProbe | indent 12 }}
          {{- end }}
          {{- if .Values.readinessProbe }}
          readinessProbe:
{{ toYaml .Values.readinessProbe | indent 12 }}
          {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
          {{- if .Values.extraVolumeMounts }}
            {{- toYaml .Values.extraVolumeMounts | nindent 12 }}
          {{- end }}
      volumes:
      {{- if .Values.extraVolumes }}
        {{- toYaml .Values.extraVolumes | nindent 8 }}
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
