From ab5b9c1f90fd5f2598b3424acbea43e51831f67b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 1 Jul 2025 21:20:43 +0000 Subject: [PATCH] fix: Integrate bjw-s/common library for exporter controller - Corrected bjw-s/common library repository URL in Chart.yaml to the traditional HTTPS URL and ensured dependencies are fetched. - Renamed exporter template to exporter-controller.yaml. - Updated exporter-controller.yaml to correctly use `bjw-s.common.render.controllers` for rendering. - Refined the context passed to the common library to include Values, Chart, Release, and Capabilities, and initialized expected top-level keys (global, defaultPodOptionsStrategy) in the Values. - Ensured image.tag is defaulted to Chart.AppVersion in the template data to pass common library validations. - Helm lint and template commands now pass successfully for both Deployment and DaemonSet configurations of the exporter. --- .../templates/exporter-controller.yaml | 161 ++++++++++++------ 1 file changed, 110 insertions(+), 51 deletions(-) diff --git a/charts/iperf3-monitor/templates/exporter-controller.yaml b/charts/iperf3-monitor/templates/exporter-controller.yaml index 6b27598..ba83052 100644 --- a/charts/iperf3-monitor/templates/exporter-controller.yaml +++ b/charts/iperf3-monitor/templates/exporter-controller.yaml @@ -1,70 +1,129 @@ {{- /* -Get the exporter controller configuration from values. -We make a deep copy to be able to modify it locally for env vars and service account. +This template is responsible for rendering the 'exporter' controller (Deployment or DaemonSet) +by calling the bjw-s common library. + +The primary values for the exporter are expected under .Values.controllers.exporter. +Modifications to environment variables and service account are handled here before +passing the configuration to the common library. */}} -{{- $exporterControllerConfig := deepCopy .Values.controllers.exporter -}} -{{- $appName := include "iperf3-monitor.name" . -}} -{{- $fullName := include "iperf3-monitor.fullname" . -}} + +{{- /* +Prepare a local, modifiable copy of the .Values. This allows us to adjust the +exporter controller's configuration (like env vars and SA) specifically for this chart's needs +before the common library processes it. +Convert to map[string]interface{} via toYaml/fromYaml to ensure compatibility with 'dig'. +*/}} +{{- $localValues := .Values | toYaml | fromYaml | deepCopy -}} {{- $chart := .Chart -}} {{- $release := .Release -}} -{{- $values := .Values -}} +{{- $appName := include "iperf3-monitor.name" . -}} +{{- $fullName := include "iperf3-monitor.fullname" . -}} {{- /* -Construct the base environment variables for the exporter container. +Define the key for the exporter controller, typically "exporter" as per our values.yaml. */}} -{{- $baseExporterEnv := dict -}} -{{- $_ := set $baseExporterEnv "SOURCE_NODE_NAME" (dict "valueFrom" (dict "fieldRef" (dict "fieldPath" "spec.nodeName"))) -}} -{{- $_ := set $baseExporterEnv "IPERF_TEST_INTERVAL" ($exporterControllerConfig.appConfig.testInterval | toString) -}} -{{- $_ := set $baseExporterEnv "IPERF_TEST_PROTOCOL" $exporterControllerConfig.appConfig.testProtocol -}} -{{- $_ := set $baseExporterEnv "LOG_LEVEL" $exporterControllerConfig.appConfig.logLevel -}} -{{- $_ := set $baseExporterEnv "IPERF_SERVER_PORT" ($exporterControllerConfig.appConfig.serverPort | toString) -}} -{{- $_ := set $baseExporterEnv "IPERF_SERVER_NAMESPACE" (dict "valueFrom" (dict "fieldRef" (dict "fieldPath" "metadata.namespace"))) -}} -{{- $_ := set $baseExporterEnv "IPERF_TEST_TIMEOUT" ($exporterControllerConfig.appConfig.testTimeout | toString) -}} -{{- $serverLabelSelector := tpl ($exporterControllerConfig.appConfig.serverLabelSelector | default (printf "app.kubernetes.io/name=%s,app.kubernetes.io/instance=%s,app.kubernetes.io/component=server" $appName $release.Name)) . -}} -{{- $_ := set $baseExporterEnv "IPERF_SERVER_LABEL_SELECTOR" $serverLabelSelector -}} +{{- $exporterControllerKey := "exporter" -}} {{- /* -Merge with any additional environment variables defined by the user -under controllers.exporter.containers.exporter.env. -User-defined values (from .Values.controllers.exporter.containers.exporter.env) -will take precedence if keys conflict, achieved by merging them on top of base. +Attempt to get the exporter controller's configuration block. +Proceed with modifications only if the exporter controller is defined. */}} -{{- $userExporterEnv := $exporterControllerConfig.containers.exporter.env | default dict -}} -{{- $finalExporterEnv := mergeOverwrite $baseExporterEnv $userExporterEnv -}} +{{- $exporterControllerConfig := get $localValues.controllers $exporterControllerKey -}} +{{- if $exporterControllerConfig -}} -{{- /* -Update the exporter container's env in our local copy of the controller config. -The common library expects the env map under containers..env. -The container name is assumed to be 'exporter' as per our values.yaml structure. -*/}} -{{- if not $exporterControllerConfig.containers.exporter -}} - {{- $_ := set $exporterControllerConfig.containers "exporter" dict -}} + {{- /* + Construct the base set of environment variables required by the iperf3-exporter application. + These are derived from the 'appConfig' section of the exporter's controller configuration. + */}} + {{- $baseExporterEnv := dict -}} + {{- if $exporterControllerConfig.appConfig -}} + {{- $_ := set $baseExporterEnv "SOURCE_NODE_NAME" (dict "valueFrom" (dict "fieldRef" (dict "fieldPath" "spec.nodeName"))) -}} + {{- $_ := set $baseExporterEnv "IPERF_TEST_INTERVAL" ($exporterControllerConfig.appConfig.testInterval | default "300" | toString) -}} + {{- $_ := set $baseExporterEnv "IPERF_TEST_PROTOCOL" ($exporterControllerConfig.appConfig.testProtocol | default "tcp") -}} + {{- $_ := set $baseExporterEnv "LOG_LEVEL" ($exporterControllerConfig.appConfig.logLevel | default "INFO") -}} + {{- $_ := set $baseExporterEnv "IPERF_SERVER_PORT" ($exporterControllerConfig.appConfig.serverPort | default "5201" | toString) -}} + {{- $_ := set $baseExporterEnv "IPERF_SERVER_NAMESPACE" (dict "valueFrom" (dict "fieldRef" (dict "fieldPath" "metadata.namespace"))) -}} + {{- $_ := set $baseExporterEnv "IPERF_TEST_TIMEOUT" ($exporterControllerConfig.appConfig.testTimeout | default "10" | toString) -}} + {{- $serverLabelSelectorDefault := printf "app.kubernetes.io/name=%s,app.kubernetes.io/instance=%s,app.kubernetes.io/component=server" $appName $release.Name -}} + {{- $serverLabelSelector := tpl ($exporterControllerConfig.appConfig.serverLabelSelector | default $serverLabelSelectorDefault) (dict "Release" $release "Chart" $chart "Values" $localValues) -}} + {{- $_ := set $baseExporterEnv "IPERF_SERVER_LABEL_SELECTOR" $serverLabelSelector -}} + {{- end -}} + + {{- /* + Merge the base environment variables with any user-defined environment variables. + User-defined variables (from .Values.controllers.exporter.containers.exporter.env) + will take precedence in case of conflicting keys. + */}} + {{- $userExporterEnv := $exporterControllerConfig.containers.exporter.env | default dict -}} + {{- $finalExporterEnv := mergeOverwrite $baseExporterEnv $userExporterEnv -}} + + {{- /* + Ensure the container structure exists and update its 'env' field with the final set. + The common library expects this under controllers..containers..env + */}} + {{- if not $exporterControllerConfig.containers -}} + {{- $_ := set $exporterControllerConfig "containers" dict -}} + {{- end -}} + {{- if not $exporterControllerConfig.containers.exporter -}} + {{- $_ := set $exporterControllerConfig.containers "exporter" dict -}} + {{- end -}} + {{- $_ := set $exporterControllerConfig.containers.exporter "env" $finalExporterEnv -}} + + {{- /* + Ensure the container image tag is set, defaulting to Chart.AppVersion if empty, + as the common library validation requires it during 'helm template'. + */}} + {{- $exporterContainerCfg := get $exporterControllerConfig.containers "exporter" -}} + {{- if $exporterContainerCfg -}} + {{- if not $exporterContainerCfg.image.tag -}} + {{- if $chart.AppVersion -}} + {{- $_ := set $exporterContainerCfg.image "tag" $chart.AppVersion -}} + {{- else -}} + {{- fail (printf "Error: Container image tag is not specified for controller '%s', container '%s', and Chart.AppVersion is also empty." $exporterControllerKey "exporter") -}} + {{- end -}} + {{- end -}} + {{- end -}} + + {{- /* + Configure the Service Account for the exporter controller. + This ensures the controller pod uses the ServiceAccount that is intended by this chart's + RBAC configuration (.Values.rbac.create and .Values.serviceAccount.name). + */}} + {{- $serviceAccountNameFromValues := $localValues.serviceAccount.name | default (printf "%s-exporter" $fullName) -}} + {{- if not $exporterControllerConfig.serviceAccount -}} + {{- $_ := set $exporterControllerConfig "serviceAccount" dict -}} + {{- end -}} + {{- $_ := set $exporterControllerConfig.serviceAccount "name" $serviceAccountNameFromValues -}} + {{- $_ := set $exporterControllerConfig.serviceAccount "create" $localValues.rbac.create -}} + {{- $_ := set $exporterControllerConfig.serviceAccount "automountServiceAccountToken" ($exporterControllerConfig.pod.automountServiceAccountToken | default true) -}} + + {{- /* + Replace the original exporter controller config in our $localValues copy + with the modified version (that now includes the correct env and SA settings). + */}} + {{- $_ := set $localValues.controllers $exporterControllerKey $exporterControllerConfig -}} {{- end -}} -{{- $_ := set $exporterControllerConfig.containers.exporter "env" $finalExporterEnv -}} {{- /* -Configure Service Account for the exporter controller. -It should use the SA name defined in .Values.serviceAccount.name, and its creation -should be controlled by .Values.rbac.create. -The common library helper "bjw-s.common.lib.controller.serviceAccountName" will use -serviceAccount.name if serviceAccount.create is true. +Ensure .Values.global exists and is a map, as the common library expects it. */}} -{{- $serviceAccountNameFromValues := .Values.serviceAccount.name | default (printf "%s-exporter" $fullName) -}} -{{- if not $exporterControllerConfig.serviceAccount -}} - {{- $_ := set $exporterControllerConfig "serviceAccount" dict -}} +{{- if not (get $localValues "global") -}} + {{- $_ := set $localValues "global" dict -}} +{{- else if not (kindIs "map" (get $localValues "global")) -}} + {{- $_ := set $localValues "global" dict -}} {{- end -}} -{{- $_ := set $exporterControllerConfig.serviceAccount "name" $serviceAccountNameFromValues -}} -{{- $_ := set $exporterControllerConfig.serviceAccount "create" .Values.rbac.create -}} -{{- $_ := set $exporterControllerConfig.serviceAccount "automountServiceAccountToken" true -}} {{/* Explicitly set, though often default */}} - {{- /* -Call the common library template to render the controller (Deployment or DaemonSet). -Pass necessary context: -- controller: our modified $exporterControllerConfig. -- config: The top-level .Values. -- chart: The .Chart object. -- release: The .Release object. -- name: The application name (used by library for defaults if needed). +Ensure defaultPodOptionsStrategy exists, as common lib expects it at the root of Values. */}} -{{- include "bjw-s.common.lib.chart.controller" (dict "controller" $exporterControllerConfig "config" $values "chart" $chart "release" $release "name" $appName ) -}} +{{- if not (get $localValues "defaultPodOptionsStrategy") -}} + {{- $_ := set $localValues "defaultPodOptionsStrategy" "overwrite" -}} +{{- end -}} + +{{- /* +Call the common library's main render function for controllers. +This function iterates through all controllers defined under $localValues.controllers +(in our case, just "exporter") and renders them using their specified type and configuration. +The context passed must mirror the global Helm context, including 'Values', 'Chart', 'Release', and 'Capabilities'. +*/}} +{{- include "bjw-s.common.render.controllers" (dict "Values" $localValues "Chart" $chart "Release" $release "Capabilities" .Capabilities) | nindent 0 -}}