apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "omada-controller.fullname" . }}
  labels:
    {{- include "omada-controller.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
{{- end }}
  selector:
    matchLabels:
      {{- include "omada-controller.selectorLabels" . | nindent 6 }}
  template:
    metadata:
    {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      labels:
        {{- include "omada-controller.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "omada-controller.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 }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: TZ
              value: {{ .Values.TZ | quote }}
          ports:
            - name: manage
              containerPort: 8088
              protocol: TCP
            - name: managessl
              containerPort: 8043
              protocol: TCP
            - name: portalssl
              containerPort: 8843
              protocol: TCP
            - name: eapdiscovery
              containerPort: 29810
              protocol: UDP
            - name: eapmanage
              containerPort: 29811
              protocol: TCP
            - name: eapadopt
              containerPort: 29812
              protocol: TCP
            - name: eapupgrade
              containerPort: 29813
              protocol: TCP
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - name: work
              mountPath: /opt/tplink/EAPController/work
            - name: data
              mountPath: /opt/tplink/EAPController/data
            - name: logs
              mountPath: /opt/tplink/EAPController/logs
      volumes:
        - name: work
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ include "omada-controller.fullname" . }}-work
        {{- else }}
          emptyDir: {}
        {{- end }}
        - name: data
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ include "omada-controller.fullname" . }}-data
        {{- else }}
          emptyDir: {}
        {{- end }}
        - name: logs
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ include "omada-controller.fullname" . }}-logs
        {{- else }}
          emptyDir: {}
        {{- 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 }}
      {{- if .Values.hostNetwork }}
      hostNetwork: {{ .Values.hostNetwork }}
      {{- end }}
