apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Chart.Name }}
  labels:
    {{- include "oai-du.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "oai-du.selectorLabels" . | nindent 6 }}
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        {{- include "oai-du.selectorLabels" . | nindent 8 }}
        app: oai-du
    {{- if .Values.multus.f1Interface.create }}
      annotations:
        k8s.v1.cni.cncf.io/networks: >-
          [{
                 "name": "{{ .Chart.Name }}-f1",
                 "interface": "f1"
                {{- if .Values.multus.defaultGateway }}
                 ,"default-route": ["{{ .Values.multus.defaultGateway }}"]
                {{- end }}
          }
          {{- if .Values.multus.ruInterface.create }}
          ,{
                 "name": "{{ .Chart.Name }}-ru",
                 "interface": "ru"
          }
          {{- end }}
          ]
    {{- end }}
    spec:
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
    {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
        {{ toYaml .Values.imagePullSecrets | indent 8 }}
    {{- end }}
      containers:
      - name: gnbdu
        image: "{{ .Values.nfimage.repository }}:{{ .Values.nfimage.version }}"
        volumeMounts:
          - mountPath: /opt/oai-gnb/etc
            name: configuration
        {{- 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}}
        securityContext:
           privileged: true
        ports:
        - containerPort: 38472
          name: f1c
          protocol: SCTP
        - containerPort: {{ .Values.config.f1duPort}}
          name: f1u
          protocol: UDP
        {{- if .Values.start.gnbdu}}
        command: [ "/bin/bash", "-c" ]
        args:
         -  CU_IP_ADDRESS=$(getent hosts $CU_HOST | awk '{print $1}')
            CU_IP_ADDRESS=$(if [[ $CU_IP_ADDRESS ]]; then echo $CU_IP_ADDRESS; else echo $CU_HOST;fi)
            F1_DU_IP_ADDRESS=$(ip -f inet addr show $F1_IF_NAME | grep -o "inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*");
            sed -e s/@F1_DU_IP_ADDRESS@/$F1_DU_IP_ADDRESS/g
                -e s/@CU_IP_ADDRESS@/$CU_IP_ADDRESS/g
                /opt/oai-gnb/etc/du.conf | tee /tmp/du.conf;
          {{- if eq .Values.config.usrp "b2xx"}}
            /opt/oai-gnb/bin/uhd_images_downloader.py -t b2xx;
          {{- else if eq .Values.config.usrp "n3xx" }}
            /opt/oai-gnb/bin/uhd_images_downloader.py -t n3xx;
          {{- else if eq .Values.config.usrp "x3xx" }}
            /opt/oai-gnb/bin/uhd_images_downloader.py -t x3xx;
          {{- end }}
            exec /opt/oai-gnb/bin/nr-softmodem -O /tmp/du.conf $USE_ADDITIONAL_OPTIONS;
        {{- else}}
        command:
          - /bin/sleep
          - infinity
        {{- end}}
        env:
          - name: TZ
            value: {{ .Values.config.timeZone }}
          - name: USE_ADDITIONAL_OPTIONS
            value: {{ .Values.config.useAdditionalOptions }}
          - name: F1_IF_NAME
            value: {{ .Values.config.f1IfName }}
          - name: CU_HOST
            value: {{ .Values.config.cuHost }}
      {{- 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:
           privileged: true
           capabilities:
              add:
               - NET_ADMIN
              drop:
               - ALL
        {{- 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}}
      {{- end }}
      volumes:
      - configMap:
          name: {{ .Chart.Name }}-configmap
        name: configuration
      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 }}
