{{- /*
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 "helm") -}}
{{- $ca := genCA "cilium-ca" 365 }}
{{- $releaseNamespace := include "common.names.namespace" . }}
{{- $clusterDomain := .Values.clusterDomain }}
{{- $peersCN := list "*" (.Values.clusterName | replace "." "-") "hubble-grpc.cilium.io" | join "." -}}
{{- $peersFullname := printf "%s-hubble-peers" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" -}}
{{- $peersAltNames := list $peersCN (printf "%s.%s.svc.%s" $peersFullname $releaseNamespace $clusterDomain) $peersFullname "127.0.0.1" "localhost" }}
{{- $peersCert := genSignedCert $peersCN nil $peersAltNames 365 $ca }}
{{- $peersSecretName := include "cilium.hubble.tls.peers.secretName" . }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $peersSecretName }}
  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 }}
type: kubernetes.io/tls
data:
  ca.crt: {{ include "common.secrets.lookup" (dict "secret" $peersSecretName "key" "ca.crt" "defaultValue" $ca.Cert "context" $) }}
  tls.crt: {{ include "common.secrets.lookup" (dict "secret" $peersSecretName "key" "tls.crt" "defaultValue" $peersCert.Cert "context" $) }}
  tls.key: {{ include "common.secrets.lookup" (dict "secret" $peersSecretName "key" "tls.key" "defaultValue" $peersCert.Key "context" $) }}
---
{{- $relayCN := "*.hubble-relay.cilium.io" -}}
{{- $relayFullname := include "cilium.hubble.relay.fullname" . -}}
{{- $relayAltNames := list $relayCN (printf "%s.%s.svc.%s" $relayFullname $releaseNamespace $clusterDomain) $relayFullname "127.0.0.1" "localhost" }}
{{- $relayCert := genSignedCert $relayCN nil $relayAltNames 365 $ca }}
{{- $relaySecretName := include "cilium.hubble.tls.relay.secretName" . }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $relaySecretName }}
  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-relay
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  ca.crt: {{ include "common.secrets.lookup" (dict "secret" $relaySecretName "key" "ca.crt" "defaultValue" $ca.Cert "context" $) }}
  tls.crt: {{ include "common.secrets.lookup" (dict "secret" $relaySecretName "key" "tls.crt" "defaultValue" $relayCert.Cert "context" $) }}
  tls.key: {{ include "common.secrets.lookup" (dict "secret" $relaySecretName "key" "tls.key" "defaultValue" $relayCert.Key "context" $) }}
---
{{- $relayClientCert := genSignedCert $relayCN nil (list $relayCN) 365 $ca }}
{{- $relayClientSecretName := include "cilium.hubble.tls.relayClient.secretName" . }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $relayClientSecretName }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cilium
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  ca.crt: {{ include "common.secrets.lookup" (dict "secret" $relayClientSecretName "key" "ca.crt" "defaultValue" $ca.Cert "context" $) }}
  tls.crt: {{ include "common.secrets.lookup" (dict "secret" $relayClientSecretName "key" "tls.crt" "defaultValue" $relayClientCert.Cert "context" $) }}
  tls.key: {{ include "common.secrets.lookup" (dict "secret" $relayClientSecretName "key" "tls.key" "defaultValue" $relayClientCert.Key "context" $) }}
{{- else if and .Values.hubble.tls.enabled (not .Values.hubble.tls.autoGenerated.enabled) (empty .Values.hubble.tls.existingCASecret) (empty .Values.hubble.tls.peers.existingSecret) (empty .Values.hubble.tls.relay.existingSecret) (empty .Values.hubble.tls.relayClient.existingSecret) -}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "cilium.hubble.tls.peers.secretName" . }}
  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 }}
type: kubernetes.io/tls
data:
  ca.crt: {{ required "A valid .Values.hubble.tls.ca entry required!" .Values.hubble.tls.ca | b64enc | quote }}
  tls.crt: {{ required "A valid .Values.hubble.tls.peers.cert entry required!" .Values.hubble.tls.peers.cert | b64enc | quote }}
  tls.key: {{ required "A valid .Values.hubble.tls.peers.key entry required!" .Values.hubble.tls.peers.key | b64enc | quote }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "cilium.hubble.tls.relay.secretName" . }}
  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-relay
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  ca.crt: {{ required "A valid .Values.hubble.tls.ca entry required!" .Values.hubble.tls.ca | b64enc | quote }}
  tls.crt: {{ required "A valid .Values.hubble.tls.relay.cert entry required!" .Values.hubble.tls.relay.cert | b64enc | quote }}
  tls.key: {{ required "A valid .Values.hubble.tls.relay.key entry required!" .Values.hubble.tls.relay.key | b64enc | quote }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "cilium.hubble.tls.relayClient.secretName" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cilium
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  ca.crt: {{ required "A valid .Values.hubble.tls.ca entry required!" .Values.hubble.tls.ca | b64enc | quote }}
  tls.crt: {{ required "A valid .Values.hubble.tls.relayClient.cert entry required!" .Values.hubble.tls.relayClient.cert | b64enc | quote }}
  tls.key: {{ required "A valid .Values.hubble.tls.relayClient.key entry required!" .Values.hubble.tls.relayClient.key | b64enc | quote }}
{{- end }}

