apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "infisical.frontend.fullname" . }}
  {{- with .Values.frontend.deploymentAnnotations }}
  annotations:
    {{- toYaml . | nindent 8 }}
  {{- end }}
  labels:
    {{- include "infisical.frontend.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.frontend.replicaCount }}
  selector:
    matchLabels:
      {{- include "infisical.frontend.matchLabels" . | nindent 6 }}
  template:
    metadata: 
      labels:
        {{- include "infisical.frontend.matchLabels" . | nindent 8 }}
      {{- with .Values.frontend.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      containers:
      - name: {{ template "infisical.name" . }}-{{ .Values.frontend.name }}
        image: "{{ .Values.frontend.image.repository }}:{{ .Values.frontend.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.frontend.image.pullPolicy }}
        readinessProbe:
          httpGet:
            path: /
            port: 3000
          initialDelaySeconds: 10
          periodSeconds: 10
        {{- if .Values.frontend.kubeSecretRef }}
        envFrom:
        - secretRef:
            name: {{ .Values.frontend.kubeSecretRef }}
        {{- end }}
        {{- if .Values.frontendEnvironmentVariables }}
        env:
        {{- range $key, $value := .Values.frontendEnvironmentVariables }}
        {{- if $value | quote | eq "MUST_REPLACE" }}
          {{ fail "Environment variables are not set. Please set all environment variables to continue." }}
        {{ end }}
        - name: {{ $key }}
          value: {{ quote $value }}
        {{- end }}
        {{- end }}
        ports: 
        - containerPort: 3000
---
apiVersion: v1
kind: Service 
metadata:
  name: {{ include "infisical.frontend.fullname" . }}
  labels:
    {{- include "infisical.frontend.labels" . | nindent 4 }}
  {{- with .Values.frontend.service.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.frontend.service.type }}
  selector:
    {{- include "infisical.frontend.matchLabels" . | nindent 8 }}
  ports:
    - protocol: TCP
      port: 3000 # service
      targetPort: 3000 # container port 
      {{- if eq .Values.frontend.service.type "NodePort" }}
      nodePort: {{ .Values.frontend.service.nodePort }}
      {{- end }}