---
# Copyright 2018-present Open Networking Foundation
# Copyright 2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ .Values.spgwu.name }}
  labels:
    app: {{ .Values.spgwu.name }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ .Values.spgwu.name }}
  serviceName: {{ .Values.spgwu.name | quote }}
  template:
    metadata:
      labels:
        app: {{ .Values.spgwu.name }}
      annotations:
        k8s.v1.cni.cncf.io/networks: '[
                { "name": "s1u-net", "interface": "s1u-net", "interfaceRequest": "s1u-net" },
                { "name": "sgi-net", "interface": "sgi-net", "interfaceRequest": "sgi-net" }
        ]'
    spec:
{{- if .Values.network.sriov.enabled }}
      nodeSelector:
{{ toYaml .Values.spgwu.nodeselector | indent 8 }}
{{- end }}
{{- if not .Values.network.sriov.enabled }}
      initContainers:
        - name: spgwu-iptables-init
          image: {{ .Values.spgwu.images.tags.init | quote }}
          imagePullPolicy: {{ .Values.spgwu.images.pullPolicy | quote }}
          command: [ "sh", "-xec"]
          securityContext:
            capabilities:
              add:
                - NET_ADMIN
          args:
            - iptables -I OUTPUT -p icmp --icmp-type destination-unreachable -j DROP;
        - name: spgwu-af-iface-init
          image: {{ .Values.spgwu.images.tags.spgwu | quote }}
          imagePullPolicy: {{ .Values.spgwu.images.pullPolicy | quote }}
          securityContext:
            capabilities:
              add:
                - NET_ADMIN
          command:
            - /opt/dp/scripts/setup-af-iface.sh
          volumeMounts:
            - name: dp-script
              mountPath: /opt/dp/scripts/setup-af-iface.sh
              subPath: setup-af-iface.sh
{{- end }}
      containers:
      - name: {{ .Values.spgwu.name }}
        image: {{ .Values.spgwu.images.tags.spgwu | quote }}
        imagePullPolicy: {{ .Values.spgwu.images.pullPolicy | quote }}
        stdin: true
        tty: true
        env:
        - name: MEM_LIMIT
          valueFrom:
            resourceFieldRef:
              containerName: {{ .Values.spgwu.name }}
              resource: limits.memory
              divisor: 1Mi
        - name: S1U_DEVNAME
          {{- if .Values.network.sriov.enabled }}
          value: s1u-net
          {{- else }}
          value: {{ .Values.spgwu.conf.dp.s1u_af_dev | quote }}
          {{- end }}
        - name: SGI_DEVNAME
          {{- if .Values.network.sriov.enabled }}
          value: sgi-net
          {{- else }}
          value: {{ .Values.spgwu.conf.dp.sgi_af_dev | quote }}
          {{- end }}
        - name: DEVICES
          value: {{ .Values.spgwu.conf.dp.devices | quote }}
        - name: CP_ADDR
          value: spgwc-0.{{ .Values.spgwc.name }}
        - name: DP_ADDR
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: RTR_SGI_IP
          value: {{ .Values.spgwu.conf.dp.rtr_sgi_ip | quote }}
        - name: SGI_MASK
          value: {{ .Values.spgwu.conf.dp.sgi_mask | quote }}
        command: ["bash", "-xc"]
        args:
        - ip a;
          /opt/dp/scripts/launch.sh;
        volumeMounts:
        - name: dp-script
          mountPath: /opt/dp/scripts/launch.sh
          subPath: launch.sh
        - name: dp-config
          mountPath: /etc/dp/config
        #- name: hugepage
        #  mountPath: /dev/hugepages
        resources:
          limits:
            #hugepages-1Gi: 8Gi
            cpu: {{ .Values.spgwu.resources.cpu | quote }}
            memory: {{ .Values.spgwu.resources.memory }}
            {{- if .Values.network.sriov.enabled }}
            intel.com/sriov_vfio: 2
            {{- end }}
        securityContext:
          capabilities:
            add:
              - IPC_LOCK
      volumes:
      - name: dp-script
        configMap:
          name: {{ .Values.spgwu.name }}
          defaultMode: 493
      - name: dp-config
        configMap:
          name: {{ .Values.spgwu.name }}
          defaultMode: 420
      #- name: hugepage
      #  emptyDir:
      #    medium: HugePages
