From cc4382db500f1b8de1f04d306b9bf5350a7e600d Mon Sep 17 00:00:00 2001 From: aakcht Date: Mon, 25 Jul 2022 14:51:23 +0300 Subject: [PATCH 1/2] Chart: custom labels for extrasecrets/configmaps --- .../configmaps/extra-configmaps.yaml | 3 ++ chart/templates/secrets/extra-secrets.yaml | 3 ++ chart/values.schema.json | 20 ++++++++++- chart/values.yaml | 8 +++-- tests/charts/test_extra_configmaps_secrets.py | 36 +++++++++++++++++++ 5 files changed, 67 insertions(+), 3 deletions(-) diff --git a/chart/templates/configmaps/extra-configmaps.yaml b/chart/templates/configmaps/extra-configmaps.yaml index 5daac57660468..58805a827a81d 100644 --- a/chart/templates/configmaps/extra-configmaps.yaml +++ b/chart/templates/configmaps/extra-configmaps.yaml @@ -31,6 +31,9 @@ metadata: heritage: {{ $Global.Release.Service }} {{- with $Global.Values.labels }} {{ toYaml . | indent 4 }} +{{- end }} +{{- if $configMapContent.labels }} +{{ toYaml $configMapContent.labels | indent 4 }} {{- end }} annotations: "helm.sh/hook": "pre-install,pre-upgrade" diff --git a/chart/templates/secrets/extra-secrets.yaml b/chart/templates/secrets/extra-secrets.yaml index 20c1751d96b43..40c56306f2dc8 100644 --- a/chart/templates/secrets/extra-secrets.yaml +++ b/chart/templates/secrets/extra-secrets.yaml @@ -31,6 +31,9 @@ metadata: heritage: {{ $Global.Release.Service }} {{- with $Global.Values.labels }} {{ toYaml . | indent 4 }} +{{- end }} +{{- if $secretContent.labels }} +{{ toYaml $secretContent.labels | indent 4 }} {{- end }} annotations: "helm.sh/hook": "pre-install,pre-upgrade" diff --git a/chart/values.schema.json b/chart/values.schema.json index b24e8fcf0c7cc..803aa923bb1c7 100644 --- a/chart/values.schema.json +++ b/chart/values.schema.json @@ -857,6 +857,15 @@ "description": "Type **as string** of secret E.G. Opaque, kubernetes.io/dockerconfigjson, etc.", "type": "string" }, + "labels": { + "description": "Labels for the secret", + "type": "object", + "additionalProperties": { + "description": "Label key", + "default": null, + "type": "string" + } + }, "data": { "description": "Content **as string** for the 'data' item of the secret (can be templated)", "type": "string" @@ -887,8 +896,17 @@ "minProperties": 1, "additionalProperties": false, "properties": { + "labels": { + "description": "Labels for the configmap", + "type": "object", + "additionalProperties": { + "description": "Label key", + "default": null, + "type": "string" + } + }, "data": { - "description": "Content **as string** for the 'data' item of the secret (can be templated)", + "description": "Content **as string** for the 'data' item of the configmap (can be templated)", "type": "string" } } diff --git a/chart/values.yaml b/chart/values.yaml index f795e0f02a436..9df5572388baf 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -268,7 +268,7 @@ enableBuiltInSecretEnvVars: # Extra secrets that will be managed by the chart # (You can use them with extraEnv or extraEnvFrom or some of the extraVolumes values). -# The format is "key/value" where +# The format for secret data is "key/value" where # * key (can be templated) is the name of the secret that will be created # * value: an object with the standard 'data' or 'stringData' key (or both). # The value associated with those keys must be a string (can be templated) @@ -277,6 +277,8 @@ extraSecrets: {} # extraSecrets: # '{{ .Release.Name }}-airflow-connections': # type: 'Opaque' +# labels: +# my.custom.label/v1: my_custom_label_value_1 # data: | # AIRFLOW_CONN_GCP: 'base64_encoded_gcp_conn_string' # AIRFLOW_CONN_AWS: 'base64_encoded_aws_conn_string' @@ -288,7 +290,7 @@ extraSecrets: {} # Extra ConfigMaps that will be managed by the chart # (You can use them with extraEnv or extraEnvFrom or some of the extraVolumes values). -# The format is "key/value" where +# The format for configmap data is "key/value" where # * key (can be templated) is the name of the configmap that will be created # * value: an object with the standard 'data' key. # The value associated with this keys must be a string (can be templated) @@ -296,6 +298,8 @@ extraConfigMaps: {} # eg: # extraConfigMaps: # '{{ .Release.Name }}-airflow-variables': +# labels: +# my.custom.label/v2: my_custom_label_value_2 # data: | # AIRFLOW_VAR_HELLO_MESSAGE: "Hi!" # AIRFLOW_VAR_KUBERNETES_NAMESPACE: "{{ .Release.Namespace }}" diff --git a/tests/charts/test_extra_configmaps_secrets.py b/tests/charts/test_extra_configmaps_secrets.py index c129b786ffbc5..7bf0b9fab5878 100644 --- a/tests/charts/test_extra_configmaps_secrets.py +++ b/tests/charts/test_extra_configmaps_secrets.py @@ -21,6 +21,7 @@ from unittest import mock import yaml +from parameterized import parameterized from tests.charts.helm_template_generator import prepare_k8s_lookup_dict, render_chart @@ -148,3 +149,38 @@ def test_extra_configmaps_secrets_labels(self): } for k8s_object in k8s_objects: assert k8s_object['metadata']['labels'] == expected_labels + + @parameterized.expand( + [ + ({}, {"label3": "value3", "label4": "value4"}), + ({"label1": "value1", "label2": "value2"}, {}), + ({"label1": "value1", "label2": "value2"}, {"label3": "value3", "label4": "value4"}), + ] + ) + def test_extra_configmaps_secrets_additional_labels(self, chart_labels, local_labels): + k8s_objects = render_chart( + name=RELEASE_NAME, + values={ + "labels": chart_labels, + "extraSecrets": { + "{{ .Release.Name }}-extra-secret-1": { + "labels": local_labels, + "stringData": "data: secretData", + } + }, + "extraConfigMaps": { + "{{ .Release.Name }}-extra-configmap-1": { + "labels": local_labels, + "data": "data: configData", + } + }, + }, + show_only=["templates/configmaps/extra-configmaps.yaml", "templates/secrets/extra-secrets.yaml"], + ) + common_labels = { + "release": RELEASE_NAME, + "heritage": "Helm", + "chart": mock.ANY, + } + for k8s_object in k8s_objects: + assert k8s_object['metadata']['labels'] == {**common_labels, **chart_labels, **local_labels} From f2db479f5acf1be42b24f601c71440ece648bdf2 Mon Sep 17 00:00:00 2001 From: aakcht Date: Thu, 4 Aug 2022 14:22:04 +0300 Subject: [PATCH 2/2] Chart: fix values.schema.json for extrasecrets/configmaps --- chart/values.schema.json | 6 ++---- chart/values_schema.schema.json | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/chart/values.schema.json b/chart/values.schema.json index 803aa923bb1c7..279a59d955ef9 100644 --- a/chart/values.schema.json +++ b/chart/values.schema.json @@ -860,9 +860,8 @@ "labels": { "description": "Labels for the secret", "type": "object", + "default": null, "additionalProperties": { - "description": "Label key", - "default": null, "type": "string" } }, @@ -899,9 +898,8 @@ "labels": { "description": "Labels for the configmap", "type": "object", + "default": null, "additionalProperties": { - "description": "Label key", - "default": null, "type": "string" } }, diff --git a/chart/values_schema.schema.json b/chart/values_schema.schema.json index f4b344d613d89..f74d8ab871326 100644 --- a/chart/values_schema.schema.json +++ b/chart/values_schema.schema.json @@ -5,8 +5,19 @@ "definitions": { "leafs": { "additionalProperties": { - "additionalProperties": { - "$ref": "#/definitions/leafs" + "if": { + "not": { + "properties": { + "description": { + "pattern": "^Labels for the configmap$|^Labels for the secret$" + } + } + } + }, + "then": { + "additionalProperties": { + "$ref": "#/definitions/leafs" + } } }, "if": {