diff --git a/charts/helm_lib/Chart.yaml b/charts/helm_lib/Chart.yaml index 6212599..c0b3b2f 100644 --- a/charts/helm_lib/Chart.yaml +++ b/charts/helm_lib/Chart.yaml @@ -1,5 +1,5 @@ apiVersion: v2 type: library name: deckhouse_lib_helm -version: 1.71.0 +version: 1.71.1 description: "Helm utils template definitions for Deckhouse modules." diff --git a/charts/helm_lib/templates/_module_image.tpl b/charts/helm_lib/templates/_module_image.tpl index 7485b33..74ab354 100644 --- a/charts/helm_lib/templates/_module_image.tpl +++ b/charts/helm_lib/templates/_module_image.tpl @@ -3,29 +3,54 @@ {{- define "helm_lib_module_image" }} {{- $context := index . 0 }} {{- /* Template context with .Values, .Chart, etc */ -}} {{- $containerName := index . 1 | trimAll "\"" }} {{- /* Container name */ -}} - {{- $rawModuleName := $context.Chart.Name }} - {{- if ge (len .) 3 }} - {{- $rawModuleName = (index . 2) }} {{- /* Optional module name */ -}} - {{- end }} - {{- $moduleName := (include "helm_lib_module_camelcase_name" $rawModuleName) }} - {{- $imageDigest := index $context.Values.global.modulesImages.digests $moduleName $containerName }} - {{- if not $imageDigest }} - {{- $error := (printf "Image %s.%s has no digest" $moduleName $containerName ) }} - {{- fail $error }} - {{- end }} - {{- $registryBase := $context.Values.global.modulesImages.registry.base }} + + {{- /* New approach: use module package values */}} + {{- if and $context.Module $context.Module.Package }} + {{- $registryBase := $context.Module.Package.Registry.repository }} + {{- if not $registryBase }} + {{- fail "Registry base is not set" }} + {{- end }} + + {{- $packageName := $context.Module.Package.Name }} + {{- if not $packageName }} + {{- fail "Package name is not set" }} + {{- end }} + + {{- $imageDigest := index $context.Module.Package.Digests $containerName }} + {{- if not $imageDigest }} + {{- fail (printf "Image %s has no digest" $containerName) }} + {{- end }} + + {{- printf "%s/%s@%s" $registryBase $packageName $imageDigest }} + + {{- /* Legacy fallback: use global modulesImages values */}} + {{- else }} + {{- $rawModuleName := $context.Chart.Name }} + {{- if ge (len .) 3 }} + {{- $rawModuleName = (index . 2) }} {{- /* Optional module name */ -}} + {{- end }} + {{- $moduleName := (include "helm_lib_module_camelcase_name" $rawModuleName) }} + + {{- $imageDigest := index $context.Values.global.modulesImages.digests $moduleName $containerName }} + {{- if not $imageDigest }} + {{- $error := (printf "Image %s.%s has no digest" $moduleName $containerName ) }} + {{- fail $error }} + {{- end }} + + {{- $registryBase := $context.Values.global.modulesImages.registry.base }} {{- /* handle external modules registry */}} - {{- if index $context.Values $moduleName }} - {{- if index $context.Values $moduleName "registry" }} - {{- if index $context.Values $moduleName "registry" "base" }} - {{- $host := trimAll "/" (index $context.Values $moduleName "registry" "base") }} - {{- $path := trimAll "/" (include "helm_lib_module_kebabcase_name" $rawModuleName) }} - {{- $registryBase = join "/" (list $host $path) }} + {{- if index $context.Values $moduleName }} + {{- if index $context.Values $moduleName "registry" }} + {{- if index $context.Values $moduleName "registry" "base" }} + {{- $host := trimAll "/" (index $context.Values $moduleName "registry" "base") }} + {{- $path := trimAll "/" (include "helm_lib_module_kebabcase_name" $rawModuleName) }} + {{- $registryBase = join "/" (list $host $path) }} + {{- end }} {{- end }} {{- end }} - {{- end }} {{- /* end of external module handling block */}} - {{- printf "%s@%s" $registryBase $imageDigest }} + {{- printf "%s@%s" $registryBase $imageDigest }} + {{- end }} {{- end }} {{- /* Usage: {{ include "helm_lib_module_image_no_fail" (list . "") }} */ -}} diff --git a/tests/templates/helm_lib_module_image.yaml b/tests/templates/helm_lib_module_image.yaml index d840e64..9bcec02 100644 --- a/tests/templates/helm_lib_module_image.yaml +++ b/tests/templates/helm_lib_module_image.yaml @@ -1,3 +1,4 @@ +# Fallbacked logic embeddedModule: {{ include "helm_lib_module_image" (list . "testContainer") }} embeddedModuleWithOptionalName: {{ include "helm_lib_module_image" (list . "testContainer" "fooBar") }} --- @@ -22,3 +23,8 @@ moduleImageNoFailNotExist: {{ include "helm_lib_module_image_no_fail" (list . "n commonImage: {{ include "helm_lib_module_common_image" (list . "commonContainer") }} commonImageNoFail: {{ include "helm_lib_module_common_image_no_fail" (list . "commonContainer") }} commonImageNoFailNotExist: {{ include "helm_lib_module_common_image_no_fail" (list . "nonExistentCommonContainer") }} +--- +# New approach: use module package values +externalModuleImage: {{ include "helm_lib_module_image" (list .Values.new "testContainer") }} +externalModuleImageWithOptionalName: {{ include "helm_lib_module_image" (list .Values.new "testContainer" "fooBar") }} +externalModuleKebabImageWithOptionalName: {{ include "helm_lib_module_image" (list .Values.new "externalContainer" "some-module") }} diff --git a/tests/tests/helm_lib_module_image_test.yaml b/tests/tests/helm_lib_module_image_test.yaml index 313de51..3bced1a 100644 --- a/tests/tests/helm_lib_module_image_test.yaml +++ b/tests/tests/helm_lib_module_image_test.yaml @@ -2,6 +2,35 @@ suite: helm_lib_module_image definition templates: - helm_lib_module_image.yaml set: + new: + Chart: + Name: test-module + Module: + Package: + Name: "test-module" + Digests: + testContainer: "sha111" + externalContainer: "sha222" + commonContainer: "sha333" + Registry: + name: "deckhouse" + repository: "registry.deckhouse.io/deckhouse/ce" + Version: "1.0.0" + Deckhouse: + modulesImages: + registry: + base: "registry.deckhouse.io/ce/modules/" + digests: + testModule: + testContainer: "sha444" + externalContainer: "sha555" + fooBar: + testContainer: "sha666" + someModule: + externalContainer: "sha777" + common: + commonContainer: "sha888" + global: modulesImages: registry: @@ -17,6 +46,7 @@ set: common: commonContainer: "sha999" tests: + # Fallbacked logic - it: should render embedded module image documentIndex: 0 asserts: @@ -116,3 +146,17 @@ tests: - equal: path: "commonImageNoFailNotExist" value: null + + # New approach: use module package values + - it: should render external module image + documentIndex: 7 + asserts: + - equal: + path: "externalModuleImage" + value: "registry.deckhouse.io/deckhouse/ce/test-module@sha111" + - equal: + path: "externalModuleImageWithOptionalName" + value: "registry.deckhouse.io/deckhouse/ce/test-module@sha111" + - equal: + path: "externalModuleKebabImageWithOptionalName" + value: "registry.deckhouse.io/deckhouse/ce/test-module@sha222"