diff --git a/src/integrationtest/python/issue_47/.kubernator.py b/src/integrationtest/python/issue_47/.kubernator.py new file mode 100644 index 0000000..36db9d7 --- /dev/null +++ b/src/integrationtest/python/issue_47/.kubernator.py @@ -0,0 +1,7 @@ +# flake8: noqa +import os + +ktor.app.register_plugin("minikube", k8s_version=os.environ["K8S_VERSION"], + start_fresh=True, keep_running=False, profile="issue-47") +ktor.app.register_plugin("k8s") +ktor.app.register_plugin("templates") diff --git a/src/integrationtest/python/issue_47/.test.tmpl.yaml b/src/integrationtest/python/issue_47/.test.tmpl.yaml new file mode 100644 index 0000000..f91ceeb --- /dev/null +++ b/src/integrationtest/python/issue_47/.test.tmpl.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: {${ values.name }$} \ No newline at end of file diff --git a/src/integrationtest/python/issue_47/apply/apply.tmpl.yaml b/src/integrationtest/python/issue_47/apply/apply.tmpl.yaml new file mode 100644 index 0000000..4741708 --- /dev/null +++ b/src/integrationtest/python/issue_47/apply/apply.tmpl.yaml @@ -0,0 +1,4 @@ +apply: + - name: test + values: + name: ns1 diff --git a/src/integrationtest/python/issue_47/define.tmpl.yaml b/src/integrationtest/python/issue_47/define.tmpl.yaml new file mode 100644 index 0000000..f893e63 --- /dev/null +++ b/src/integrationtest/python/issue_47/define.tmpl.yaml @@ -0,0 +1,3 @@ +define: +- name: test + path: .test.tmpl.yaml diff --git a/src/integrationtest/python/issue_47_tests.py b/src/integrationtest/python/issue_47_tests.py new file mode 100644 index 0000000..6345f21 --- /dev/null +++ b/src/integrationtest/python/issue_47_tests.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2020 Express Systems USA, Inc +# Copyright 2024 Karellen, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from test_support import IntegrationTestSupport, unittest + +unittest # noqa +# Above import must be first + +from pathlib import Path # noqa: E402 +import os # noqa: E402 +import tempfile # noqa: E402 +import yaml # noqa: E402 +from pprint import pprint # noqa: E402 + + +class Issue47Test(IntegrationTestSupport): + def test_issue_47(self): + test_dir = Path(__file__).parent / "issue_47" + with tempfile.TemporaryDirectory() as results_dir: + results_file = Path(results_dir) / "results" + for k8s_version in (self.K8S_TEST_VERSIONS[-1],): + with self.subTest(k8s_version=k8s_version): + os.environ["K8S_VERSION"] = k8s_version + + self.run_module_test("kubernator", "-p", str(test_dir), + "-v", "TRACE", "-f", str(results_file), "dump") + + with open(results_file, "rb") as f: + results = list(yaml.safe_load_all(f)) + pprint(results) + + +if __name__ == "__main__": + unittest.main() diff --git a/src/main/python/kubernator/plugins/template.py b/src/main/python/kubernator/plugins/template.py index e0461e0..b817419 100644 --- a/src/main/python/kubernator/plugins/template.py +++ b/src/main/python/kubernator/plugins/template.py @@ -159,12 +159,12 @@ def render_template(self, name, source, values=()) -> str: raise ValueError(f"Template with a name {name} does not exist") template = self.templates[name] - logger.debug("Rendering template %s from %s in %s", name, template.source, source) + templ_source = _get_source(name, template.source, source) + logger.debug("Rendering %s", templ_source) rendered_template = template.render(self.context, values) if self.template_engine.failures(): - raise ValueError(f"Unable to render template {name} from {template.source} in {source} " - "due to undefined required variables") + raise ValueError(f"Unable to render template {templ_source} due to undefined required variables") return rendered_template @@ -174,8 +174,9 @@ def apply_template(self, name, values=(), source=None): rendered_template = self.render_template(name, source, values) template = self.templates[name] - logger.info("Applying template %s from %s", name, template.source) - self.context.k8s.add_resources(rendered_template, source=template.source) + templ_source = _get_source(name, template.source, source) + logger.info("Applying %s", templ_source) + self.context.k8s.add_resources(rendered_template, source=templ_source) def _process_template_doc(self, template_doc, source): logger.info("Processing Kubernator template from %s", source) @@ -209,3 +210,9 @@ def _add_parsed_template(self, source, *, name, path, defaults): def __repr__(self): return "Template Plugin" + + +def _get_source(name, template_def_source, template_appl_source): + return "template %r from %s%s" % (name, template_def_source, + "" if template_def_source == template_appl_source else + " in %s" % template_appl_source)