# Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}
  namespace: {{ .Release.Namespace }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}
    spec:
      containers:
      - name: nginx
        image: {{ .Values.nginxImage }}
        {{- with .Values.imagePullPolicy }}
        imagePullPolicy: {{ . }}
        {{- end }}
        ports:
        - containerPort: 80
        volumeMounts:
        - name: config
          mountPath: /etc/nginx/conf.d/staticfile.conf
          subPath: staticfile.conf
        {{- if .Values.deploymentDetails }}
        - name: content-static
          mountPath: /usr/share/nginx/html
        {{- end }}
        {{- if .Values.runtimeDetails }}
        - name: content-runtime
          mountPath: /usr/share/nginx/html/runtime
          readOnly: true
        {{- end }}
        resources:
          {{ toYaml .Values.resources | nindent 12 }}
      {{- if .Values.runtimeDetails }}
      - name: content-runtime-updater
        image: {{ .Values.nginxImage }}
        {{- with .Values.imagePullPolicy }}
        imagePullPolicy: {{ . }}
        {{- end }}
        command: ["/bin/sh", "-c"]
        args:
          - |
            set -eu;

            period=60;

            html_dir=/usr/share/nginx/html;
            runtime_index_file="$html_dir/runtime/index.html";

            dso_json_path=runtime/dso.json;
            dso_json_file="$html_dir/$dso_json_path";

            echo "{\"dso\": \"/$dso_json_path\"}" > "$runtime_index_file";

            fail_count=0
            # prevent spamming alerts by logging only after the issues are persistent
            while true; do
              start_time=$(date +%s);

              # prevent curl from writing to stderr while still logging them ourselves
              if result=$(curl -m 10 -sSL --fail-with-body "$SCAN_URL/api/scan/v0/dso" 2>&1); then
                echo "$result" > "$dso_json_file.new"
                mv "$dso_json_file.new" "$dso_json_file";
                fail_count=0
              else
                fail_count=$((fail_count + 1))
                echo "Failed to fetch DSO from $SCAN_URL (failure #$fail_count): $result"
                if [ $((fail_count % LOG_TO_STDERR_AFTER_FAILURES)) -eq 0 ]; then
                  echo "ERROR: $fail_count consecutive failures fetching DSO from $SCAN_URL" 1>&2
                fi
              fi;

              end_time=$(date +%s);
              sleep "$((period - (end_time - start_time)))";
            done;

        env:
          - name: SCAN_URL
            value: {{ .Values.runtimeDetails.scanUrl | quote }}
          - name: LOG_TO_STDERR_AFTER_FAILURES
            value: {{ .Values.runtimeDetails.logToStderrAfterFailures | default 100 | quote }}
        volumeMounts:
        - name: content-runtime
          mountPath: /usr/share/nginx/html/runtime
      {{- end }}
      volumes:
      - name: config
        configMap:
          name: {{ .Release.Name }}-config
      {{- if .Values.deploymentDetails }}
      - name: content-static
        configMap:
          name: {{ .Release.Name }}-content-static
      {{- end }}
      {{- if .Values.runtimeDetails }}
      - name: content-runtime
        emptyDir:
          sizeLimit: 10Mi
          medium: Memory
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
