{{/*
Copyright 2019-present Open Networking Foundation

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.
*/}}

{{ tuple "cdn-local" . | include "cdn-local.service_account" }}

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: cdn-local
  labels:
{{ tuple "cdn-local" . | include "cdn-local.metadata_labels" | indent 4 }}
spec:
  replicas: {{ .Values.config.nginx.replicas }}
  selector:
    matchLabels:
{{ tuple "cdn-local" . | include "cdn-local.metadata_labels" | indent 6 }}
  serviceName: "cdn-local"
  template:
    metadata:
      labels:
{{ tuple "cdn-local" . | include "cdn-local.metadata_labels" | indent 8 }}
{{ if .Values.config.sriov.enabled }}
      annotations:
        k8s.v1.cni.cncf.io/networks: '[
          {
            "name": "sgi-net-cdn",
            "interface": {{ .Values.config.nginx.sgiInterface.name | quote }},
            "ips": {{ .Values.config.nginx.sgiInterface.ip | quote }}
          }
        ]'
{{ end }}
    spec:
    {{- if .Values.nodeSelectors.enabled }}
      nodeSelector:
        {{ .Values.nodeSelectors.nginx.label }}: {{ .Values.nodeSelectors.nginx.value }}
    {{- end }}
      hostNetwork: {{ not .Values.config.sriov.enabled }}
      serviceAccountName: cdn-local
      initContainers:
      - name: cdn-local-init
        image: {{ .Values.images.tags.nginx }}
        imagePullPolicy: {{ .Values.images.pullPolicy }}
        securityContext:
          allowPrivilegeEscalation: false
          readOnlyRootFilesystem: false
          runAsUser: 0
        command: [ "bash",  "-xc"]
        args:
        - cp /config/*.sh /shared-vol;cp /conf/* /shared-vol
        volumeMounts:
        - name: shared-volume
          mountPath: /shared-vol
        - name: nginx-net-config
          mountPath: /config
        - name: nginx-rtmp-config
          mountPath: /conf
      containers:
      - name: nginx
        image: {{ .Values.images.tags.nginx }}
        imagePullPolicy: {{ .Values.images.pullPolicy }}
        command: [ "bash",  "-xc"]
        args:
        - chmod a+x /config/*.sh; /config/run-nginx.sh
        securityContext:
          privileged: true
        volumeMounts:
        - name: shared-volume
          mountPath: /config
        - name: shared-volume
          mountPath: /conf
        resources:
          requests:
          {{- if .Values.resources.enabled }}
{{ toYaml .Values.resources.nginx.requests | indent 12 }}
          {{- end }}
          {{- if .Values.config.sriov.enabled }}
            intel.com/sriov_netdevice: 2
          {{- end }}
          limits:
          {{- if .Values.resources.enabled }}
{{ toYaml .Values.resources.nginx.limits | indent 12 }}
          {{- end }}
          {{- if .Values.config.sriov.enabled }}
            intel.com/sriov_netdevice: 2
          {{- end }}
      volumes:
      - name: nginx-net-config
        configMap:
          name: nginx-net-config
          defaultMode: 493
      - name: nginx-rtmp-config
        configMap:
          name: nginx-rtmp-config
          defaultMode: 420
      - name: shared-volume
        emptyDir: {}
