{{- if .Values.webui.enabled }}
{{- if and .Values.webui.redis.enabled .Values.webui.externalRedis.enabled }}
  {{ fail "Both webui.redis and webui.externalRedis modules are enabled. Please disable one of them." }}
{{- else if and (not .Values.webui.redis.enabled) (not .Values.webui.externalRedis.enabled) }}
  {{ fail "Neither the included Redis nor the external Redis is enabled. Please enable one of them." }}
{{- end }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "falcosidekick.fullname" . }}-ui
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "falcosidekick.labels" . | nindent 4 }}
    app.kubernetes.io/component: ui
spec:
  replicas: {{ .Values.webui.replicaCount }}
  {{- if .Values.webui.revisionHistoryLimit }}
  revisionHistoryLimit: {{ .Values.webui.revisionHistoryLimit }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "falcosidekick.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: ui
  template:
    metadata:
      labels:
        {{- include "falcosidekick.labels" . | nindent 8 }}
        app.kubernetes.io/component: ui
      {{- if .Values.webui.podLabels }}
{{ toYaml .Values.webui.podLabels | indent 8 }}
      {{- end }}
      {{- if .Values.webui.podAnnotations }}
      annotations:
{{ toYaml .Values.webui.podAnnotations | indent 8 }}
      {{- end }}
    spec:
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
      {{- range .Values.imagePullSecrets }}
        - name: {{ . }}
      {{- end }}
      {{- end }}
      serviceAccountName: {{ include "falcosidekick.fullname" . }}-ui
      {{- if .Values.webui.priorityClassName }}
      priorityClassName: "{{ .Values.webui.priorityClassName }}"
      {{- end }}
      {{- if .Values.webui.podSecurityContext }}
      securityContext:
      {{- toYaml .Values.webui.podSecurityContext | nindent 8}}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}-ui
          image: "{{ .Values.webui.image.registry }}/{{ .Values.webui.image.repository }}:{{ .Values.webui.image.tag }}"
          imagePullPolicy: {{ .Values.webui.image.pullPolicy }}
          envFrom:
            - secretRef:
                {{- if .Values.webui.existingSecret }}
                name: {{ .Values.webui.existingSecret }}
                {{- else }}
                name: {{ include "falcosidekick.fullname" . }}-ui
                {{- end }}
          args:
            - "-r"
            {{- if .Values.webui.redis.enabled }}
            - {{ include "falcosidekick.fullname" . }}-ui-redis{{ if .Values.webui.redis.fullfqdn }}.{{ .Release.Namespace }}.svc.cluster.local{{ end }}:{{ .Values.webui.redis.service.port | default "6379" }}
            {{- else if .Values.webui.externalRedis.enabled }}
            - "{{ required "External Redis is enabled. Please set the URL to the database." .Values.webui.externalRedis.url }}:{{ .Values.webui.externalRedis.port | default "6379" }}"
            {{- end}}
            {{- if .Values.webui.ttl }}
            - "-t"
            - {{ .Values.webui.ttl | quote }}
            {{- end}}
            {{- if .Values.webui.loglevel }}
            - "-l"
            - {{ .Values.webui.loglevel }}
            {{- end}}
            {{- if .Values.webui.allowcors }}
            - "-x"
            {{- end}}
            {{- if .Values.webui.disableauth }}
            - "-d"
            {{- end}}
          ports:
            - name: http
              containerPort: 2802
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /api/v1/healthz
              port: http
            initialDelaySeconds: 10
            periodSeconds: 5
          readinessProbe:
            httpGet:
              path: /api/v1/healthz
              port: http
            initialDelaySeconds: 10
            periodSeconds: 5
          {{- if .Values.webui.securityContext }}
          securityContext:
          {{- toYaml .Values.webui.securityContext | nindent 12 }}
          {{- end }}
          resources:
            {{- toYaml .Values.webui.resources | nindent 12 }}
      {{- with .Values.webui.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.webui.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.webui.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
{{- if .Values.webui.redis.enabled }}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "falcosidekick.fullname" . }}-ui-redis
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "falcosidekick.labels" . | nindent 4 }}
    app.kubernetes.io/component: ui-redis
spec:
  replicas: 1
  serviceName: {{ include "falcosidekick.fullname" . }}-ui-redis
  selector:
    matchLabels:
      {{- include "falcosidekick.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: ui-redis
  template:
    metadata:
      labels:
        {{- include "falcosidekick.labels" . | nindent 8 }}
        app.kubernetes.io/component: ui-redis
      {{- if .Values.webui.redis.podLabels }}
{{ toYaml .Values.webui.redis.podLabels | indent 8 }}
      {{- end }}
      {{- if .Values.webui.redis.podAnnotations }}
      annotations:
{{ toYaml .Values.webui.redis.podAnnotations | indent 8 }}
      {{- end }}
    spec:
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
      {{- range .Values.imagePullSecrets }}
        - name: {{ . }}
      {{- end }}
      {{- end }}
      serviceAccountName: {{ include "falcosidekick.fullname" . }}-ui
      {{- if .Values.webui.redis.priorityClassName }}
      priorityClassName: "{{ .Values.webui.redis.priorityClassName }}"
      {{- end }}
      {{- if .Values.webui.redis.podSecurityContext }}
      securityContext:
      {{- toYaml .Values.webui.redis.podSecurityContext | nindent 8}}
      {{- end }}
      containers:
        - name: redis
          image: "{{ .Values.webui.redis.image.registry }}/{{ .Values.webui.redis.image.repository }}:{{ .Values.webui.redis.image.tag }}"
          imagePullPolicy: {{ .Values.webui.redis.image.pullPolicy }}
          {{- if .Values.webui.redis.password }}
          envFrom:
            - secretRef:
                {{- if .Values.webui.redis.existingSecret }}
                name: {{ .Values.webui.redis.existingSecret }}
                {{- else }}
                name: {{ include "falcosidekick.fullname" . }}-ui-redis
                {{- end }}
          {{- end}}
          args: []
          ports:
            - name: redis
              containerPort: 6379
              protocol: TCP
          livenessProbe:
            tcpSocket:
              port: 6379
            initialDelaySeconds: 5
            periodSeconds: 5
            timeoutSeconds: 2
            successThreshold: 1
            failureThreshold: 3
          readinessProbe:
            tcpSocket:
              port: 6379
            initialDelaySeconds: 5
            periodSeconds: 5
            timeoutSeconds: 2
            successThreshold: 1
            failureThreshold: 3
          {{- if .Values.webui.redis.securityContext }}
          securityContext:
          {{- toYaml .Values.webui.redis.securityContext | nindent 12 }}
          {{- end }}
          {{- if .Values.webui.redis.storageEnabled }}
          volumeMounts:
          - name: {{ include "falcosidekick.fullname" . }}-ui-redis-data
            mountPath: /data
          {{- end }}
          resources:
            {{- toYaml .Values.webui.redis.resources | nindent 12 }}
      {{- with .Values.webui.redis.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.webui.redis.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.webui.redis.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
  {{- if .Values.webui.redis.storageEnabled }}
  volumeClaimTemplates:
    - metadata:
        name: {{ include "falcosidekick.fullname" . }}-ui-redis-data
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: {{ .Values.webui.redis.storageSize }}
        {{- if .Values.webui.redis.storageClass }}
        storageClassName: {{ .Values.webui.redis.storageClass }}
        {{- end }}
  {{- end }}
{{- end }}
{{- end }}
