---
# 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: {{ include "cert-manager-webhook-pcloud.selfSignedIssuer" . }}
  namespace: {{ .Values.certManager.namespace | quote }}
  labels:
    app: {{ include "cert-manager-webhook-pcloud.name" . }}
    chart: {{ include "cert-manager-webhook-pcloud.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  selfSigned: {}
---
# Generate a CA Certificate used to sign certificates for the webhook
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: {{ include "cert-manager-webhook-pcloud.rootCACertificate" . }}
  namespace: {{ .Values.certManager.namespace | quote }}
  labels:
    app: {{ include "cert-manager-webhook-pcloud.name" . }}
    chart: {{ include "cert-manager-webhook-pcloud.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  secretName: {{ include "cert-manager-webhook-pcloud.rootCACertificate" . }}
  duration: 43800h # 5y
  issuerRef:
    name: {{ include "cert-manager-webhook-pcloud.selfSignedIssuer" . }}
  commonName: "ca.cert-manager-webhook-pcloud.cert-manager"
  isCA: true
---
# Create an Issuer that uses the above generated CA certificate to issue certs
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
  name: {{ include "cert-manager-webhook-pcloud.rootCAIssuer" . }}
  namespace: {{ .Values.certManager.namespace | quote }}
  labels:
    app: {{ include "cert-manager-webhook-pcloud.name" . }}
    chart: {{ include "cert-manager-webhook-pcloud.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  ca:
    secretName: {{ include "cert-manager-webhook-pcloud.rootCACertificate" . }}
---
# Finally, generate a serving certificate for the webhook to use
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: {{ include "cert-manager-webhook-pcloud.servingCertificate" . }}
  namespace: {{ .Values.certManager.namespace | quote }}
  labels:
    app: {{ include "cert-manager-webhook-pcloud.name" . }}
    chart: {{ include "cert-manager-webhook-pcloud.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  secretName: {{ include "cert-manager-webhook-pcloud.servingCertificate" . }}
  duration: 8760h # 1y
  issuerRef:
    name: {{ include "cert-manager-webhook-pcloud.rootCAIssuer" . }}
  dnsNames:
  - {{ include "cert-manager-webhook-pcloud.fullname" . }}
  - {{ include "cert-manager-webhook-pcloud.fullname" . }}.{{ .Values.certManager.namespace }}
  - {{ include "cert-manager-webhook-pcloud.fullname" . }}.{{ .Values.certManager.namespace }}.svc