apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Chart.Name }}
  labels:
    {{- include "oai-cu-cp.labels" . | nindent 4 }}
spec:
  replicas: 1
  selector:
    matchLabels:
      {{- include "oai-cu-cp.selectorLabels" . | nindent 6 }}
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        {{- include "oai-cu-cp.selectorLabels" . | nindent 8 }}
        app: oai-cu-cp
    {{- if .Values.multus.e1Interface.create }}
      annotations:
        k8s.v1.cni.cncf.io/networks: >-
          [{
                 "name": "{{ .Chart.Name }}-e1",
                 "interface": "e1"
                {{- if .Values.multus.defaultGateway }}
                 ,"default-route": ["{{ .Values.multus.defaultGateway }}"]
                {{- end }}
          }
          {{- if .Values.multus.n2Interface.create }}
          ,{
                 "name": "{{ .Chart.Name }}-n2",
                 "interface": "n2"
          }
          {{- end }}
          {{- if .Values.multus.f1cInterface.create }}
          ,{
                 "name": "{{ .Chart.Name }}-f1c",
                 "interface": "f1"
          }
          {{- end }}
          ]
    {{- end }}
    spec:
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
    {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
        {{ toYaml .Values.imagePullSecrets | indent 8 }}
    {{- end }}
      containers:
      - name: gnbcucp
        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: 36412
          name: n2
          protocol: SCTP
        - containerPort: 38462
          name: e1
          protocol: SCTP
        - containerPort: {{ .Values.config.f1cuPort}}
          name: f1c
          protocol: UDP
        {{- if .Values.start.gnbcucp}}
        command: [ "/bin/bash", "-c" ]
        args:
         -  AMF_IP_ADDRESS=$(getent hosts $AMF_HOST | awk '{print $1}');
            AMF_IP_ADDRESS=$(if [[ $AMF_IP_ADDRESS ]]; then echo $AMF_IP_ADDRESS; else echo $AMF_HOST;fi)
            N2_IP_ADDRESS=$(ip -f inet addr show $N2_IF_NAME | grep -o "inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*");
            F1_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]*");
            CUCP_IP_ADDRESS=$(ip -f inet addr show $E1_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/@N2_IP_ADDRESS@/$N2_IP_ADDRESS/g
                -e s/@F1_IP_ADDRESS@/$F1_IP_ADDRESS/g
                -e s/@CUCP_IP_ADDRESS@/$CUCP_IP_ADDRESS/g
                -e s/@AMF_IP_ADDRESS@/$AMF_IP_ADDRESS/g
                /opt/oai-gnb/etc/cucp.conf | tee /tmp/cucp.conf;
            /opt/oai-gnb/bin/nr-softmodem -O /tmp/cucp.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: OAI_GDBSTACKS
            value: "1"
          - name: AMF_HOST
            value: {{ .Values.config.amfhost }}
          - name: N2_IF_NAME
            value: {{ .Values.config.n2IfName }}
          - name: E1_IF_NAME
            value: {{ .Values.config.e1IfName }}
          - name: N3_IF_NAME
            value: {{ .Values.config.n3IfName }}
          - name: F1_IF_NAME
            value: {{ .Values.config.f1IfName }}
      {{- 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 }}
