---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: {{ $.Release.Name }}-nginx-cache
  labels:
    app.kubernetes.io/instance: {{ $.Release.Name }}
    app.kubernetes.io/managed-by: {{ $.Release.Service }}
spec:
  maxUnavailable: 1
  selector:
    matchLabels:
      app: {{ $.Release.Name }}-nginx-cache
{{- range $i := until (int $.Values.nginx.replicas) }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $.Release.Name }}-nginx-cache-{{ $i }}
  labels:
    app.kubernetes.io/instance: {{ $.Release.Name }}
    app.kubernetes.io/managed-by: {{ $.Release.Service }}
spec:
  selector:
    matchLabels:
      app: {{ $.Release.Name }}-nginx-cache
      instance: "{{ $i }}"
  template:
    metadata:
      labels:
        app: {{ $.Release.Name }}-nginx-cache
        instance: "{{ $i }}"
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/nginx/configmap.yaml") $ | sha256sum }}
    spec:
      imagePullSecrets:
      - name: {{ $.Release.Name }}-regsecret
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - {{ $.Release.Name }}-nginx-cache
              - key: instance
                operator: NotIn
                values:
                - "{{ $i }}"
            topologyKey: kubernetes.io/hostname
      shareProcessNamespace: true
      containers:
      - name: nginx
        image: "{{ $.Files.Get "images/nginx-cache.tag" | trim }}@{{ index ($.Files.Get "images/nginx-cache.json" | fromJson) "containerimage.digest" }}"
        readinessProbe:
          httpGet:
            path: /healthz
            port: metrics
          initialDelaySeconds: 5
          periodSeconds: 5
        livenessProbe:
          httpGet:
            path: /healthz
            port: metrics
          failureThreshold: 1
          periodSeconds: 10
        ports:
        - containerPort: 80
          name: http
        - containerPort: 8087
          name: cache
        - containerPort: 10253
          name: metrics
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf
          name: config
          subPath: nginx.conf
        - mountPath: /data
          name: data
        - mountPath: /run
          name: run
      - name: reloader
        image: "{{ $.Files.Get "images/nginx-cache.tag" | trim }}@{{ index ($.Files.Get "images/nginx-cache.json" | fromJson) "containerimage.digest" }}"
        command: ["/usr/bin/nginx-reloader.sh"]
        #command: ["sleep", "infinity"]
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf
          name: config
          subPath: nginx.conf
        - mountPath: /data
          name: data
        - mountPath: /run
          name: run
      volumes:
      - name: config
        configMap:
          name: {{ $.Release.Name }}-nginx-cache
      - name: data
        persistentVolumeClaim:
          claimName: {{ $.Release.Name }}-nginx-cache-{{ $i }}
      - name: run
        emptyDir: {}
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: {{ $.Release.Name }}-nginx-cache-{{ $i }}
  labels:
    app.kubernetes.io/instance: {{ $.Release.Name }}
    app.kubernetes.io/managed-by: {{ $.Release.Service }}
spec:
  accessModes: [ "ReadWriteOnce" ]
  resources:
    requests:
      storage: "{{ $.Values.size }}"
---
apiVersion: v1
kind: Service
metadata:
  name: {{ $.Release.Name }}-nginx-cache-{{ $i }}
  labels:
    app: {{ $.Release.Name }}-nginx-cache
    app.kubernetes.io/instance: {{ $.Release.Name }}
    app.kubernetes.io/managed-by: {{ $.Release.Service }}
spec:
  type: ClusterIP
  selector:
    app: {{ $.Release.Name }}-nginx-cache
    instance: "{{ $i }}"
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: http
    - name: metrics
      protocol: TCP
      port: 10253
      targetPort: metrics
{{- end }}
