{{- range $key, $val := $.Values.probes }}
{{- if or (not (hasKey $val "enabled")) $val.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ printf "%s-%s" $.Release.Name (printf "probe-%s" $key)  }}
  namespace: {{ $.Release.Namespace }}
  labels:
    app: {{ printf "%s-%s" $.Release.Name (printf "probe-%s" $key)  }}
    app.kubernetes.io/part-of: oneuptime
    app.kubernetes.io/managed-by: Helm
    appname: oneuptime
    {{- if $.Values.deployment.includeTimestampLabel }}
    date: "{{ now | unixEpoch }}"
    {{- end }}
spec:
  selector:
    matchLabels:
      app: {{ printf "%s-%s" $.Release.Name (printf "probe-%s" $key)  }}
  {{- if and (ne $val.replicaCount nil) $val.disableAutoscaler }}
  replicas: {{ $val.replicaCount }}
  {{- else }}
  {{- if or (not $.Values.autoscaling.enabled) ($val.disableAutoscaler) }}
  replicas: {{ $.Values.deployment.replicaCount }}
  {{- end }}
  {{- end }}
  strategy: {{- toYaml $.Values.deployment.updateStrategy | nindent 4 }}
  template:
    metadata:
      labels:
        app: {{ printf "%s-%s" $.Release.Name (printf "probe-%s" $key)  }}
        {{- if $.Values.deployment.includeTimestampLabel }}
        date: "{{ now | unixEpoch }}"
        {{- end }}
        appname: oneuptime
    spec:
      {{- if $.Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml $.Values.imagePullSecrets | nindent 8 }}
      {{- end }}
      {{- if $val.podSecurityContext }}
      securityContext:
        {{- toYaml $val.podSecurityContext | nindent 8 }}
      {{- else if $.Values.podSecurityContext }}
      securityContext:
        {{- toYaml $.Values.podSecurityContext | nindent 8 }}
      {{- end }}
      {{- if $.Values.affinity }}
      affinity: {{- $.Values.affinity | toYaml | nindent 8 }}
      {{- end }}
      {{- if $.Values.tolerations }}
      tolerations: {{- $.Values.tolerations | toYaml | nindent 8 }}
      {{- end }}
      {{- if $val.nodeSelector }}
      nodeSelector:
        {{- toYaml $val.nodeSelector | nindent 8 }}
      {{- else if $.Values.nodeSelector }}
      nodeSelector:
        {{- toYaml $.Values.nodeSelector | nindent 8 }}
      {{- end }}
      containers:
        - image: {{ include "oneuptime.image" (dict "Values" $.Values "ServiceName" "probe") }}
          name: {{ printf "%s-%s" $.Release.Name (printf "probe-%s" $key)  }}
          {{- if $val.containerSecurityContext }}
          securityContext:
            {{- toYaml $val.containerSecurityContext | nindent 12 }}
          {{- else if $.Values.containerSecurityContext }}
          securityContext:
            {{- toYaml $.Values.containerSecurityContext | nindent 12 }}
          {{- end }}
          imagePullPolicy: {{ $.Values.image.pullPolicy }}
          env:
            - name: BILLING_ENABLED
              value: {{ $.Values.billing.enabled | squote }}
            - name: LOG_LEVEL
              value: {{ $.Values.logLevel }}
            - name: PORT
              {{- if and $val.ports $val.ports.http }}
              value: {{ $val.ports.http | squote }}
              {{- else }}
              value: "3874"
              {{- end }}
            - name: OPENTELEMETRY_EXPORTER_OTLP_HEADERS
              value: {{ $.Values.openTelemetryExporter.headers }}
            - name: OPENTELEMETRY_EXPORTER_OTLP_ENDPOINT
              value: {{ $.Values.openTelemetryExporter.endpoint }}
            - name: ONEUPTIME_URL
              value: http://{{ $.Release.Name }}-probe-ingest.{{ $.Release.Namespace }}.svc.{{ $.Values.global.clusterDomain }}:{{ $.Values.probeIngest.ports.http }}
            - name: PROBE_NAME
              value: {{ $val.name }}
            - name: PROBE_DESCRIPTION
              value: {{ $val.description }}
            - name: PROBE_MONITORING_WORKERS
              value: {{ $val.monitoringWorkers | squote }}
              {{- if $val.monitorRetryLimit }}
            - name: PROBE_MONITOR_RETRY_LIMIT
              value: {{ $val.monitorRetryLimit | squote }}
              {{- else }}
            - name: PROBE_MONITOR_RETRY_LIMIT
              value: {{ 3 | squote }}
              {{- end }}
            # syntheticMonitorScriptTimeoutInMs
            - name: PROBE_SYNTHETIC_MONITOR_SCRIPT_TIMEOUT_IN_MS
              value: {{ $val.syntheticMonitorScriptTimeoutInMs | squote }}
            - name: PROBE_CUSTOM_CODE_MONITOR_SCRIPT_TIMEOUT_IN_MS
              value: {{ $val.customCodeMonitorScriptTimeoutInMs | squote }}
            - name: PROBE_KEY
              {{- if $val.key }}
              value: {{ $val.key }}
              {{- else }}
              valueFrom:
                secretKeyRef:
                  name: {{ printf "%s-%s" $.Release.Name "secrets"  }}
                  key: {{printf "probe-%s" $key}}
              {{- end }}
            - name: PROBE_MONITOR_FETCH_LIMIT
              value: {{ $val.monitorFetchLimit | squote }}
            {{- if $val.disableTelemetryCollection }}
            - name: DISABLE_TELEMETRY
              value: {{ $val.disableTelemetryCollection | quote }}
            {{- end }}
            {{- if and $val.proxy $val.proxy.httpProxyUrl }}
            - name: HTTP_PROXY_URL
              value: {{ $val.proxy.httpProxyUrl | squote }}
            {{- end }}
            {{- if and $val.proxy $val.proxy.httpsProxyUrl }}
            - name: HTTPS_PROXY_URL
              value: {{ $val.proxy.httpsProxyUrl | squote }}
            {{- end }}
            {{- if and $val.proxy $val.proxy.noProxy }}
            - name: NO_PROXY
              value: {{ $val.proxy.noProxy | squote }}
            {{- end }}
            {{- include "oneuptime.env.runtime" (dict "Values" $.Values "Release" $.Release) | nindent 12 }}
          ports:
            - containerPort: {{ if and $val.ports $val.ports.http }}{{ $val.ports.http }}{{ else }}3874{{ end }}
              protocol: TCP
              name: http
          {{- if $val.resources }}
          resources:
            {{- toYaml $val.resources | nindent 12 }}
          {{- end }}
        {{- if $val.additionalContainers }}
        {{ toYaml $val.additionalContainers | nindent 8 }}
        {{- end }}
      restartPolicy: {{ $.Values.image.restartPolicy }}
---

# OneUptime probe Service
{{- $probePort := 3874 }}
{{- if and $val.ports $val.ports.http }}
{{- $probePort = $val.ports.http }}
{{- end }}
{{- $probePorts := dict "port" $probePort -}}
{{- $probeServiceArgs := dict "ServiceName" (printf "probe-%s" $key) "Ports" $probePorts "Release" $.Release "Values" $.Values -}}
{{- include "oneuptime.service" $probeServiceArgs }}
---

{{- if and (not $val.disableAutoscaler) (not (and $.Values.keda.enabled (and $val.keda $val.keda.enabled))) }}
# OneUptime probe autoscaler
{{- $probeAutoScalerArgs := dict "ServiceName" (printf "probe-%s" $key) "Release" $.Release "Values" $.Values -}}
{{- include "oneuptime.autoscaler" $probeAutoScalerArgs }}
{{- end }}
---

{{- end }}

{{- end }}

