apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "karpenter.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "karpenter.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "karpenter.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "karpenter.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "karpenter.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}{{ if .Values.image.digest }}@{{ .Values.image.digest }}{{ end }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: IBM_API_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "karpenter.fullname" . }}-credentials
                  key: IBM_API_KEY
            - name: IBM_REGION
              valueFrom:
                secretKeyRef:
                  name: {{ include "karpenter.fullname" . }}-credentials
                  key: region
            - name: VPC_URL
              valueFrom:
                secretKeyRef:
                  name: {{ include "karpenter.fullname" . }}-credentials
                  key: vpc_url
            - name: VPC_AUTH_TYPE
              valueFrom:
                secretKeyRef:
                  name: {{ include "karpenter.fullname" . }}-credentials
                  key: vpc_auth_type
            - name: VPC_APIKEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "karpenter.fullname" . }}-credentials
                  key: VPC_APIKEY
          ports:
            - name: metrics
              containerPort: {{ .Values.controller.metrics.port }}
              protocol: TCP
            - name: health
              containerPort: 8081
              protocol: TCP
          {{- with .Values.controller.livenessProbe }}
          livenessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.controller.readinessProbe }}
          readinessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
