diff --git a/library/common-testing/templates/common.yaml b/library/common-testing/templates/common.yaml index e69de29bb2..cb793047c6 100644 --- a/library/common-testing/templates/common.yaml +++ b/library/common-testing/templates/common.yaml @@ -0,0 +1 @@ +{{- include "ix.v1.common.loader.all" . -}} diff --git a/library/common-testing/tests/configmap/data_test.yaml b/library/common-testing/tests/configmap/data_test.yaml new file mode 100644 index 0000000000..fa7641b24e --- /dev/null +++ b/library/common-testing/tests/configmap/data_test.yaml @@ -0,0 +1,75 @@ +suite: configmap data test +templates: + - common.yaml +tests: + - it: should pass with key-value data + set: + configmaps: + my-configmap1: + enabled: true + data: + foo: bar + asserts: + - documentIndex: &configmapDoc 0 + isKind: + of: ConfigMap + - documentIndex: *configmapDoc + equal: + path: data + value: + foo: bar + + - it: should pass with key-value data from tpl + set: + data: bar + configmaps: + my-configmap1: + enabled: true + data: + foo: "{{ .Values.data }}" + asserts: + - documentIndex: *configmapDoc + equal: + path: data + value: + foo: bar + + - it: should pass with scalar data + set: + configmaps: + my-configmap1: + enabled: true + data: + foo: | + some multi line + string text + asserts: + - documentIndex: &configmapDoc 0 + isKind: + of: ConfigMap + - documentIndex: *configmapDoc + equal: + path: data + value: + foo: |- + some multi line + string text + + - it: should pass with scalar data with tpl + set: + data: some_text + configmaps: + my-configmap1: + enabled: true + data: + foo: | + Some other text + {{ .Values.data }} + asserts: + - documentIndex: *configmapDoc + equal: + path: data + value: + foo: |- + Some other text + some_text diff --git a/library/common-testing/tests/configmap/labels_test.yaml b/library/common-testing/tests/configmap/labels_test.yaml new file mode 100644 index 0000000000..e834c6609a --- /dev/null +++ b/library/common-testing/tests/configmap/labels_test.yaml @@ -0,0 +1,54 @@ +suite: configmap test +templates: + - common.yaml +chart: + appVersion: &appVer v9.9.9 +tests: + - it: should pass with configmap created with labels and annotations + documentIndex: &configMapDoc 0 + set: + label1: label1 + label2: global_label2 + annotation1: annotation1 + annotation2: global_annotation2 + global: + labels: + g_label1: global_label1 + g_label2: "{{ .Values.label2 }}" + annotations: + g_annotation1: global_annotation1 + g_annotation2: "{{ .Values.annotation2 }}" + configmaps: + my-configmap1: + enabled: true + labels: + label1: "{{ .Values.label1 }}" + label2: label2 + annotations: + annotation1: "{{ .Values.annotation1 }}" + annotation2: annotation2 + data: + foo: bar + asserts: + - isKind: + of: ConfigMap + - equal: + path: metadata.annotations + value: + annotation1: annotation1 + annotation2: annotation2 + g_annotation1: global_annotation1 + g_annotation2: global_annotation2 + - equal: + path: metadata.labels + value: + app: common-test-1.0.0 + release: release-name + helm-revision: 0 + helm.sh/chart: common-test-1.0.0 + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/version: v9.9.9 + g_label1: global_label1 + g_label2: global_label2 + label1: label1 + label2: label2 diff --git a/library/common-testing/tests/configmap/name_test.yaml b/library/common-testing/tests/configmap/name_test.yaml new file mode 100644 index 0000000000..70c3dd7053 --- /dev/null +++ b/library/common-testing/tests/configmap/name_test.yaml @@ -0,0 +1,36 @@ +suite: configmap name test +templates: + - common.yaml +tests: + - it: should generate correct name + set: + configmaps: + my-configmap1: + enabled: true + data: + foo: bar + my-configmap2: + enabled: true + data: + foo: bar + asserts: + - documentIndex: &configmapDoc 0 + isKind: + of: ConfigMap + - documentIndex: *configmapDoc + isAPIVersion: + of: v1 + - documentIndex: *configmapDoc + equal: + path: metadata.name + value: release-name-common-test-my-configmap1 + - documentIndex: &configmapDoc2 1 + isKind: + of: ConfigMap + - documentIndex: *configmapDoc2 + isAPIVersion: + of: v1 + - documentIndex: *configmapDoc2 + equal: + path: metadata.name + value: release-name-common-test-my-configmap2 diff --git a/library/common-testing/tests/configmap/validation_test.yaml b/library/common-testing/tests/configmap/validation_test.yaml new file mode 100644 index 0000000000..35283486b5 --- /dev/null +++ b/library/common-testing/tests/configmap/validation_test.yaml @@ -0,0 +1,69 @@ +suite: configmap validation test +templates: + - common.yaml +tests: + - it: should fail with name longer than 63 characters + set: + configmaps: + my-configmap-super-long-name-that-is-longer-than-63-characters: + enabled: true + data: + foo: bar + asserts: + - failedTemplate: + errorMessage: Name [release-name-common-test-my-configmap-super-long-name-that-is-longer-than-63-characters] is not valid. Must start and end with an alphanumeric character. It can contain '-'. And must be at most 63 characters. + + - it: should fail with name starting with underscore + set: + configmaps: + _my-configmap: + enabled: true + data: + foo: bar + asserts: + - failedTemplate: + errorMessage: Name [release-name-common-test-_my-configmap] is not valid. Must start and end with an alphanumeric character. It can contain '-'. And must be at most 63 characters. + + - it: should fail with labels not a dict + set: + configmaps: + my-configmap: + enabled: true + labels: "not a dict" + data: + foo: bar + asserts: + - failedTemplate: + errorMessage: Configmap - Expected to be a dictionary, but got [string] + + - it: should fail with annotations not a dict + set: + configmaps: + my-configmap: + enabled: true + annotations: "not a dict" + data: + foo: bar + asserts: + - failedTemplate: + errorMessage: Configmap - Expected to be a dictionary, but got [string] + + - it: should fail with data not a dict + set: + configmaps: + my-configmap: + enabled: true + data: "not a dict" + asserts: + - failedTemplate: + errorMessage: Configmap - Expected to be a dictionary, but got [string] + + - it: should fail with empty data + set: + configmaps: + my-configmap: + enabled: true + data: {} + asserts: + - failedTemplate: + errorMessage: Configmap - Expected non-empty diff --git a/library/common/1.0.1/docs/configmaps.md b/library/common/1.0.1/docs/configmaps.md index 3db6ae659e..a0645f09cf 100644 --- a/library/common/1.0.1/docs/configmaps.md +++ b/library/common/1.0.1/docs/configmaps.md @@ -19,7 +19,7 @@ Appears in: Naming scheme: -- `$FullName-$ConfigmapName` +- `$FullName-$ConfigmapName` (release-name-chart-name-configmapName) --- @@ -43,7 +43,6 @@ configmaps: enabled: true data: key: | + multi line text value - - ``` diff --git a/library/common/1.0.1/templates/classes/_configmap.tpl b/library/common/1.0.1/templates/classes/_configmap.tpl index 7e9f2ebee2..e09251db06 100644 --- a/library/common/1.0.1/templates/classes/_configmap.tpl +++ b/library/common/1.0.1/templates/classes/_configmap.tpl @@ -1,6 +1,34 @@ {{/* Configmap Class */}} {{/* Call this template: +{{ include "ix.v1.common.class.configmap" (dict "objectData" $objectData "rootCtx" $) }} +rootCtx: The root context of the template. It is used to access the global context. +objectData: + name: The name of the configmap. + labels: The labels of the configmap. + annotations: The annotations of the configmap. + data: The data of the configmap. */}} + {{- define "ix.v1.common.class.configmap" -}} + + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData }} +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ $objectData.name }} + {{- $labels := (mustMerge ($objectData.labels | default dict) (include "ix.v1.common.lib.metadata.allLabels" $rootCtx | fromYaml)) -}} + {{- with (include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }} + labels: + {{- . | nindent 4 }} + {{- end }} + {{- $annotations := (mustMerge ($objectData.annotations | default dict) (include "ix.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml)) -}} + {{- with (include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "annotations" $annotations) | trim) }} + annotations: + {{- . | nindent 4 }} + {{- end }} +data: + {{- tpl (toYaml $objectData.data) $rootCtx | nindent 2 }} {{- end -}} diff --git a/library/common/1.0.1/templates/lib/chart/_names.tpl b/library/common/1.0.1/templates/lib/chart/_names.tpl index 5d76647247..5e37851f56 100644 --- a/library/common/1.0.1/templates/lib/chart/_names.tpl +++ b/library/common/1.0.1/templates/lib/chart/_names.tpl @@ -3,7 +3,7 @@ {{/* Returns the name of the Chart */}} {{- define "ix.common.lib.chart.names.name" -}} - {{- .Chart.Name | trunc 63 | trimSuffix "-" -}} + {{- .Chart.Name | lower | trunc 63 | trimSuffix "-" -}} {{- end -}} @@ -12,12 +12,30 @@ {{- $name := include "ix.common.lib.chart.names.name" . -}} - {{- if contains $name .Release.name -}} + {{- if contains $name .Release.Name -}} {{- $name = .Release.Name -}} {{- else -}} {{- $name = printf "%s-%s" .Release.Name $name -}} {{- end -}} - {{- $name | trunc 63 | trimSuffix "-" -}} + {{- $name | lower | trunc 63 | trimSuffix "-" -}} + +{{- end -}} + +{{/* Validates names */}} +{{- define "ix.v1.common.lib.chart.names.validation" -}} + + {{- $name := .name -}} + + {{- if not (mustRegexMatch "^[a-z0-9]([a-z0-9-]){1,61}[a-z0-9]$" $name) -}} + {{- fail (printf "Name [%s] is not valid. Must start and end with an alphanumeric character. It can contain '-'. And must be at most 63 characters." $name) -}} + {{- end -}} + +{{- end -}} + +{{/* Create chart name and version as used by the chart label */}} +{{- define "ix.v1.common.lib.chart.names.chart" -}} + + {{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} {{- end -}} diff --git a/library/common/1.0.1/templates/lib/chart/_notes.tpl b/library/common/1.0.1/templates/lib/chart/_notes.tpl index 93cf53f514..b7b6a34450 100644 --- a/library/common/1.0.1/templates/lib/chart/_notes.tpl +++ b/library/common/1.0.1/templates/lib/chart/_notes.tpl @@ -9,11 +9,11 @@ {{- end -}} {{- define "ix.v1.common.lib.chart.header" -}} - {{- tpl $.Values.common.notes.header $ | nindent 0 }} + {{- tpl $.Values.notes.header $ | nindent 0 }} {{- end -}} {{- define "ix.v1.common.lib.chart.custom" -}} - {{- tpl $.Values.common.notes.header $ | nindent 0 }} + {{- tpl $.Values.notes.header $ | nindent 0 }} {{- end -}} {{- define "ix.v1.common.lib.chart.footer" -}} - {{- tpl $.Values.common.notes.header $ | nindent 0 }} + {{- tpl $.Values.notes.header $ | nindent 0 }} {{- end -}} diff --git a/library/common/1.0.1/templates/lib/configmap/_validation.tpl b/library/common/1.0.1/templates/lib/configmap/_validation.tpl new file mode 100644 index 0000000000..0163b526c5 --- /dev/null +++ b/library/common/1.0.1/templates/lib/configmap/_validation.tpl @@ -0,0 +1,30 @@ +{{/* Configmap Validation */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.configmap.validation" (dict "objectData" $objectData) -}} +objectData: + name: The name of the configmap. + labels: The labels of the configmap. + annotations: The annotations of the configmap. + data: The data of the configmap. +*/}} + +{{- define "ix.v1.common.lib.configmap.validation" -}} + {{- $objectData := .objectData -}} + + {{- if not $objectData.data -}} + {{- fail "Configmap - Expected non-empty " -}} + {{- end -}} + + {{- if not (kindIs "map" $objectData.data) -}} + {{- fail (printf "Configmap - Expected to be a dictionary, but got [%v]" (kindOf $objectData.data)) -}} + {{- end -}} + + {{- if and $objectData.labels (not (kindIs "map" $objectData.labels)) -}} + {{- fail (printf "Configmap - Expected to be a dictionary, but got [%v]" (kindOf $objectData.labels)) -}} + {{- end -}} + + {{- if and $objectData.annotations (not (kindIs "map" $objectData.annotations)) -}} + {{- fail (printf "Configmap - Expected to be a dictionary, but got [%v]" (kindOf $objectData.annotations)) -}} + {{- end -}} + +{{- end -}} diff --git a/library/common/1.0.1/templates/lib/metadata/_allAnnotations.tpl b/library/common/1.0.1/templates/lib/metadata/_allAnnotations.tpl new file mode 100644 index 0000000000..408f7bb348 --- /dev/null +++ b/library/common/1.0.1/templates/lib/metadata/_allAnnotations.tpl @@ -0,0 +1,9 @@ +{{/* Annotations that are added to all objects */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.metadata.allAnnotations" . }} +*/}} +{{- define "ix.v1.common.lib.metadata.allAnnotations" -}} + {{/* Currently empty but can add later, if needed */}} +{{- include "ix.v1.common.lib.metadata.globalAnnotations" . }} + +{{- end -}} diff --git a/library/common/1.0.1/templates/lib/metadata/_allLabels.tpl b/library/common/1.0.1/templates/lib/metadata/_allLabels.tpl new file mode 100644 index 0000000000..4a71f26c93 --- /dev/null +++ b/library/common/1.0.1/templates/lib/metadata/_allLabels.tpl @@ -0,0 +1,13 @@ +{{/* Labels that are added to all objects */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.metadata.allLabels" . }} +*/}} +{{- define "ix.v1.common.lib.metadata.allLabels" -}} +helm.sh/chart: {{ include "ix.v1.common.lib.chart.names.chart" . }} +helm-revision: {{ .Release.Revision | quote }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +app: {{ include "ix.v1.common.lib.chart.names.chart" . }} +release: {{ .Release.Name | lower }} +{{- include "ix.v1.common.lib.metadata.globalLabels" . }} +{{- end -}} diff --git a/library/common/1.0.1/templates/lib/metadata/_globalAnnotations.tpl b/library/common/1.0.1/templates/lib/metadata/_globalAnnotations.tpl new file mode 100644 index 0000000000..c05c24bc79 --- /dev/null +++ b/library/common/1.0.1/templates/lib/metadata/_globalAnnotations.tpl @@ -0,0 +1,6 @@ +{{/* Returns the global annotations */}} +{{- define "ix.v1.common.lib.metadata.globalAnnotations" -}} + + {{- include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $ "annotations" .Values.global.annotations) -}} + +{{- end -}} diff --git a/library/common/1.0.1/templates/lib/metadata/_globalLabels.tpl b/library/common/1.0.1/templates/lib/metadata/_globalLabels.tpl new file mode 100644 index 0000000000..18da33a798 --- /dev/null +++ b/library/common/1.0.1/templates/lib/metadata/_globalLabels.tpl @@ -0,0 +1,6 @@ +{{/* Returns the global labels */}} +{{- define "ix.v1.common.lib.metadata.globalLabels" -}} + + {{- include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $ "labels" .Values.global.labels) -}} + +{{- end -}} diff --git a/library/common/1.0.1/templates/lib/metadata/_render.tpl b/library/common/1.0.1/templates/lib/metadata/_render.tpl new file mode 100644 index 0000000000..340396e282 --- /dev/null +++ b/library/common/1.0.1/templates/lib/metadata/_render.tpl @@ -0,0 +1,28 @@ +{{/* Renders a dict of labels */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $ "labels" $labels) }} +{{ include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $ "annotations" $annotations) }} +*/}} + +{{- define "ix.v1.common.lib.metadata.render" -}} + {{- $labels := .labels -}} + {{- $annotations := .annotations -}} + {{- $rootCtx := .rootCtx -}} + + {{- with $labels -}} + {{- range $k, $v := . -}} + {{- if and $k $v }} +{{ $k }}: {{ tpl $v $rootCtx }} + {{- end -}} + {{- end -}} + {{- end -}} + + {{- with $annotations -}} + {{- range $k, $v := . -}} + {{- if and $k $v }} +{{ $k }}: {{ tpl $v $rootCtx }} + {{- end -}} + {{- end -}} + {{- end -}} + +{{- end -}} diff --git a/library/common/1.0.1/templates/spawner/_configmap.tpl b/library/common/1.0.1/templates/spawner/_configmap.tpl index ea46777a63..461f399ce7 100644 --- a/library/common/1.0.1/templates/spawner/_configmap.tpl +++ b/library/common/1.0.1/templates/spawner/_configmap.tpl @@ -12,8 +12,13 @@ {{/* Create a copy of the configmap */}} {{- $objectData := (mustDeepCopy $configmap) -}} + {{- $objectName := (printf "%s-%s" (include "ix.common.lib.chart.names.fullname" $) $name) -}} + {{/* Perform validations */}} + {{- include "ix.v1.common.lib.chart.names.validation" (dict "name" $objectName) -}} + {{- include "ix.v1.common.lib.configmap.validation" (dict "objectData" $objectData) -}} + {{/* Set the name of the configmap */}} - {{- $_ := set $objectData "name" (printf "%s-%s" (include "ix.common.lib.chart.names.fullname" $) $name) -}} + {{- $_ := set $objectData "name" $objectName -}} {{/* Call class to create the object */}} {{- include "ix.v1.common.class.configmap" (dict "objectData" $objectData "rootCtx" $) -}} diff --git a/library/common/1.0.1/values.yaml b/library/common/1.0.1/values.yaml index 6b28041ebe..e74ecdbc30 100644 --- a/library/common/1.0.1/values.yaml +++ b/library/common/1.0.1/values.yaml @@ -36,19 +36,17 @@ image: pullPolicy: IfNotPresent # -- ConfigMaps -configmaps: - # -- ConfigMap name - conf-name: - # -- Enables the ConfigMap - enabled: false - # -- ConfigMap labels - labels: {} - # -- ConfigMap annotations - annotations: {} - # -- ConfigMap data - data: - # -- ConfigMap data - key: value +configmaps: {} + # # -- ConfigMap name + # conf-name: + # # -- Enables the ConfigMap + # enabled: false + # # -- ConfigMap labels + # labels: {} + # # -- ConfigMap annotations + # annotations: {} + # # -- ConfigMap data + # data: {} # -- Secrets secrets: