apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "deepflow.fullname" . }}-app
  labels:
    {{- include "deepflow-app.labels" . | nindent 4 }}
spec:
  replicas: {{ tpl (toString .Values.app.replicas) . }}
  selector:
    matchLabels:
      {{- include "deepflow-app.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-app.selectorLabels" . | nindent 8 }}
        {{- with .Values.app.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      hostNetwork: {{ tpl .Values.app.hostNetwork . }}
      dnsPolicy: {{ tpl .Values.app.dnsPolicy . }}
      imagePullSecrets:
      {{- with .Values.global.imagePullSecrets }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.imagePullSecrets }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: deepflow-app
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ tpl .Values.image.app.repository . }}:{{ tpl .Values.image.app.tag . }}"
          imagePullPolicy: "{{ tpl .Values.image.app.pullPolicy . }}"
          ports:
            - name: app
              containerPort: 20418
              protocol: TCP
          livenessProbe:
            {{ toYaml .Values.app.livenessProbe | nindent 12 }}
          readinessProbe:
            {{ toYaml .Values.app.readinessProbe | nindent 12 }}
          resources:
            {{- toYaml .Values.app.resources | nindent 12 }}
          volumeMounts:
          - name: app-config
            mountPath: /etc/deepflow/app.yaml
            subPath: app.yaml
          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: TZ
            value: "{{ tpl .Values.timezone . }}"
      volumes:
        - name: app-config
          configMap:
            name: {{ include "deepflow.fullname" . }}
            items:
            - key: app.yaml
              path: app.yaml
      {{- with .Values.app.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      affinity:
      {{- include "appNodeaffinity" . | indent 6 }}
      {{- include "appPodAffinity" . | indent 6 }}
      {{- include "appPodAntiAffinity" . | 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 }}