apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "deepflow.fullname" . }}-server
  labels:
    {{- include "deepflow-server.labels" . | nindent 4 }}
spec:
  replicas: {{ tpl (toString .Values.server.replicas) . }}
  strategy:
    type: Recreate
  selector:
    matchLabels:
      {{- include "deepflow-server.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        checksum/customConfig: {{ sha256sum (print (tpl (toYaml .Values.configmap) $)) }}
      labels:
        {{- include "deepflow-server.selectorLabels" . | nindent 8 }}
        {{- range $index, $values := .Values.server.featureFlag }}
        {{- (printf "deepflow.yunshan.net/%s" (lower $values)) | quote | nindent 8 }}: "true"
        {{- end }}
        {{- with .Values.server.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      hostNetwork: {{ tpl .Values.server.hostNetwork . }}
      dnsPolicy: {{ tpl .Values.server.dnsPolicy . }}
      imagePullSecrets:
      {{- with .Values.global.imagePullSecrets }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.imagePullSecrets }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      serviceAccountName: {{ include "deepflow.fullname" . }}-server
      containers:
        - name: deepflow-server
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ tpl .Values.image.server.repository . }}:{{ tpl (toString .Values.image.server.tag) . }}"
          imagePullPolicy: "{{ tpl .Values.image.server.pullPolicy . }}"
          ports:
            - name: server
              containerPort: 20417
              protocol: TCP
            - containerPort: 20035
              name: grpc
              protocol: TCP
            - containerPort: 20135
              name: ssl-grpc
              protocol: TCP
            - containerPort: 20416
              name: querier
              protocol: TCP
          livenessProbe:
            {{ toYaml .Values.server.livenessProbe | nindent 12 }}
          readinessProbe:
            {{ toYaml .Values.server.readinessProbe | nindent 12 }}
          resources:
            {{- toYaml .Values.server.resources | nindent 12 }}
          volumeMounts:
          - name: server-config
            mountPath: /etc/server.yaml
            subPath: server.yaml
        {{- range .Values.server.extraVolumeMounts }}
          - name: {{ .name }}
            mountPath: {{ .mountPath }}
            subPath: {{ .subPath | default "" }}
            readOnly: {{ .readOnly }}
        {{- end }}
          env:
          - name: K8S_NODE_IP_FOR_DEEPFLOW
            valueFrom:
              fieldRef:
                fieldPath: status.hostIP
          - name: K8S_NODE_NAME_FOR_DEEPFLOW
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          - name: K8S_POD_NAME_FOR_DEEPFLOW
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: K8S_POD_IP_FOR_DEEPFLOW
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
          - name: K8S_NAMESPACE_FOR_DEEPFLOW
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: TZ
            value: "{{ tpl .Values.timezone . }}"
          {{- range $index, $values := .Values.server.featureFlag }}
          - name: {{ printf "FEATURE_FLAG_%s" $values | quote }}
            value: "true"
          {{- end }}
      volumes:
        - name: server-config
          configMap:
            name: {{ include "deepflow.fullname" . }}
            items:
            - key: server.yaml
              path: server.yaml
      {{- range .Values.server.extraVolumeMounts }}
        - name: {{ .name }}
          {{- if .existingClaim }}
          persistentVolumeClaim:
            claimName: {{ .existingClaim }}
          {{- else if .hostPath }}
          hostPath:
            path: {{ .hostPath }}
          {{- else }}
          emptyDir: {}
          {{- end }}
      {{- end }}
      {{- with .Values.server.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      affinity:
      {{- include "serverNodeaffinity" . | indent 6 }}
      {{- include "serverPodAffinity" . | indent 6 }}
      {{- include "serverPodAntiAffinity" . | indent 6 }}
      {{- if or .Values.global.tolerations .Values.tolerations }}
      tolerations:
      {{- if .Values.global.tolerations }}
      {{- toYaml .Values.global.tolerations | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      {{- toYaml .Values.tolerations | nindent 8 }}
      {{- end }}
      {{- end }}