apiVersion: apps/v1
kind: Deployment
metadata:
  name: "{{ include "aws-calico.fullname" . }}-typha"
  labels:
    app.kubernetes.io/name: "{{ include "aws-calico.fullname" . }}-typha"
{{ include "aws-calico.labels" . | indent 4 }}
spec:
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app.kubernetes.io/name: "{{ include "aws-calico.fullname" . }}-typha"
  template:
    metadata:
      labels:
        app.kubernetes.io/name: "{{ include "aws-calico.fullname" . }}-typha"
      annotations:
        cluster-autoscaler.kubernetes.io/safe-to-evict: 'true'
    spec:
      priorityClassName: system-cluster-critical
      nodeSelector:
        {{- toYaml .Values.calico.typha.nodeSelector | nindent 8 }}
      tolerations:
        # Mark the pod as a critical add-on for rescheduling.
        - key: CriticalAddonsOnly
          operator: Exists
      {{- if .Values.calico.typha.tolerations }}
{{ toYaml .Values.calico.typha.tolerations | indent 10 }}
      {{- end }}
      hostNetwork: true
      serviceAccountName: "{{ include "aws-calico.serviceAccountName" . }}-node"
      # fsGroup allows using projected serviceaccount tokens as described here kubernetes/kubernetes#82573
      securityContext:
        fsGroup: 65534
      containers:
        - image: "{{ .Values.calico.typha.image }}:{{ .Values.calico.tag }}"
          name: calico-typha
          ports:
            - containerPort: 5473
              name: calico-typha
              protocol: TCP
          env:
            # Use eni not cali for interface prefix
            - name: FELIX_INTERFACEPREFIX
              value: "eni"
            - name: TYPHA_LOGFILEPATH
              value: "none"
            - name: TYPHA_LOGSEVERITYSYS
              value: "none"
            - name: TYPHA_LOGSEVERITYSCREEN
              value: "{{ .Values.calico.typha.logseverity }}"
            - name: TYPHA_PROMETHEUSMETRICSENABLED
              value: "true"
            - name: TYPHA_CONNECTIONREBALANCINGMODE
              value: "kubernetes"
            - name: TYPHA_PROMETHEUSMETRICSPORT
              value: "9093"
            - name: TYPHA_DATASTORETYPE
              value: "kubernetes"
            - name: TYPHA_MAXCONNECTIONSLOWERLIMIT
              value: "1"
            - name: TYPHA_HEALTHENABLED
              value: "true"
            # This will make Felix honor AWS VPC CNI's mangle table
            # rules.
            - name: FELIX_IPTABLESMANGLEALLOWACTION
              value: Return
          livenessProbe:
            httpGet:
              path: /liveness
              port: 9098
              host: localhost
            periodSeconds: 30
            initialDelaySeconds: 30
          securityContext:
            runAsNonRoot: true
            allowPrivilegeEscalation: false
          readinessProbe:
            httpGet:
              path: /readiness
              port: 9098
              host: localhost
            periodSeconds: 10
          resources:
            {{- toYaml .Values.calico.typha.resources | nindent 12 }}

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: "{{ include "aws-calico.fullname" . }}-typha-horizontal-autoscaler"
  labels:
    app.kubernetes.io/name: "{{ include "aws-calico.fullname" . }}-typha-autoscaler"
{{ include "aws-calico.labels" . | indent 4 }}
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: "{{ include "aws-calico.fullname" . }}-typha-autoscaler"
  replicas: 1
  template:
    metadata:
      labels:
        app.kubernetes.io/name: "{{ include "aws-calico.fullname" . }}-typha-autoscaler"
    spec:
      priorityClassName: system-cluster-critical
      nodeSelector:
        {{- toYaml .Values.calico.typha_autoscaler.nodeSelector | nindent 8 }}
      tolerations:
        # Mark the pod as a critical add-on for rescheduling.
        - key: CriticalAddonsOnly
          operator: Exists
      {{- if .Values.calico.typha_autoscaler.tolerations }}
{{ toYaml .Values.calico.typha_autoscaler.tolerations | indent 10 }}
      {{- end }}
      containers:
        - image: "{{ .Values.autoscaler.image }}:{{ .Values.autoscaler.tag }}"
          name: autoscaler
          command:
            - /cluster-proportional-autoscaler
            - --namespace={{ .Release.Namespace }}
            - --configmap={{ include "aws-calico.fullname" . }}-typha-horizontal-autoscaler
            - --target=deployment/{{ include "aws-calico.fullname" . }}-typha
            - --logtostderr=true
            - --v=2
          resources:
            {{- toYaml .Values.calico.typha_autoscaler.resources | nindent 12 }}
      serviceAccountName: "{{ include "aws-calico.serviceAccountName" . }}-typha-cpha"
