From b5ca927a2cba8bb08fdf4df29c26e49d4370bd66 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Sun, 10 Mar 2024 01:40:10 +0200 Subject: [PATCH] cleanup --- .../common/templates/class/cnpg/_cluster.tpl | 27 +++++++++++------ .../common/templates/lib/cnpg/_pgVersion.tpl | 4 +-- library/common/templates/spawner/_cnpg.tpl | 29 ++++++++++--------- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/library/common/templates/class/cnpg/_cluster.tpl b/library/common/templates/class/cnpg/_cluster.tpl index 5ef8d540..f2d487a9 100644 --- a/library/common/templates/class/cnpg/_cluster.tpl +++ b/library/common/templates/class/cnpg/_cluster.tpl @@ -132,16 +132,25 @@ {{- $walAccessModes = . -}} {{- end -}} - {{/* Ensure version and container tracking */}} - {{- $imageType := camelcase ( $objectData.type | default "postgres" ) }} - {{- if eq $imageType "Postgres" }} - {{- $imageType = "" }} - {{- end }} - {{- $imageKey := printf "postgres%v%vImage" $imageType $objectData.pgversion }} - {{- $imageValue := fromJson (include "tc.v1.common.lib.container.imageSelector" (dict "rootCtx" $rootCtx "objectData" (dict "imageSelector" $imageKey))) }} - {{- $formatImage := printf "%s:%s" $imageValue.repository $imageValue.tag }} + {{- $imageName := $objectData.cluster.imageName -}} + {{- if not $imageName -}} + {{/* Ensure version and container tracking */}} + {{- $imageType := camelcase ($objectData.type | default "postgres") -}} + {{- if eq $imageType "Postgres" -}} + {{- $imageType = "" -}} + {{- end -}} + {{- $pgVersion := $rootCtx.Values.global.fallbackDefaults.pgVersion -}} + {{- with $objectData.pgVersion -}} + {{- $pgVersion = . -}} + {{- end -}} - {{- $imageName := $objectData.cluster.imageName | default $formatImage }} + {{/* Format is [postgresCustomNameVersionImage] */}} + {{- $imageKey := printf "postgres%s%sImage" $imageType $pgVersion -}} + {{- $imageValue := fromJson (include "tc.v1.common.lib.container.imageSelector" (dict "rootCtx" $rootCtx "objectData" (dict "imageSelector" $imageKey))) -}} + {{- $formatImage := printf "%s:%s" $imageValue.repository $imageValue.tag -}} + + {{- $imageName = $formatImage -}} + {{- end -}} {{- include "tc.v1.common.lib.util.verifycrd" (dict "rootCtx" $rootCtx "crd" "clusters.postgresql.cnpg.io" "missing" "CloudNative-PG") }} --- diff --git a/library/common/templates/lib/cnpg/_pgVersion.tpl b/library/common/templates/lib/cnpg/_pgVersion.tpl index 676398b3..267fc5ec 100644 --- a/library/common/templates/lib/cnpg/_pgVersion.tpl +++ b/library/common/templates/lib/cnpg/_pgVersion.tpl @@ -1,6 +1,6 @@ {{- define "tc.v1.common.lib.cnpg.configmap.pgVersion" -}} - {{- $major := .major }} +{{- $version := .version }} enabled: true data: - major: {{ $major | quote }} + version: {{ $version | quote }} {{- end -}} diff --git a/library/common/templates/spawner/_cnpg.tpl b/library/common/templates/spawner/_cnpg.tpl index 6cae3b93..083cc913 100644 --- a/library/common/templates/spawner/_cnpg.tpl +++ b/library/common/templates/spawner/_cnpg.tpl @@ -24,24 +24,25 @@ {{- if eq $enabled "true" -}} {{/* Handle version string */}} - {{- $pgversion := toString ( $objectData.version | default $.Values.global.fallbackDefaults.pgversion ) -}} + {{- $pgVersion := $objectData.pgVersion | default $.Values.global.fallbackDefaults.pgVersion -}} {{- $versionConfigMapName := printf "cnpg-%s-pgversion" $objectData.shortName -}} - + {{/* If there are previous configmap, fetch value */}} - {{- with (lookup "v1" "ConfigMap" $.Release.Namespace ( printf "%s-%s" $fullname $versionConfigMapName )) -}} - {{/* If a new major is set and upgrade is enabled, upgrade */}} - {{- if and ( ne $pgversion .data.major ) $objectData.upgradeMajor -}} - {{/* TODO handle postgresql major updates here */}} - {{- else if .data.major -}} - {{- $pgversion = ( toString .data.major ) -}} + {{- with (lookup "v1" "ConfigMap" $.Release.Namespace (printf "%s-%s" $fullname $versionConfigMapName)) -}} + {{/* If a different version is set and upgrade is enabled, upgrade */}} + {{- if and (ne $pgVersion .data.version) $objectData.upgradeMajor -}} + {{/* TODO: maybe check that the version is newer and not older? */}} + {{/* TODO: actually handle postgres version updates here */}} + {{- else -}} + {{- $pgVersion = .data.version -}} {{- end -}} {{- end -}} - - {{/* append the pg (major) version to objectData */}} - {{- $_ := set $objectData "pgversion" $pgversion -}} - - {{/* ensure configmap with pg version is updated */}} - {{- $verConfig := include "tc.v1.common.lib.cnpg.configmap.pgversion" (dict "major" $pgversion ) | fromYaml -}} + + {{/* Set the updated pgVersion version to objectData */}} + {{- $_ := set $objectData "pgVersion" $pgVersion -}} + + {{/* Ensure configmap with pgVersion is updated */}} + {{- $verConfig := include "tc.v1.common.lib.cnpg.configmap.pgVersion" (dict "version" $pgVersion) | fromYaml -}} {{- $_ := set $.Values.configmap $versionConfigMapName $verConfig -}} {{- include "tc.v1.common.lib.util.metaListToDict" (dict "objectData" $objectData) -}}