{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if and .Values.hubble.tls.enabled .Values.hubble.tls.autoGenerated.enabled (eq .Values.hubble.tls.autoGenerated.engine "cert-manager") }}
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: {{ printf "%s-hubble-peers-crt" (include "common.names.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cilium
    app.kubernetes.io/component: hubble-peers
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  secretName: {{ template "cilium.hubble.tls.peers.secretName" . }}
  commonName: {{ list "*" (.Values.clusterName | replace "." "-") "hubble-grpc.cilium.io" | join "." | quote }}
  issuerRef:
    name: {{ printf "%s-ca-issuer" (include "common.names.fullname" .) }}
    kind: Issuer
  subject:
    organizations:
      - "Cilium"
  dnsNames:
    - '*.{{ .Values.clusterName | replace "." "-" }}.hubble-grpc.cilium.io'
    - '*.{{ include "common.names.namespace" . }}'
    - '*.{{ include "common.names.namespace" . }}.svc'
    - '*.{{ include "common.names.namespace" . }}.svc.{{ .Values.clusterDomain }}'
    - '*.{{ printf "%s-hubble-peers" ((include "common.names.fullname" .) | trunc 63 | trimSuffix "-") }}'
    - '*.{{ printf "%s-hubble-peers" ((include "common.names.fullname" .) | trunc 63 | trimSuffix "-") }}.{{ include "common.names.namespace" . }}'
    - '*.{{ printf "%s-hubble-peers" ((include "common.names.fullname" .) | trunc 63 | trimSuffix "-") }}.{{ include "common.names.namespace" . }}.svc'
    - '*.{{ printf "%s-hubble-peers" ((include "common.names.fullname" .) | trunc 63 | trimSuffix "-") }}.{{ include "common.names.namespace" . }}.svc.{{ .Values.clusterDomain }}'
  privateKey:
    algorithm: {{ .Values.hubble.tls.autoGenerated.certManager.keyAlgorithm }}
    size: {{ int .Values.hubble.tls.autoGenerated.certManager.keySize }}
  duration: {{ .Values.hubble.tls.autoGenerated.certManager.duration }}
  renewBefore: {{ .Values.hubble.tls.autoGenerated.certManager.renewBefore }}
{{- end }}
