{{- $root := . -}}
{{- range $name, $svc := .Values.services }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $svc.image }}
  namespace: {{ $root.Release.Namespace }}
  labels:
    app: {{ $svc.image }}
    {{- include "stonks.labels" $root | nindent 4 }}
    stonks-oracle/tier: {{ $svc.tier }}
spec:
  replicas: {{ $svc.replicas }}
  selector:
    matchLabels:
      app: {{ $svc.image }}
  template:
    metadata:
      labels:
        app: {{ $svc.image }}
        stonks-oracle/tier: {{ $svc.tier }}
    spec:
      automountServiceAccountToken: false
      {{- with $root.Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      securityContext:
        {{- include "stonks.podSecurityContext" $root | nindent 8 }}
      containers:
        - name: {{ $svc.image }}
          image: {{ $root.Values.image.registry }}/{{ $svc.image }}:{{ $root.Values.image.tag }}
          imagePullPolicy: {{ $root.Values.image.pullPolicy }}
          {{- if $svc.command }}
          command: ["sh", "-c", {{ $svc.command | quote }}]
          {{- end }}
          {{- if $svc.port }}
          ports:
            - containerPort: {{ $svc.port }}
          {{- end }}
          securityContext:
            {{- include "stonks.containerSecurityContext" $root | nindent 12 }}
          envFrom:
            - configMapRef:
                name: stonks-config
            {{- range $svc.secrets }}
            - secretRef:
                name: {{ . }}
            {{- end }}
          resources:
            {{- toYaml $svc.resources | nindent 12 }}
          {{- if $svc.probes }}
          {{- if $svc.probes.readiness }}
          readinessProbe:
            httpGet:
              path: {{ $svc.probes.readiness.path }}
              port: {{ $svc.probes.readiness.port }}
            initialDelaySeconds: {{ $svc.probes.readiness.initialDelay }}
            periodSeconds: {{ $svc.probes.readiness.period }}
          {{- end }}
          {{- if $svc.probes.liveness }}
          livenessProbe:
            httpGet:
              path: {{ $svc.probes.liveness.path }}
              port: {{ $svc.probes.liveness.port }}
            initialDelaySeconds: {{ $svc.probes.liveness.initialDelay }}
            periodSeconds: {{ $svc.probes.liveness.period }}
          {{- end }}
          {{- end }}
          volumeMounts:
            - name: tmp
              mountPath: /tmp
      volumes:
        - name: tmp
          emptyDir:
            sizeLimit: 10Mi
{{- end }}
