{{- if $.Values.app.enabled }}
# OneUptime app Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ printf "%s-%s" $.Release.Name "app"  }}
  namespace: {{ $.Release.Namespace }}
  labels:
    app: {{ printf "%s-%s" $.Release.Name "app"  }}
    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 "app"  }}
  {{- if and $.Values.app (hasKey $.Values.app "replicaCount") (ne $.Values.app.replicaCount nil) }}
  replicas: {{ $.Values.app.replicaCount }}
  {{- else }}
  {{- if or (not $.Values.autoscaling.enabled) ($.Values.app.disableAutoscaler) }}
  replicas: {{ $.Values.deployment.replicaCount }}
  {{- end }}
  {{- end }}
  strategy: {{- toYaml $.Values.deployment.updateStrategy | nindent 4 }}
  template:
    metadata:
      labels:
        app: {{ printf "%s-%s" $.Release.Name "app"  }}
        {{- if $.Values.deployment.includeTimestampLabel }}
        date: "{{ now | unixEpoch }}"
        {{- end }}
        appname: oneuptime
    spec:
      {{- if $.Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml $.Values.imagePullSecrets | nindent 8 }}
      {{- end }}
      {{- if $.Values.app.podSecurityContext }}
      securityContext:
        {{- toYaml $.Values.app.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 $.Values.app.nodeSelector }}
      nodeSelector:
        {{- toYaml $.Values.app.nodeSelector | nindent 8 }}
      {{- else if $.Values.nodeSelector }}
      nodeSelector:
        {{- toYaml $.Values.nodeSelector | nindent 8 }}
      {{- end }}
      containers:
        - image: {{ include "oneuptime.image" (dict "Values" $.Values "ServiceName" "app") }}
          name: {{ printf "%s-%s" $.Release.Name "app"  }}
          # Liveness probe
          {{- if $.Values.startupProbe.enabled }}
          # Startup probe
          startupProbe:
            httpGet:
              path: /status/live
              port: {{ $.Values.app.ports.http }}
            periodSeconds: {{ $.Values.startupProbe.periodSeconds }}
            failureThreshold: {{ $.Values.startupProbe.failureThreshold }}
          {{- end }}
          {{- if $.Values.livenessProbe.enabled }}
          # Liveness probe
          livenessProbe:
            httpGet:
              path: /status/live
              port: {{ $.Values.app.ports.http }}
            periodSeconds: {{ $.Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ $.Values.livenessProbe.timeoutSeconds }}
            initialDelaySeconds: {{ $.Values.livenessProbe.initialDelaySeconds }}
          {{- end }}
          {{- if $.Values.readinessProbe.enabled }}
          # Readyness Probe
          readinessProbe:
            httpGet:
              path: /status/ready
              port: {{ $.Values.app.ports.http }}
            periodSeconds: {{ $.Values.readinessProbe.periodSeconds }}
            initialDelaySeconds: {{ $.Values.readinessProbe.initialDelaySeconds }}
            timeoutSeconds: {{ $.Values.readinessProbe.timeoutSeconds }}
          {{- end }}
          {{- if $.Values.app.containerSecurityContext }}
          securityContext:
            {{- toYaml $.Values.app.containerSecurityContext | nindent 12 }}
          {{- else if $.Values.containerSecurityContext }}
          securityContext:
            {{- toYaml $.Values.containerSecurityContext | nindent 12 }}
          {{- end }}
          imagePullPolicy: {{ $.Values.image.pullPolicy }}
          env:
            {{- include "oneuptime.env.common" . | nindent 12 }}
            {{- include "oneuptime.env.runtime" (dict "Values" $.Values "Release" $.Release) | nindent 12 }}
            - name: PORT
              value: {{ $.Values.app.ports.http | quote }}
            - name: SMS_HIGH_RISK_COST_IN_CENTS
              value: {{ $.Values.billing.smsHighRiskValueInCents | quote }}
            - name: CALL_HIGH_RISK_COST_IN_CENTS_PER_MINUTE
              value: {{ $.Values.billing.callHighRiskValueInCentsPerMinute | quote }}
            - name: SMS_DEFAULT_COST_IN_CENTS
              value: {{ $.Values.billing.smsDefaultValueInCents | quote }}
            - name: WHATSAPP_TEXT_DEFAULT_COST_IN_CENTS
              value: {{ $.Values.billing.whatsAppTextDefaultValueInCents | quote }}
            - name: CALL_DEFAULT_COST_IN_CENTS_PER_MINUTE
              value: {{ $.Values.billing.callDefaultValueInCentsPerMinute | quote }}
            - name: DISABLE_TELEMETRY
              value: {{ $.Values.app.disableTelemetryCollection | quote }}
        
          ports:
            - containerPort: {{ $.Values.app.ports.http }}
              protocol: TCP
              name: http
          {{- if $.Values.app.resources }}
          resources:
            {{- toYaml $.Values.app.resources | nindent 12 }}
          {{- end }}
      restartPolicy: {{ $.Values.image.restartPolicy }}

---

# OneUptime app Service
{{- $appPorts := dict "port" $.Values.app.ports.http -}}
{{- $appServiceArgs := dict "ServiceName" "app" "Ports" $appPorts "Release" $.Release "Values" $.Values -}}
{{- include "oneuptime.service" $appServiceArgs }}
---

# OneUptime app autoscaler
{{- if not $.Values.app.disableAutoscaler }}
{{- $appAutoScalerArgs := dict "ServiceName" "app" "Release" $.Release "Values" $.Values -}}
{{- include "oneuptime.autoscaler" $appAutoScalerArgs }}
{{- end }}
---
{{- end }}
