Skip to content

Commit

Permalink
Merge pull request #50 from karellen/issue_47
Browse files Browse the repository at this point in the history
Fix template sources
  • Loading branch information
arcivanov authored Feb 7, 2024
2 parents 5c5a30f + bb0abae commit 2c242de
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/integrationtest/python/issue_47/.kubernator.py
Original file line number Diff line number Diff line change
@@ -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")
4 changes: 4 additions & 0 deletions src/integrationtest/python/issue_47/.test.tmpl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: {${ values.name }$}
4 changes: 4 additions & 0 deletions src/integrationtest/python/issue_47/apply/apply.tmpl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apply:
- name: test
values:
name: ns1
3 changes: 3 additions & 0 deletions src/integrationtest/python/issue_47/define.tmpl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
define:
- name: test
path: .test.tmpl.yaml
48 changes: 48 additions & 0 deletions src/integrationtest/python/issue_47_tests.py
Original file line number Diff line number Diff line change
@@ -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()
17 changes: 12 additions & 5 deletions src/main/python/kubernator/plugins/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

0 comments on commit 2c242de

Please sign in to comment.