{{- if and .Values.enabled.enabled .Values.enabled.webrtc }}
{{- $namespace := .Release.Namespace -}}
{{- $applicationName := .Values.deploy.name -}}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ $applicationName }}-rtc
  namespace: {{ $namespace }}
  labels:
    {{- include "srs.labels.webrtc" . | nindent 4 }}
spec:
  serviceName: srsrtc
  replicas: {{ .Values.apps.srs.replicas.rtc }}
  selector:
    matchLabels:
      {{- include "srs.matchLabels.webrtc" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "srs.labels.webrtc" . | nindent 8 }}
    spec:
      imagePullSecrets:
        - name: {{ .Values.deploy.harbor.secret }}
      affinity:
{{- /*        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" $.Values.global.nodeAffinityPreset.type "key" $.Values.global.nodeAffinityPreset.key "values" $.Values.global.nodeAffinityPreset.values) | nindent 10 -}}*/}}
      containers:
        - name: {{ $applicationName }}
          image: "{{ .Values.global.image.repository }}/{{ .Values.apps.srs.image.srs }}"
          resources:
            limits:
              memory: 4Gi
              cpu: 1200m
            requests:
              memory: 256Mi
              cpu: 100m
          ports:
            - name: srs-rtmp
              containerPort: {{ .Values.apps.srs.ports.rtmp }}
              protocol: TCP
            - name: srs-api
              containerPort: {{ .Values.apps.srs.ports.api }}
              protocol: TCP
            - name: srs-flv
              containerPort: {{ .Values.apps.srs.ports.flv }}
              protocol: TCP
            - name: srs-webrtc
              containerPort: {{ .Values.apps.srs.ports.rtc }}
              protocol: UDP
          env:
            - name: CANDIDATE
              value: "{{ .Values.deploy.domain }}"
          volumeMounts:
            - name: srs-conf-file
              mountPath: /usr/local/srs/conf/docker.conf
              subPath: docker.conf
            - name: srs-vol
              mountPath: /home/dvr
              subPath: "{{ $namespace }}/{{ $applicationName }}/dvr"
            - name: srs-vol
              mountPath: /home/hls
              subPath: "{{ $namespace }}/{{ $applicationName }}/hls"
        - name: oss-adaptor
          image: "{{ .Values.global.image.repository }}/{{ .Values.apps.srs.image.oss }}"
          imagePullPolicy: Always
          resources:
            limits:
              memory: 4Gi
              cpu: 1200m
            requests:
              memory: 256Mi
              cpu: 100m
          env:
            - name: OSS_ENDPOINT
              value: {{ .Values.apps.srs.oss.endpoint }}
            - name: OSS_AK
              value: {{ .Values.apps.srs.oss.ak }}
            - name: OSS_SK
              value: {{ .Values.apps.srs.oss.sk }}
            - name: OSS_BUCKET
              value: {{ .Values.apps.srs.oss.bucket }}
            - name: SRS_OP
              value: "http://{{ $applicationName }}-op-svc:8080"
            - name: MYSQL_ENDPOINT
              value: {{ .Values.apps.mysql.endpoint }}
            - name: MYSQL_USERNAME
              value: {{ .Values.apps.mysql.username }}
            - name: MYSQL_PASSWORD
              value: {{ .Values.apps.mysql.password }}
          volumeMounts:
            - name: srs-vol
              mountPath: /cmii/share/hls
              subPath: {{ $namespace }}/{{ $applicationName }}/hls
      volumes:
        - name: srs-conf-file
          configMap:
            name: "{{ $applicationName }}-cm"
            items:
              - key: "srs.rtc.conf"
                path: "docker.conf"
        - name: srs-vol
          {{- include "srs.vol.main" . | nindent 10 }}

---
{{- end }}
