From 14374f5630bf09021cf1e0d84d52412387ef777e Mon Sep 17 00:00:00 2001 From: Kjeld Schouten-Lebbing Date: Mon, 22 Nov 2021 17:12:06 +0100 Subject: [PATCH] feat(common): Implement some simplifications from Bitnami (#1378) * fix(common): add bitnami translation layer for images and labels * port bitnami storage and tpl handling * hmm * add affinity-handling, warnings and fix issues * bump feature * hmm * slight alterations * add some more tests * fix small mistake * fixing a minor bug --- charts/library/common/Chart.yaml | 2 +- .../library/common/templates/_statefulset.tpl | 6 +- .../library/common/templates/classes/_pvc.tpl | 6 +- .../common/templates/lib/chart/_setup.tpl | 1 + .../templates/lib/utils/_affinities.tpl | 102 ++++++++++++++++++ .../common/templates/lib/utils/_images.tpl | 24 +++++ .../common/templates/lib/utils/_labels.tpl | 14 +++ .../common/templates/lib/utils/_storage.tpl | 39 +++++++ .../common/templates/lib/utils/_tplvalues.tpl | 13 +++ .../common/templates/lib/utils/_warnings.tpl | 14 +++ tests/library/common/pvc_spec.rb | 51 ++++++++- 11 files changed, 260 insertions(+), 12 deletions(-) create mode 100644 charts/library/common/templates/lib/utils/_affinities.tpl create mode 100644 charts/library/common/templates/lib/utils/_images.tpl create mode 100644 charts/library/common/templates/lib/utils/_labels.tpl create mode 100644 charts/library/common/templates/lib/utils/_storage.tpl create mode 100644 charts/library/common/templates/lib/utils/_tplvalues.tpl create mode 100644 charts/library/common/templates/lib/utils/_warnings.tpl diff --git a/charts/library/common/Chart.yaml b/charts/library/common/Chart.yaml index 57ace40de17..2ed2b98f17a 100644 --- a/charts/library/common/Chart.yaml +++ b/charts/library/common/Chart.yaml @@ -15,4 +15,4 @@ maintainers: name: common sources: null type: library -version: 8.8.1 +version: 8.9.0 diff --git a/charts/library/common/templates/_statefulset.tpl b/charts/library/common/templates/_statefulset.tpl index 267ed4d0dd1..9f4318ec7cd 100644 --- a/charts/library/common/templates/_statefulset.tpl +++ b/charts/library/common/templates/_statefulset.tpl @@ -63,10 +63,6 @@ spec: resources: requests: storage: {{ $vct.size | default "999Gi" | quote }} - {{- if $vct.storageClass }} - storageClassName: {{ if (eq "-" $vct.storageClass) }}""{{- else if (eq "SCALE-ZFS" $vct.storageClass ) }}{{ ( printf "%v-%v" "ix-storage-class" $releaseName ) }}{{- else }}{{ $vct.storageClass | quote }}{{- end }} - {{- else if or ( $values.global.isSCALE ) ( $values.ixChartContext ) ( $values.global.ixChartContext ) }} - storageClassName: {{ printf "%v-%v" "ix-storage-class" $releaseName }} - {{- end }} + {{ include "common.storage.class" ( dict "persistence" $vct "global" $) }} {{- end }} {{- end }} diff --git a/charts/library/common/templates/classes/_pvc.tpl b/charts/library/common/templates/classes/_pvc.tpl index fe7b2cc7aea..f677a27ccc8 100644 --- a/charts/library/common/templates/classes/_pvc.tpl +++ b/charts/library/common/templates/classes/_pvc.tpl @@ -43,11 +43,7 @@ spec: resources: requests: storage: {{ $values.size | default "999Gi" | quote }} - {{- if $values.storageClass }} - storageClassName: {{ if (eq "-" $values.storageClass) }}""{{- else if (eq "SCALE-ZFS" $values.storageClass ) }}{{ ( printf "%v-%v" "ix-storage-class" .Release.Name ) }}{{- else }}{{ $values.storageClass | quote }}{{- end }} - {{- else if or ( $.Values.global.isSCALE ) ( $.Values.ixChartContext ) ( $.Values.global.ixChartContext ) }} - storageClassName: {{ ( printf "%v-%v" "ix-storage-class" .Release.Name ) }} - {{- end }} + {{ include "common.storage.class" ( dict "persistence" $values "global" $ ) }} {{- if $values.volumeName }} volumeName: {{ $values.volumeName | quote }} {{- end }} diff --git a/charts/library/common/templates/lib/chart/_setup.tpl b/charts/library/common/templates/lib/chart/_setup.tpl index 034bc9f03fe..afa2147fcf0 100644 --- a/charts/library/common/templates/lib/chart/_setup.tpl +++ b/charts/library/common/templates/lib/chart/_setup.tpl @@ -1,4 +1,5 @@ {{- define "common.setup" -}} + {{- /* Merge the local chart values and the common chart defaults */ -}} {{- include "common.values" . }} diff --git a/charts/library/common/templates/lib/utils/_affinities.tpl b/charts/library/common/templates/lib/utils/_affinities.tpl new file mode 100644 index 00000000000..e85b8792dc0 --- /dev/null +++ b/charts/library/common/templates/lib/utils/_affinities.tpl @@ -0,0 +1,102 @@ +{{/* vim: set filetype=mustache: */}} + +{{/* +Return a soft nodeAffinity definition +{{ include "common.affinities.nodes.soft" (dict "key" "FOO" "values" (list "BAR" "BAZ")) -}} +*/}} +{{- define "common.affinities.nodes.soft" -}} +preferredDuringSchedulingIgnoredDuringExecution: + - preference: + matchExpressions: + - key: {{ .key }} + operator: In + values: + {{- range .values }} + - {{ . | quote }} + {{- end }} + weight: 1 +{{- end -}} + +{{/* +Return a hard nodeAffinity definition +{{ include "common.affinities.nodes.hard" (dict "key" "FOO" "values" (list "BAR" "BAZ")) -}} +*/}} +{{- define "common.affinities.nodes.hard" -}} +requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: {{ .key }} + operator: In + values: + {{- range .values }} + - {{ . | quote }} + {{- end }} +{{- end -}} + +{{/* +Return a nodeAffinity definition +{{ include "common.affinities.nodes" (dict "type" "soft" "key" "FOO" "values" (list "BAR" "BAZ")) -}} +*/}} +{{- define "common.affinities.nodes" -}} + {{- if eq .type "soft" }} + {{- include "common.affinities.nodes.soft" . -}} + {{- else if eq .type "hard" }} + {{- include "common.affinities.nodes.hard" . -}} + {{- end -}} +{{- end -}} + +{{/* +Return a soft podAffinity/podAntiAffinity definition +{{ include "common.affinities.pods.soft" (dict "component" "FOO" "extraMatchLabels" .Values.extraMatchLabels "context" $) -}} +*/}} +{{- define "common.affinities.pods.soft" -}} +{{- $component := default "" .component -}} +{{- $extraMatchLabels := default (dict) .extraMatchLabels -}} +preferredDuringSchedulingIgnoredDuringExecution: + - podAffinityTerm: + labelSelector: + matchLabels: {{- (include "common.labels.matchLabels" .context) | nindent 10 }} + {{- if not (empty $component) }} + {{ printf "app.kubernetes.io/component: %s" $component }} + {{- end }} + {{- range $key, $value := $extraMatchLabels }} + {{ $key }}: {{ $value | quote }} + {{- end }} + namespaces: + - {{ .context.Release.Namespace | quote }} + topologyKey: kubernetes.io/hostname + weight: 1 +{{- end -}} + +{{/* +Return a hard podAffinity/podAntiAffinity definition +{{ include "common.affinities.pods.hard" (dict "component" "FOO" "extraMatchLabels" .Values.extraMatchLabels "context" $) -}} +*/}} +{{- define "common.affinities.pods.hard" -}} +{{- $component := default "" .component -}} +{{- $extraMatchLabels := default (dict) .extraMatchLabels -}} +requiredDuringSchedulingIgnoredDuringExecution: + - labelSelector: + matchLabels: {{- (include "common.labels.matchLabels" .context) | nindent 8 }} + {{- if not (empty $component) }} + {{ printf "app.kubernetes.io/component: %s" $component }} + {{- end }} + {{- range $key, $value := $extraMatchLabels }} + {{ $key }}: {{ $value | quote }} + {{- end }} + namespaces: + - {{ .context.Release.Namespace | quote }} + topologyKey: kubernetes.io/hostname +{{- end -}} + +{{/* +Return a podAffinity/podAntiAffinity definition +{{ include "common.affinities.pods" (dict "type" "soft" "key" "FOO" "values" (list "BAR" "BAZ")) -}} +*/}} +{{- define "common.affinities.pods" -}} + {{- if eq .type "soft" }} + {{- include "common.affinities.pods.soft" . -}} + {{- else if eq .type "hard" }} + {{- include "common.affinities.pods.hard" . -}} + {{- end -}} +{{- end -}} diff --git a/charts/library/common/templates/lib/utils/_images.tpl b/charts/library/common/templates/lib/utils/_images.tpl new file mode 100644 index 00000000000..4a2698aa41b --- /dev/null +++ b/charts/library/common/templates/lib/utils/_images.tpl @@ -0,0 +1,24 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Return the proper image name +{{ include "common.images.image" ( dict "imageRoot" .Values.path.to.the.image "global" $) }} +*/}} +{{- define "common.images.image" -}} +{{- $repositoryName := .imageRoot.repository -}} +{{- $tag := .imageRoot.tag | toString -}} +{{- printf "%s:%s" $repositoryName $tag -}} +{{- end -}} + +{{/* +Return the proper Docker Image Registry Secret Names (deprecated: use common.images.renderPullSecrets instead) +{{ include "common.images.pullSecrets" ( dict "images" (list .Values.path.to.the.image1, .Values.path.to.the.image2) "global" .Values.global) }} +*/}} +{{- define "common.images.pullSecrets" -}} +{{- end -}} + +{{/* +Return the proper Docker Image Registry Secret Names evaluating values as templates +{{ include "common.images.renderPullSecrets" ( dict "images" (list .Values.path.to.the.image1, .Values.path.to.the.image2) "context" $) }} +*/}} +{{- define "common.images.renderPullSecrets" -}} +{{- end -}} diff --git a/charts/library/common/templates/lib/utils/_labels.tpl b/charts/library/common/templates/lib/utils/_labels.tpl new file mode 100644 index 00000000000..eab03905101 --- /dev/null +++ b/charts/library/common/templates/lib/utils/_labels.tpl @@ -0,0 +1,14 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Kubernetes standard labels +*/}} +{{- define "common.labels.standard" -}} +{{- include "common.labels" . }} +{{- end -}} + +{{/* +Labels to use on deploy.spec.selector.matchLabels and svc.spec.selector +*/}} +{{- define "common.labels.matchLabels" -}} +{{- include "common.labels.selectorLabels" . }} +{{- end -}} diff --git a/charts/library/common/templates/lib/utils/_storage.tpl b/charts/library/common/templates/lib/utils/_storage.tpl new file mode 100644 index 00000000000..52e25089852 --- /dev/null +++ b/charts/library/common/templates/lib/utils/_storage.tpl @@ -0,0 +1,39 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Return the proper Storage Class +{{ include "common.storage.class" ( dict "persistence" .Values.path.to.the.persistence "global" $ ) }} +*/}} +{{- define "common.storage.class" -}} + +{{- $storageClass := .persistence.storageClass -}} + +{{- if $storageClass -}} + {{- if (eq "-" $storageClass) -}} + {{- printf "storageClassName: \"\"" -}} + {{- else if (eq "SCALE-ZFS" $storageClass ) }} + {{- ( printf "storageClassName: ix-storage-class-%s" .global.Release.Name ) -}} + {{- else }} + {{- printf "storageClassName: %s" $storageClass -}} + {{- end -}} +{{- end -}} + +{{- if .global }} +{{- if .global.Values.global -}} + {{- if .global.Values.global.storageClass -}} + {{- $storageClass = .global.Values.global.storageClass -}} + {{- end -}} + {{- if or ( .global.Values.global.ixChartContext ) ( .global.Values.global.isSCALE ) -}} + {{- ( printf "storageClassName: ix-storage-class-%s" .global.Release.Name ) -}} + {{- end }} +{{- end -}} + +{{- if .global.Values.storageClass -}} + {{- $storageClass = .global.Values.storageClass -}} +{{- end -}} + +{{- if .global.Values.ixChartContext }} + {{- ( printf "storageClassName: ix-storage-class-%s" .global.Release.Name ) -}} +{{- end }} +{{- end }} + +{{- end -}} diff --git a/charts/library/common/templates/lib/utils/_tplvalues.tpl b/charts/library/common/templates/lib/utils/_tplvalues.tpl new file mode 100644 index 00000000000..2db166851bb --- /dev/null +++ b/charts/library/common/templates/lib/utils/_tplvalues.tpl @@ -0,0 +1,13 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Renders a value that contains template. +Usage: +{{ include "common.tplvalues.render" ( dict "value" .Values.path.to.the.Value "context" $) }} +*/}} +{{- define "common.tplvalues.render" -}} + {{- if typeIs "string" .value }} + {{- tpl .value .context }} + {{- else }} + {{- tpl (.value | toYaml) .context }} + {{- end }} +{{- end -}} diff --git a/charts/library/common/templates/lib/utils/_warnings.tpl b/charts/library/common/templates/lib/utils/_warnings.tpl new file mode 100644 index 00000000000..464afa4cc93 --- /dev/null +++ b/charts/library/common/templates/lib/utils/_warnings.tpl @@ -0,0 +1,14 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Warning about using rolling tag. +Usage: +{{ include "common.warnings.rollingTag" .Values.path.to.the.imageRoot }} +*/}} +{{- define "common.warnings.rollingTag" -}} + +{{- if not (.tag | toString | regexFind "-r\\d+$|sha256:") }} +WARNING: Rolling tag detected ({{ .repository }}:{{ .tag }}), please note that it is strongly recommended to avoid using rolling tags in a production environment. ++info https://docs.bitnami.com/containers/how-to/understand-rolling-tags-containers/ +{{- end }} + +{{- end -}} diff --git a/tests/library/common/pvc_spec.rb b/tests/library/common/pvc_spec.rb index cd45e1cb8e4..28bcf1adaac 100644 --- a/tests/library/common/pvc_spec.rb +++ b/tests/library/common/pvc_spec.rb @@ -62,7 +62,7 @@ class Test < ChartTest assert_equal('test', pvc["spec"]["storageClassName"]) end - it 'can generate TrueNAS SCALE zfs storageClass' do + it 'can generate TrueNAS SCALE zfs storageClass on demand' do values = { persistence: { config: { @@ -77,6 +77,55 @@ class Test < ChartTest assert_equal('ix-storage-class-common-test', pvc["spec"]["storageClassName"]) end + it 'generate TrueNAS SCALE zfs storageClass as default when isSCALE' do + values = { + global: { + isSCALE: true + }, + persistence: { + config: { + enabled: true + } + } + } + chart.value values + pvc = chart.resources(kind: "PersistentVolumeClaim").find{ |s| s["metadata"]["name"] == "common-test-config" } + refute_nil(pvc) + assert_equal('ix-storage-class-common-test', pvc["spec"]["storageClassName"]) + end + + it 'generate TrueNAS SCALE zfs storageClass as default when global ixChartContext' do + values = { + global: { + ixChartContext: "somethingsomething" + }, + persistence: { + config: { + enabled: true + } + } + } + chart.value values + pvc = chart.resources(kind: "PersistentVolumeClaim").find{ |s| s["metadata"]["name"] == "common-test-config" } + refute_nil(pvc) + assert_equal('ix-storage-class-common-test', pvc["spec"]["storageClassName"]) + end + + it 'generate TrueNAS SCALE zfs storageClass as default when ixChartContext' do + values = { + ixChartContext: "somethingsomething", + persistence: { + config: { + enabled: true + } + } + } + chart.value values + pvc = chart.resources(kind: "PersistentVolumeClaim").find{ |s| s["metadata"]["name"] == "common-test-config" } + refute_nil(pvc) + assert_equal('ix-storage-class-common-test', pvc["spec"]["storageClassName"]) + end + it 'storageClass can be set to an empty value' do values = { persistence: {