apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "cluster-proxy.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "cluster-proxy.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "cluster-proxy.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "cluster-proxy.selectorLabels" . | nindent 8 }}
    spec:
      serviceAccountName: {{ include "cluster-proxy.serviceAccountName" . }}
      securityContext:
        {{ toYaml .Values.podSecurityContext | nindent 8 }}
      {{- if .Values.podHostNetwork }}
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      {{- end }}
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app.kubernetes.io/name
                  operator: In
                  values:
                  - {{ include "cluster-proxy.name" . }}
              namespaces:
              - {{ .Release.Namespace }}
              topologyKey: kubernetes.io/hostname
            weight: 100
          - podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app.kubernetes.io/name
                  operator: In
                  values:
                  - {{ include "cluster-proxy.name" . }}
              namespaces:
              - {{ .Release.Namespace }}
              topologyKey: topology.kubernetes.io/zone
            weight: 100
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{ toYaml .Values.securityContext | nindent 12 }}
          ports:
          - containerPort: 80
          - containerPort: 8081
            name: metrics
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          command:
          - /proxy
          - --port
          - {{ .Values.service.port | quote }}
          {{- if .Values.deployment.proxyArgs.enableDevMode }}
          - --dev
          {{- end }}
          - --gotty-image
          - "{{ .Values.gotty.repository }}:{{ .Values.gotty.tag }}"
          - --gotty-cmd
          - /gotty -w --max-connection 1 --timeout 60 --reconnect /usql --password
          {{- with .Values.env }}
          env:
            {{- toYaml . | nindent 10 }}
          {{- end }}
