apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Chart.Name }}
  labels:
    {{- include "oai-upf.labels" . | nindent 4 }}
spec:
  replicas: 1
  selector:
    matchLabels:
      {{- include "oai-upf.selectorLabels" . | nindent 6 }}
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        {{- include "oai-upf.selectorLabels" . | nindent 8 }}
    {{- if .Values.multus.n3Interface.create }}
      annotations:
        k8s.v1.cni.cncf.io/networks: >-
          [{
                 "name": "{{ .Chart.Name }}-n3",
                 "interface": "n3"
                {{- if .Values.multus.n3Interface.Mac }}
                 ,"mac":  "{{ .Values.multus.n3Interface.Mac }}"
                {{- end }}
                {{- if .Values.multus.defaultGateway }}
                 ,"default-route": ["{{ .Values.multus.defaultGateway }}"]
                {{- end }}
                {{- if .Values.multus.n3Interface.Gateway }}
                ,"gateway": "{{ .Values.multus.n3Interface.Gateway }}"
                {{- end }}
          }
          {{- if .Values.multus.n4Interface.create }}
          ,{
                 "name": "{{ .Chart.Name }}-n4",
                 "interface": "n4"
                {{- if .Values.multus.n4Interface.Mac }}
                 ,"mac":  "{{ .Values.multus.n4Interface.Mac }}"
                {{- end }}
                {{- if .Values.multus.n4Interface.Gateway }}
                ,"gateway": "{{ .Values.multus.n4Interface.Gateway }}"
                {{- end }}
          }
          {{- end }}
          {{- if .Values.multus.n6Interface.create }}
          ,{
                 "name": "{{ .Chart.Name }}-n6",
                 "interface": "n6"
                {{- if .Values.multus.n6Interface.Mac }}
                 ,"mac":  "{{ .Values.multus.n6Interface.Mac }}"
                {{- end }}
                {{- if .Values.multus.n6Interface.Gateway }}
                ,"gateway": "{{ .Values.multus.n6Interface.Gateway }}"
                {{- end }}
          }
          {{- end }}
          ]
    {{- end }}
    spec:
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
    {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
        {{ toYaml .Values.imagePullSecrets | indent 8 }}
    {{- end }}
    {{- if .Values.global }}{{ if .Values.global.waitForNRF }}
      initContainers:
        - name: init
          image: docker.io/alpine/curl:3.14
          imagePullPolicy: IfNotPresent
          command: 
            - /bin/sh
            - -c
            - until curl --connect-timeout {{ .Values.global.timeout }} --head -X GET http://oai-nrf/nnrf-nfm/v1/nf-instances?nf-type='NRF' {{ .Values.global.http2Param }}; do echo waiting for oai-nrf; sleep 1; done
    {{- end }}{{ end }}
      containers:
    {{- if .Values.includeTcpDumpContainer }}
      - name: tcpdump
        image: "{{ .Values.tcpdumpimage.repository }}:{{ .Values.tcpdumpimage.version }}"
        imagePullPolicy: {{ .Values.tcpdumpimage.pullPolicy }}
        {{- if .Values.resources.define}}
        resources:
          requests:
            memory: {{ .Values.resources.requests.tcpdump.memory | quote }}
            cpu: {{ .Values.resources.requests.tcpdump.cpu | quote }}
          limits:
            memory: {{ .Values.resources.limits.tcpdump.memory | quote }}
            cpu: {{ .Values.resources.limits.tcpdump.cpu | quote }}
        {{- end}}
        securityContext:
          {{- toYaml .Values.securityContext | nindent 12 }}
        {{- if .Values.start.tcpdump}}
        command:
          - /bin/sh
          - -c
          - /usr/sbin/tcpdump -i any -w /tmp/pcap/{{ .Chart.Name }}_`date +%Y-%m-%d_%H_%M-%S-%Z`.pcap
        {{- else}}
        command:
          - /bin/sleep
          - infinity
        {{- end}}
        {{- if .Values.includeTcpDumpContainer}}
        {{- if .Values.persistent.sharedvolume}}
        volumeMounts:
        - mountPath: "/tmp/pcap"
          name: cn5g-pvc
        {{- end}}
        {{- end}}
    {{- end }}
      - name: upf
        image: "{{ .Values.nfimage.repository }}:{{ .Values.nfimage.version }}"
        imagePullPolicy: {{ .Values.nfimage.pullPolicy }}
        volumeMounts:
          - mountPath: /openair-upf/etc
            name: configuration
        securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
        {{- if .Values.readinessProbe}}
        readinessProbe:
          exec:
            command:
            - /bin/bash
            - /openair-upf/bin/healthcheck.sh
          initialDelaySeconds: 2
          periodSeconds: 3
        {{- end}}
        lifecycle:
          postStart:
            exec:
              command: ["/sbin/sysctl", "-w", "net.ipv4.ip_forward=1"]
        {{- if .Values.livenessProbe}}
        livenessProbe:
          exec:
            command:
            - /bin/bash
            - /openair-upf/bin/healthcheck.sh
          initialDelaySeconds: 10
          periodSeconds: 5
        {{- end}}
        ports:
        - containerPort: 8805
          name: pfcp
        - containerPort: 2152
          name: s1u
        {{- if .Values.start.spgwu}}
        {{- else}}
        command:
          - /bin/sleep
          - infinity
        {{- end}}
        {{- if .Values.resources.define}}
        resources:
          requests:
            memory: {{ .Values.resources.requests.nf.memory | quote }}
            cpu: {{ .Values.resources.requests.nf.cpu | quote }}
          limits:
            memory: {{ .Values.resources.limits.nf.memory | quote }}
            cpu: {{ .Values.resources.limits.nf.cpu | quote }}
        {{- end}}
      volumes:
      - configMap:
      {{- if .Values.global }}
          name: {{ .Values.global.nfConfigurationConfigMap }}
      {{- else }}
          name: {{ .Chart.Name }}-configmap
      {{- end }}
        name: configuration
      {{- if .Values.includeTcpDumpContainer}}
      {{- if .Values.persistent.sharedvolume}}
      - name: cn5g-pvc
        persistentVolumeClaim:
          claimName: cn5g-pvc
      {{- end }}
      {{- end }}
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      serviceAccountName: {{ .Values.serviceAccount.name }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- if .Values.nodeSelector}}
      nodeSelector: 
         {{- toYaml .Values.nodeSelector | nindent 12 }}
      {{- end }}
      {{- if .Values.nodeName}}
      nodeName: {{ .Values.nodeName }}
      {{- end }}