From 71eac895e7c31ca0b75775b4bb840ad9d1e68409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Bregu=C5=82a?= Date: Sun, 23 May 2021 19:27:38 +0200 Subject: [PATCH 1/3] Fix elasticsearch secret created without elastticsearch enabled --- chart/templates/check-values.yaml | 11 +++++ .../secrets/elasticsearch-secret.yaml | 2 +- chart/tests/test_elasticsearch_secret.py | 49 ++++++++++++++++++- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/chart/templates/check-values.yaml b/chart/templates/check-values.yaml index 01a764a5c4cac..1307d3d33e332 100644 --- a/chart/templates/check-values.yaml +++ b/chart/templates/check-values.yaml @@ -49,3 +49,14 @@ The sole purpose of this yaml file is it to check the values file is consistent {{- end }} {{- end }} + + {{- if and .Values.elasticsearch.enabled }} + {{- if and .Values.elasticsearch.secretName .Values.elasticsearch.connection }} + {{ required "You must not set both values elasticsearch.secretName and elasticsearch.connection" nil }} + {{- end }} + + {{- if not (or .Values.elasticsearch.secretName .Values.elasticsearch.connection) }} + {{ required "You must set one of the values elasticsearch.secretName or elasticsearch.connection when using a Elasticsearch" nil }} + {{- end }} + + {{- end }} diff --git a/chart/templates/secrets/elasticsearch-secret.yaml b/chart/templates/secrets/elasticsearch-secret.yaml index b220ab5f8013f..0e35b8b5f1064 100644 --- a/chart/templates/secrets/elasticsearch-secret.yaml +++ b/chart/templates/secrets/elasticsearch-secret.yaml @@ -18,7 +18,7 @@ ################################ ## Elasticsearch Secret ################################# -{{- if (and .Values.elasticsearch.connection (not .Values.elasticsearch.secretName)) }} +{{- if (and .Values.elasticsearch.enabled (not .Values.elasticsearch.secretName)) }} kind: Secret apiVersion: v1 metadata: diff --git a/chart/tests/test_elasticsearch_secret.py b/chart/tests/test_elasticsearch_secret.py index 9d2c2b9daa92f..66b5245c859ad 100644 --- a/chart/tests/test_elasticsearch_secret.py +++ b/chart/tests/test_elasticsearch_secret.py @@ -17,6 +17,7 @@ import base64 import unittest +from subprocess import CalledProcessError import jmespath @@ -24,6 +25,51 @@ class ElasticsearchSecretTest(unittest.TestCase): + def test_should_not_generate_a_document_if_elasticsearch_disabled(self): + + docs = render_chart( + values={"elasticsearch": {"enabled": False}}, + show_only=["templates/secrets/elasticsearch-secret.yaml"], + ) + + assert 0 == len(docs) + + def test_should_raise_error_when_connection_not_provided(self): + with self.assertRaises(CalledProcessError) as ex_ctx: + render_chart( + values={ + "elasticsearch": { + "enabled": True, + } + }, + show_only=["templates/secrets/elasticsearch-secret.yaml"], + ) + assert ( + "You must set one of the values elasticsearch.secretName or elasticsearch.connection when using a Elasticsearch" + in ex_ctx.exception.stderr.decode() + ) + + def test_should_raise_error_when_conflicting_options(self): + with self.assertRaises(CalledProcessError) as ex_ctx: + render_chart( + values={ + "elasticsearch": { + "enabled": True, + "secretName": "my-test", + "connection": { + "user": "username!@#$%%^&*()", + "pass": "password!@#$%%^&*()", + "host": "elastichostname", + }, + }, + }, + show_only=["templates/secrets/elasticsearch-secret.yaml"], + ) + assert ( + "You must not set both values elasticsearch.secretName and elasticsearch.connection" + in ex_ctx.exception.stderr.decode() + ) + def _get_connection(self, values: dict) -> str: docs = render_chart( values=values, @@ -36,11 +82,12 @@ def test_should_correctly_handle_password_with_special_characters(self): connection = self._get_connection( { "elasticsearch": { + "enabled": True, "connection": { "user": "username!@#$%%^&*()", "pass": "password!@#$%%^&*()", "host": "elastichostname", - } + }, } } ) From 4f83049d589a09ecaa119128544da3bd89782798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Bregu=C5=82a?= Date: Sun, 23 May 2021 19:41:06 +0200 Subject: [PATCH 2/3] Update chart/templates/check-values.yaml Co-authored-by: Kaxil Naik --- chart/templates/check-values.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chart/templates/check-values.yaml b/chart/templates/check-values.yaml index 1307d3d33e332..81fb80901730c 100644 --- a/chart/templates/check-values.yaml +++ b/chart/templates/check-values.yaml @@ -50,7 +50,7 @@ The sole purpose of this yaml file is it to check the values file is consistent {{- end }} - {{- if and .Values.elasticsearch.enabled }} + {{- if .Values.elasticsearch.enabled }} {{- if and .Values.elasticsearch.secretName .Values.elasticsearch.connection }} {{ required "You must not set both values elasticsearch.secretName and elasticsearch.connection" nil }} {{- end }} From bed38b58a8d30162218c0cde5161bed4e004308f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Bregu=C5=82a?= Date: Sun, 23 May 2021 20:04:36 +0200 Subject: [PATCH 3/3] fixup! Update chart/templates/check-values.yaml --- chart/tests/test_elasticsearch_secret.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chart/tests/test_elasticsearch_secret.py b/chart/tests/test_elasticsearch_secret.py index 66b5245c859ad..69b4b4cf38237 100644 --- a/chart/tests/test_elasticsearch_secret.py +++ b/chart/tests/test_elasticsearch_secret.py @@ -45,8 +45,8 @@ def test_should_raise_error_when_connection_not_provided(self): show_only=["templates/secrets/elasticsearch-secret.yaml"], ) assert ( - "You must set one of the values elasticsearch.secretName or elasticsearch.connection when using a Elasticsearch" - in ex_ctx.exception.stderr.decode() + "You must set one of the values elasticsearch.secretName or elasticsearch.connection " + "when using a Elasticsearch" in ex_ctx.exception.stderr.decode() ) def test_should_raise_error_when_conflicting_options(self):