Skip to content

Commit

Permalink
feat/strip-j2-jinja: implement rstrip for jinja2 input (#916)
Browse files Browse the repository at this point in the history
* feat/strip-j2-jinja: implement rstrip for jinja2 input

Added options "strip_postfix" and "stripped_postfix" to jinja2 input.
This allows to remove output files extensions which is often the case
for jinja2 template files (.j2).

Stripping is disabled by default and default value to strip is ".j2".

* feat/strip-j2-jinja: rename strip dropping parameters

Rename parameters for suffix removal in jinja2 compile step.

Rename:
strip_postfix -> suffix_remove
stripped_postfix -> suffix_stripped

* feat/strip-j2-jinja: apply formatter
  • Loading branch information
jkrzemin authored Jan 13, 2023
1 parent 161d59c commit 3cda228
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 3 deletions.
7 changes: 5 additions & 2 deletions kapitan/inputs/jinja2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@


class Jinja2(InputType):
def __init__(self, compile_path, search_paths, ref_controller):
def __init__(self, compile_path, search_paths, ref_controller, args):
super().__init__("jinja2", compile_path, search_paths, ref_controller)
self.input_params = {}
self.strip_postfix = args.get("suffix_remove", False)
self.stripped_postfix = args.get("suffix_stripped", ".j2")

def set_input_params(self, input_params):
self.input_params = input_params
Expand Down Expand Up @@ -46,8 +48,9 @@ def compile_file(self, file_path, compile_path, ext_vars, **kwargs):
for item_key, item_value in render_jinja2(
file_path, context, jinja2_filters=jinja2_filters, search_paths=self.search_paths
).items():
if self.strip_postfix and item_key.endswith(self.stripped_postfix):
item_key = item_key.rstrip(self.stripped_postfix)
full_item_path = os.path.join(compile_path, item_key)

with CompiledFile(
full_item_path, self.ref_controller, mode="w", reveal=reveal, target_name=target_name
) as fp:
Expand Down
4 changes: 3 additions & 1 deletion kapitan/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ def compile_target(target_obj, search_paths, compile_path, ref_controller, globa
output_path = comp_obj["output_path"]

if input_type == "jinja2":
input_compiler = Jinja2(compile_path, search_paths, ref_controller)
input_compiler = Jinja2(compile_path, search_paths, ref_controller, comp_obj)
if "input_params" in comp_obj:
input_compiler.set_input_params(comp_obj["input_params"])
elif input_type == "jsonnet":
Expand Down Expand Up @@ -601,6 +601,8 @@ def valid_target_obj(target_obj, require_compile=True):
"input_params": {"type": "object"},
"env_vars": {"type": "object"},
"args": {"type": "array"},
"suffix_remove": {"type": "boolean"},
"suffix_stripped": {"type": "string"},
},
"required": ["input_type", "input_paths", "output_path"],
"minItems": 1,
Expand Down
22 changes: 22 additions & 0 deletions tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ def tearDown(self):
reset_cache()


class CompileTestResourcesTestJinja2PostfixStrip(unittest.TestCase):
def setUp(self):
os.chdir(os.getcwd() + "/tests/test_resources/")

def test_compile(self):
sys.argv = ["kapitan", "compile", "-t", "jinja2-postfix-strip"]
main()

def test_compile_postfix_strip_disabled(self):
self.assertListEqual(os.listdir("compiled/jinja2-postfix-strip/unstripped"), ["stub.txt.j2"])

def test_compile_postfix_strip_overridden(self):
self.assertListEqual(os.listdir("compiled/jinja2-postfix-strip/stripped-overridden"), ["stub"])

def test_compile_postfix_strip_enabled(self):
self.assertListEqual(os.listdir("compiled/jinja2-postfix-strip/stripped"), ["stub.txt"])

def tearDown(self):
os.chdir(os.getcwd() + "/../../")
reset_cache()


class CompileKubernetesTest(unittest.TestCase):
def setUp(self):
os.chdir(os.getcwd() + "/examples/kubernetes/")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Is this a Jinja2 template?
Yes!
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Is this a Jinja2 template?
Yes!
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Is this a Jinja2 template?
Yes!
29 changes: 29 additions & 0 deletions tests/test_resources/inventory/targets/jinja2-postfix-strip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
parameters:
kapitan:
vars:
target: jinja2-postfix-strip
compile:
- output_path: stripped/
input_type: jinja2
input_paths:
- templates/stub.txt.j2
input_params:
name: test1
namespace: ns1
suffix_remove: true
- output_path: stripped-overridden/
input_type: jinja2
input_paths:
- templates/stub.txt.j2
input_params:
name: test2
namespace: ns2
suffix_remove: true
suffix_stripped: .txt.j2
- output_path: unstripped/
input_type: jinja2
input_paths:
- templates/stub.txt.j2
input_params:
name: test2
namespace: ns2
4 changes: 4 additions & 0 deletions tests/test_resources/templates/stub.txt.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Is this a Jinja2 template?
{% if True %}
Yes!
{% endif %}

0 comments on commit 3cda228

Please sign in to comment.