apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.app.name }}
  labels:
    app: {{ .Values.app.name }}
    chart: {{ .Chart.Name }}
    version: {{ .Chart.Version }}
spec:
  replicas: {{ .Values.app.replicaCount }}
  selector:
    matchLabels:
      app: {{ .Values.app.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.app.name }}
      {{- if .Values.monitoring.enabled }}
      annotations:
        prometheus.io/scrape: "{{ .Values.monitoring.prometheusScrape }}"
        prometheus.io/port: "{{ .Values.app.service.targetPort }}"
        prometheus.io/path: "/metrics"
      {{- end }}
    spec:
      containers:
      - name: {{ .Values.app.name }}
        image: "{{ .Values.app.image.repository }}:{{ .Values.app.image.tag }}"
        imagePullPolicy: {{ .Values.app.image.pullPolicy }}
        ports:
        - containerPort: {{ .Values.app.service.targetPort }}
          name: http
        env:
        {{- range $key, $value := .Values.app.env }}
        - name: {{ $key }}
          value: {{ $value | quote }}
        {{- end }}
        resources:
          {{- toYaml .Values.app.resources | nindent 10 }}
        readinessProbe:
          httpGet:
            path: /health
            port: {{ .Values.app.service.targetPort }}
          initialDelaySeconds: 10
          periodSeconds: 15