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

{{/*
Return the Hubbley Relay configuration.
*/}}
{{- define "cilium.hubble.relay.configuration" -}}
{{- if .Values.hubble.relay.configuration }}
{{- include "common.tplvalues.render" (dict "value" .Values.hubble.relay.configuration .) }}
{{- else }}
cluster-name: {{ .Values.clusterName | quote }}
peer-service: {{ printf "%s.%s.svc.%s:%d" (printf "%s-hubble-peers" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-") (include "common.names.namespace" .) .Values.clusterDomain (int .Values.hubble.peers.service.port) | quote }}
listen-address: {{ printf ":%d" (int .Values.hubble.relay.containerPorts.grpc) | quote }}
{{- if .Values.hubble.relay.metrics.enabled }}
metrics-listen-address: {{ printf ":%d" (int .Values.hubble.relay.containerPorts.metrics) | quote }}
{{- end }}
gops: {{ .Values.hubble.relay.enableGops }}
{{- if .Values.hubble.relay.enableGops }}
gops-port: {{ printf "%d" (int .Values.hubble.relay.containerPorts.gops) | quote }}
{{- end }}
pprof: {{ .Values.hubble.relay.enablePprof }}
{{- if .Values.hubble.relay.enablePprof }}
pprof-listen-address: "localhost"
pprof-port: {{ printf "%d" (int .Values.hubble.relay.containerPorts.pprof) | quote }}
{{- end }}
{{- if .Values.hubble.tls.enabled }}
tls-relay-server-cert-file: /certs/relay/tls.crt
tls-relay-server-key-file: /certs/relay/tls.key
tls-relay-client-ca-files: /certs/relay/ca.crt
tls-hubble-server-ca-files: /certs/relay/ca.crt
tls-hubble-client-cert-file: /certs/relay/tls.crt
tls-hubble-client-key-file: /certs/relay/tls.key
{{- else }}
disable-client-tls: true
disable-server-tls: true
{{- end }}
{{- end }}
{{- end }}

{{- if and .Values.hubble.relay.enabled (not .Values.hubble.relay.existingConfigmap) }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "cilium.hubble.relay.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-relay
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
data:
{{- $configuration := include "cilium.hubble.relay.configuration" . | fromYaml -}}
{{- if .Values.hubble.relay.overrideConfiguration }}
{{- $overrideConfiguration := include "common.tplvalues.render" (dict "value" .Values.hubble.relay.overrideConfiguration "context" .) | fromYaml }}
{{- $configuration = mustMergeOverwrite $configuration $overrideConfiguration }}
{{- end }}
  # Hubble relay configuration to be mounted at /etc/hubble-relay/config.yaml (hardcoded value)
  # ref: https://github.com/cilium/cilium/blob/main/hubble-relay/cmd/root.go#L18
  # ref: https://github.com/cilium/cilium/blob/main/hubble-relay/cmd/serve/serve.go
  config.yaml: |
{{- $configuration | toYaml | nindent 4 }}
{{- end }}
