{{- if .Values.certManager.enabled -}}
---
# Create a selfsigned Issuer, in order to create a root CA certificate for
# signing webhook serving certificates
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
  name: {{ template "k8s-prometheus-adapter.fullname" . }}-self-signed-issuer
  namespace: {{ include "k8s-prometheus-adapter.namespace" . }}
  {{- if .Values.customAnnotations }}
  annotations:
  {{- toYaml .Values.customAnnotations | nindent 4 }}
  {{- end }}
  labels:
    {{- include "k8s-prometheus-adapter.labels" . | indent 4 }}
spec:
  selfSigned: {}
---
# Generate a CA Certificate used to sign certificates for the webhook
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: {{ template "k8s-prometheus-adapter.fullname" . }}-root-cert
  namespace: {{ include "k8s-prometheus-adapter.namespace" . }}
  {{- if .Values.customAnnotations }}
  annotations:
  {{- toYaml .Values.customAnnotations | nindent 4 }}
  {{- end }}
  labels:
    {{- include "k8s-prometheus-adapter.labels" . | indent 4 }}
spec:
  secretName: {{ template "k8s-prometheus-adapter.fullname" . }}-root-cert
  duration: {{ .Values.certManager.caCertDuration }}
  issuerRef:
    name: {{ template "k8s-prometheus-adapter.fullname" . }}-self-signed-issuer
  commonName: "ca.webhook.prometheus-adapter"
  isCA: true
---
# Create an Issuer that uses the above generated CA certificate to issue certs
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
  name: {{ template "k8s-prometheus-adapter.fullname" . }}-root-issuer
  namespace: {{ include "k8s-prometheus-adapter.namespace" . }}
  {{- if .Values.customAnnotations }}
  annotations:
  {{- toYaml .Values.customAnnotations | nindent 4 }}
  {{- end }}
  labels:
    {{- include "k8s-prometheus-adapter.labels" . | indent 4 }}
spec:
  ca:
    secretName: {{ template "k8s-prometheus-adapter.fullname" . }}-root-cert
---
# Finally, generate a serving certificate for the apiservices to use
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: {{ template "k8s-prometheus-adapter.fullname" . }}-cert
  namespace: {{ include "k8s-prometheus-adapter.namespace" . }}
  {{- if .Values.customAnnotations }}
  annotations:
  {{- toYaml .Values.customAnnotations | nindent 4 }}
  {{- end }}
  labels:
    {{- include "k8s-prometheus-adapter.labels" . | indent 4 }}
spec:
  secretName: {{ template "k8s-prometheus-adapter.fullname" . }}
  duration: {{ .Values.certManager.certDuration }}
  issuerRef:
    name: {{ template "k8s-prometheus-adapter.fullname" . }}-root-issuer
  dnsNames:
  - {{ template "k8s-prometheus-adapter.fullname" . }}
  - {{ template "k8s-prometheus-adapter.fullname" . }}.{{ include "k8s-prometheus-adapter.namespace" . }}
  - {{ template "k8s-prometheus-adapter.fullname" . }}.{{ include "k8s-prometheus-adapter.namespace" . }}.svc
{{- end -}}