apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "tunneller.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "tunneller.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    helm.sh/chart: {{ include "tunneller.chart" . }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "tunneller.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "tunneller.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
{{- with .Values.podAnnotations }}
      annotations:
{{ toYaml . | indent 8 }}
{{- end }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - socat
          args:
            - "TCP-LISTEN:$(TUNNEL_PORT),fork"
            - "TCP:$(TUNNEL_HOST):$(TUNNEL_PORT)"
          env:
            - name: TUNNEL_HOST
              value: {{ required "Must specify a target host for the tunnel." .Values.tunnel.host | quote }}
            - name: TUNNEL_PORT
              value: {{ required "Must specify a target port for the tunnel." .Values.tunnel.port | quote }}
          ports:
            - name: tunnel-port
              containerPort: {{ (int64 .Values.tunnel.port) }}
              protocol: TCP
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
