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
This commit is contained in:
Kjeld Schouten-Lebbing 2021-11-22 17:12:06 +01:00 committed by GitHub
parent 9b6ae5ae4f
commit 14374f5630
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 260 additions and 12 deletions

View File

@ -15,4 +15,4 @@ maintainers:
name: common
sources: null
type: library
version: 8.8.1
version: 8.9.0

View File

@ -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 }}

View File

@ -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 }}

View File

@ -1,4 +1,5 @@
{{- define "common.setup" -}}
{{- /* Merge the local chart values and the common chart defaults */ -}}
{{- include "common.values" . }}

View File

@ -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 -}}

View File

@ -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 -}}

View File

@ -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 -}}

View File

@ -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 -}}

View File

@ -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 -}}

View File

@ -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 -}}

View File

@ -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: {