{{- if .Values.controlPlane.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "hindsight.fullname" . }}-control-plane
  labels:
    {{- include "hindsight.controlPlane.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.controlPlane.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "hindsight.controlPlane.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        {{- if not .Values.existingSecret }}
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
        {{- end }}
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "hindsight.controlPlane.selectorLabels" . | nindent 8 }}
    spec:
      {{- if .Values.serviceAccount.create }}
      serviceAccountName: {{ include "hindsight.serviceAccountName" . }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
      - name: control-plane
        securityContext:
          {{- toYaml .Values.securityContext | nindent 10 }}
        image: "{{ .Values.controlPlane.image.repository }}:{{ .Values.controlPlane.image.tag | default .Values.version | default .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.controlPlane.image.pullPolicy }}
        ports:
        - name: http
          containerPort: {{ .Values.controlPlane.service.targetPort }}
          protocol: TCP
        {{- if .Values.existingSecret }}
        envFrom:
        - secretRef:
            name: {{ .Values.existingSecret }}
        {{- end }}
        env:
        - name: HINDSIGHT_CP_DATAPLANE_API_URL
          value: {{ include "hindsight.apiUrl" . | quote }}
        {{- range $key, $value := .Values.controlPlane.env }}
        - name: {{ $key }}
          value: {{ $value | quote }}
        {{- end }}
        {{- /* Only use controlPlane.secrets when not using existingSecret (for chart-managed secrets) */}}
        {{- if not .Values.existingSecret }}
        {{- range $key, $value := .Values.controlPlane.secrets }}
        - name: {{ $key }}
          valueFrom:
            secretKeyRef:
              name: {{ include "hindsight.secretName" $ }}
              key: {{ $key }}
        {{- end }}
        {{- end }}
        livenessProbe:
          {{- toYaml .Values.controlPlane.livenessProbe | nindent 10 }}
        readinessProbe:
          {{- toYaml .Values.controlPlane.readinessProbe | nindent 10 }}
        resources:
          {{- toYaml .Values.controlPlane.resources | nindent 10 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with (.Values.controlPlane.affinity | default .Values.affinity) }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
{{- end }}
