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 }}-{{ .Values.multus.e1Interface.name }}",
                 "interface": "{{ .Values.multus.e1Interface.name }}"
                {{- if .Values.multus.e1Interface.mac }}
                 ,"mac":  "{{ .Values.multus.e1Interface.mac }}"
                {{- end }}
                {{- if .Values.multus.defaultGateway }}
                 ,"default-route": ["{{ .Values.multus.defaultGateway }}"]
                {{- end }}
                {{- if .Values.multus.e1Interface.gateway }}
                ,"gateway": "{{ .Values.multus.e1Interface.gateway }}"
                {{- end }}
          }
          {{- if .Values.multus.n2Interface.create }}
          ,{
                 "name": "{{ .Chart.Name }}-{{ .Values.multus.n2Interface.name }}",
                 "interface": "{{ .Values.multus.n2Interface.name }}"
                {{- if .Values.multus.n2Interface.mac }}
                 ,"mac":  "{{ .Values.multus.n2Interface.mac }}"
                {{- end }}
                {{- if .Values.multus.n2Interface.gateway }}
                ,"gateway": "{{ .Values.multus.n2Interface.gateway }}"
                {{- end }}
          }
          {{- end }}
          {{- if .Values.multus.f1cInterface.create }}
          ,{
                 "name": "{{ .Chart.Name }}-{{ .Values.multus.f1cInterface.name }}",
                 "interface": "{{ .Values.multus.f1cInterface.name }}"
                {{- if .Values.multus.f1cInterface.mac }}
                 ,"mac":  "{{ .Values.multus.f1cInterface.mac }}"
                {{- end }}
                {{- if .Values.multus.f1cInterface.gateway }}
                ,"gateway": "{{ .Values.multus.f1cInterface.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.waitForAMF }}
      initContainers:
        - name: init
          image: docker.io/oaisoftwarealliance/oai-tcpdump-init:alpine-3.20
          imagePullPolicy: IfNotPresent
          command:
            - /bin/sh
            - -c
            - until ncat -zv {{ .Values.config.amfhost }} 38412 --sctp ; do echo waiting for oai-amf to start; sleep 1; done
          resources:
            requests:
              memory: 50Mi
              cpu: 1m
            limits:
              memory: 50Mi
              cpu: 1m
    {{- end }}{{ end }}
      containers:
      - name: oaicucp
        image: "{{ .Values.nfimage.repository }}:{{ .Values.nfimage.version }}"
        securityContext:
          {{- toYaml .Values.securityContext | nindent 12 }}
        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.oaicucp}}
        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 -4 addr show $N2_IF_NAME | grep inet | awk '{ print $2 }' | cut -d/ -f1);
            F1_IP_ADDRESS=$(ip -4 addr show $F1_IF_NAME | grep inet | awk '{ print $2 }' | cut -d/ -f1); 
            E1_IP_ADDRESS=$(ip -4 addr show $E1_IF_NAME | grep inet | awk '{ print $2 }' | cut -d/ -f1); 
            sed -e s/@AMF_IP_ADDRESS@/$AMF_IP_ADDRESS/g
                -e s/@GNB_N2_IP_ADDRESS@/$N2_IP_ADDRESS/g
                -e s/@F1_IP_ADDRESS@/$F1_IP_ADDRESS/g
                -e s/@E1_IP_ADDRESS@/$E1_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: 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:
           capabilities:
              add:
               - NET_ADMIN
               - NET_RAW
              drop:
               - ALL
        {{- if .Values.start.tcpdump}}
        command:
          - /bin/sh
          - -c
          - /usr/bin/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.persistent.sharedvolume }}
        volumeMounts:
        - mountPath: "/tmp/pcap"
          name: oai5g-pv
        {{- end}}
      {{- end }}
      volumes:
      {{- if .Values.persistent.sharedvolume }}
      - name: oai5g-pv
        persistentVolumeClaim:
          claimName: oai5g-pvc
      {{- end }}
      - 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 }}