apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Chart.Name }}
  labels:
    {{- include "oai-nr-ue.labels" . | nindent 4 }}
spec:
  replicas: 1
  selector:
    matchLabels:
      {{- include "oai-nr-ue.selectorLabels" . | nindent 6 }}
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        {{- include "oai-nr-ue.selectorLabels" . | nindent 8 }}
        app: oai-nr-ue
      annotations:
    {{- if .Values.multus.create }}
        k8s.v1.cni.cncf.io/networks: >-
          [{
                 "name": "{{ .Chart.Name }}-net1"
                {{- if .Values.multus.defaultGateway }}
                 ,"default-route": ["{{ .Values.multus.defaultGateway }}"]
                {{- end }}
          }]
    {{- end }}
    spec:
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
    {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
        {{ toYaml .Values.imagePullSecrets | indent 8 }}
    {{- end }}
      containers:
      - name: nr-ue
        image: "{{ .Values.nfimage.repository }}:{{ .Values.nfimage.version }}"
        volumeMounts:
          - mountPath: /opt/oai-nr-ue/etc
            name: configuration
            # subPath: gnb.conf
          # USRP B210/B200 Mini
        {{- if eq .Values.config.usrp "b2xx"}}
          - mountPath: /dev/bus/usb/
            name: usrp
        {{- 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}}
        securityContext:
           privileged: true
        {{- if .Values.start.nrue}}
        command: [ "/bin/bash", "-c" ]
        args:
          - RFSIM_IP_ADDRESS=$(getent hosts $RFSIM_HOST | awk '{print $1}');
            RFSIM_IP_ADDRESS=$(if [[ $RFSIM_IP_ADDRESS ]]; then echo $RFSIM_IP_ADDRESS; else echo $RFSIM_HOST;fi);
            {{- 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 }}
            {{- if eq .Values.config.usrp "rfsim" }}
              exec /opt/oai-nr-ue/bin/nr-uesoftmodem -O /opt/oai-nr-ue/etc/nr-ue.conf $USE_ADDITIONAL_OPTIONS --rfsimulator.serveraddr $RFSIM_IP_ADDRESS;
            {{- else}}
              exec  /opt/oai-nr-ue/bin/nr-uesoftmodem -O /opt/oai-nr-ue/etc/nr-ue.conf $USE_ADDITIONAL_OPTIONS;
            {{- end }}
        {{- else}}
        command:
          - /bin/sleep
          - infinity
        {{- end}}
        env:
          - name: TZ
            value: {{ .Values.config.timeZone }}
          - name: RFSIM_HOST
            value: {{ .Values.config.rfSimServer }}
          - name: USE_ADDITIONAL_OPTIONS
            value: {{ .Values.config.useAdditionalOptions }}
      {{- 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
      serviceAccountName: {{ .Values.serviceAccount.name }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- if .Values.nodeSelector}}
      nodeSelector: 
         {{- toYaml .Values.nodeSelector | nindent 12 }}
      {{- end }}
      {{- if .Values.nodeName}}
      nodeName: {{ .Values.nodeName }}
      {{- end }}
