{{- $root := . -}}
{{- range $name, $svc := .Values.services }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "stellaops.fullname" (dict "root" $root "name" $name) }}
  labels:
    {{- include "stellaops.labels" (dict "root" $root "name" $name "svc" $svc) | nindent 4 }}
spec:
  replicas: {{ default 1 $svc.replicas }}
  selector:
    matchLabels:
      {{- include "stellaops.selectorLabels" (dict "root" $root "name" $name "svc" $svc) | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "stellaops.selectorLabels" (dict "root" $root "name" $name "svc" $svc) | nindent 8 }}
      annotations:
        stellaops.release/version: {{ $root.Values.global.release.version | quote }}
        stellaops.release/channel: {{ $root.Values.global.release.channel | quote }}
    spec:
      containers:
        - name: {{ $name }}
          image: {{ $svc.image | quote }}
          imagePullPolicy: {{ default $root.Values.global.image.pullPolicy $svc.imagePullPolicy }}
{{- if $svc.command }}
          command:
{{- range $cmd := $svc.command }}
            - {{ $cmd | quote }}
{{- end }}
{{- end }}
{{- if $svc.args }}
          args:
{{- range $arg := $svc.args }}
            - {{ $arg | quote }}
{{- end }}
{{- end }}
{{- if $svc.env }}
          env:
{{- range $envName, $envValue := $svc.env }}
            - name: {{ $envName }}
              value: {{ $envValue | quote }}
{{- end }}
{{- end }}
{{- if $svc.envFrom }}
          envFrom:
{{ toYaml $svc.envFrom | nindent 12 }}
{{- end }}
{{- if $svc.ports }}
          ports:
{{- range $port := $svc.ports }}
            - name: {{ default (printf "%s-%v" $name $port.containerPort) $port.name | trunc 63 | trimSuffix "-" }}
              containerPort: {{ $port.containerPort }}
              protocol: {{ default "TCP" $port.protocol }}
{{- end }}
{{- else if $svc.service.port }}
          ports:
            - name: {{ printf "%s-http" $name | trunc 63 | trimSuffix "-" }}
              containerPort: {{ $svc.service.targetPort | default $svc.service.port }}
              protocol: TCP
{{- end }}
{{- if $svc.resources }}
          resources:
{{ toYaml $svc.resources | nindent 12 }}
{{- end }}
{{- if $svc.livenessProbe }}
          livenessProbe:
{{ toYaml $svc.livenessProbe | nindent 12 }}
{{- end }}
{{- if $svc.readinessProbe }}
          readinessProbe:
{{ toYaml $svc.readinessProbe | nindent 12 }}
{{- end }}
{{- if $svc.volumeMounts }}
          volumeMounts:
{{ toYaml $svc.volumeMounts | nindent 12 }}
{{- end }}
      {{- if or $svc.volumes $svc.volumeClaims }}
      volumes:
{{- if $svc.volumes }}
{{ toYaml $svc.volumes | nindent 8 }}
{{- end }}
{{- if $svc.volumeClaims }}
{{- range $claim := $svc.volumeClaims }}
        - name: {{ $claim.name }}
          persistentVolumeClaim:
            claimName: {{ $claim.claimName }}
{{- end }}
{{- end }}
      {{- end }}
      {{- if $svc.serviceAccount }}
      serviceAccountName: {{ $svc.serviceAccount | quote }}
      {{- end }}
      {{- if $svc.nodeSelector }}
      nodeSelector:
{{ toYaml $svc.nodeSelector | nindent 8 }}
      {{- end }}
      {{- if $svc.affinity }}
      affinity:
{{ toYaml $svc.affinity | nindent 8 }}
      {{- end }}
      {{- if $svc.tolerations }}
      tolerations:
{{ toYaml $svc.tolerations | nindent 8 }}
      {{- end }}
---
{{- if $svc.service }}
apiVersion: v1
kind: Service
metadata:
  name: {{ include "stellaops.fullname" (dict "root" $root "name" $name) }}
  labels:
    {{- include "stellaops.labels" (dict "root" $root "name" $name "svc" $svc) | nindent 4 }}
spec:
  type: {{ default "ClusterIP" $svc.service.type }}
  selector:
    {{- include "stellaops.selectorLabels" (dict "root" $root "name" $name "svc" $svc) | nindent 4 }}
  ports:
    - name: {{ default "http" $svc.service.portName }}
      port: {{ $svc.service.port }}
      targetPort: {{ $svc.service.targetPort | default $svc.service.port }}
      protocol: {{ default "TCP" $svc.service.protocol }}
---
{{- end }}
{{- end }}
