Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chart: custom labels for extrasecrets/configmaps #25283

Merged
merged 2 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions chart/templates/configmaps/extra-configmaps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions chart/templates/secrets/extra-secrets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 17 additions & 1 deletion chart/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,14 @@
"description": "Type **as string** of secret E.G. Opaque, kubernetes.io/dockerconfigjson, etc.",
"type": "string"
},
"labels": {
"description": "Labels for the secret",
"type": "object",
"default": null,
"additionalProperties": {
"type": "string"
}
},
"data": {
"description": "Content **as string** for the 'data' item of the secret (can be templated)",
"type": "string"
Expand Down Expand Up @@ -887,8 +895,16 @@
"minProperties": 1,
"additionalProperties": false,
"properties": {
"labels": {
"description": "Labels for the configmap",
"type": "object",
"default": null,
"additionalProperties": {
"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"
}
}
Expand Down
8 changes: 6 additions & 2 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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'
Expand All @@ -288,14 +290,16 @@ 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)
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 }}"
Expand Down
15 changes: 13 additions & 2 deletions chart/values_schema.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
36 changes: 36 additions & 0 deletions tests/charts/test_extra_configmaps_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}