From 8eea0bf040bf6c306323ccbf8d4adb2d3ef18959 Mon Sep 17 00:00:00 2001 From: Rahul Bawa Date: Sat, 7 Nov 2020 22:56:21 +0530 Subject: [PATCH 1/3] Add support for multiline json in tables - Since MD syntax does not really support it so I've created table using html table syntax and it seems to render perfectly. Pending items -------------- - rst and html templates. Fixes #17 #24 --- frigate/gen.py | 13 +++++++++--- frigate/templates/html.jinja2 | 2 +- frigate/templates/markdown.jinja2 | 35 +++++++++++++++++++++++++------ 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/frigate/gen.py b/frigate/gen.py index 8621645..a4ca1c0 100644 --- a/frigate/gen.py +++ b/frigate/gen.py @@ -3,7 +3,8 @@ import tempfile import shutil import subprocess - +import datetime +from json import JSONEncoder from jinja2 import Environment, FileSystemLoader from ruamel.yaml import YAML from ruamel.yaml.comments import CommentedMap @@ -184,6 +185,12 @@ def clean_comment(comment): return comment.strip("# ") +class DateTimeEncoder(JSONEncoder): + def default(self, obj): + if isinstance(obj, (datetime.date, datetime.datetime)): + return obj.isoformat() + + def traverse(tree, root=None): """Iterate over a tree of configuration and extract all information. @@ -230,10 +237,10 @@ def traverse(tree, root=None): if key in tree.ca.items: comment = get_comment(tree, key) param = ".".join(root + [key]) - yield [param, comment, json.dumps(default)] + yield [param, comment, json.dumps(default, indent=4, sort_keys=True, cls=DateTimeEncoder)] -def gen(chartdir, output_format, credits=True, deps=True): +def gen(chartdir, output_format, credits=True, deps=True, update=True): """Generate documentation for a Helm chart. Generate documentation for a Helm chart given the path to a chart and a diff --git a/frigate/templates/html.jinja2 b/frigate/templates/html.jinja2 index 2a20588..15c11e1 100644 --- a/frigate/templates/html.jinja2 +++ b/frigate/templates/html.jinja2 @@ -44,7 +44,7 @@ {{ param }} {{ comment }} - {{ default }} +
{{ default }}
{% endfor -%} diff --git a/frigate/templates/markdown.jinja2 b/frigate/templates/markdown.jinja2 index ec4f35c..a5cee24 100644 --- a/frigate/templates/markdown.jinja2 +++ b/frigate/templates/markdown.jinja2 @@ -15,14 +15,37 @@ ## Configuration The following table lists the configurable parameters of the {{ name | capitalize }} chart and their default values. - -| Parameter | Description | Default | -| ------------------------ | ----------------------- | -------------- | + + + + + + + + + {% for (param, comment, default) in values -%} -| `{{ param }}` | {{ comment }} | `{{ default }}` | + + + + + {% endfor -%} + +
ParameterDescriptionDefault
{{ param }}{{ comment }} +{% if "\n" in default %} + +```json + +{{ default }} + +``` + +{% else %} +{{ default }} +{% endif %} +
{%- endblock %} - {% block footnotes -%} {{ footnotes }} {% endblock -%} @@ -32,4 +55,4 @@ The following table lists the configurable parameters of the {{ name | capitaliz --- _Documentation generated by [Frigate](https://frigate.readthedocs.io)._ {%- endif -%} -{%- endblock %} \ No newline at end of file +{%- endblock %} From 731f6144d562defc434d94170c426a83ecb1540e Mon Sep 17 00:00:00 2001 From: Rahul Bawa Date: Tue, 10 Nov 2020 13:55:32 +0530 Subject: [PATCH 2/3] Support for pre-packaged charts in frigate If a user packs the chart properly (including its dependent charts) so he would only like frigate to just go through them and just dump the values from them that way it already does. The only thing would be to NOT run the repo update and the dep update commands. One thing to note here is that the above commands only slow down the whole recursion drastically (I had to wait for 20 mins). With this improvement a flag called --no-update has been introduced which would not invoke the the aforementioned commands during the traversal. Fixes #26 --- frigate/cli.py | 7 +++++-- frigate/gen.py | 41 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/frigate/cli.py b/frigate/cli.py index 274d814..c1aac59 100644 --- a/frigate/cli.py +++ b/frigate/cli.py @@ -24,7 +24,10 @@ def cli(): @click.option( "--no-deps", is_flag=True, default=True, help="Do not render dependency values", ) -def gen(filename, output_format, no_credits, no_deps): +@click.option( + "--no-update", is_flag=True, default=True, help="Do not update the charts", +) +def gen(filename, output_format, no_credits, no_deps, no_update): click.echo( - frigate.gen.gen(filename, output_format, credits=no_credits, deps=no_deps) + frigate.gen.gen(filename, output_format, credits=no_credits, deps=no_deps, update=no_update) ) diff --git a/frigate/gen.py b/frigate/gen.py index a4ca1c0..4221b0a 100644 --- a/frigate/gen.py +++ b/frigate/gen.py @@ -37,6 +37,36 @@ def load_chart(chartdir, root=None): return chart, list(traverse(values, root=root)) +def load_prepacked_chart_with_dependencies(chartdir, root=None): + root = [] if root is None else root + chart, values = load_chart(chartdir, root=root) + if "dependencies" in chart: + for dependency in chart["dependencies"]: + dependency_name = dependency["name"] + + with tempfile.TemporaryDirectory() as tmpdirname: + tar_file_path = os.path.join( + chartdir, "charts", f"{dependency_name}-{dependency['version']}.tgz", + ) + if os.path.isfile(tar_file_path): + dependency_path = tar_file_path + shutil.unpack_archive(dependency_path, tmpdirname) + dependency_dir = os.path.join(tmpdirname, dependency_name) + else: + dependency_path = os.path.join( + chartdir, "charts", f"{dependency_name}", + ) + dependency_dir = os.path.join(chartdir, dependency_path) + # chart namespace eg nginx.controller.foo + namespace = root + [dependency_name] + + _, dependency_values = load_prepacked_chart_with_dependencies( + dependency_dir, namespace + ) + values = squash_duplicate_values(values + dependency_values) + return chart, values + + def load_chart_with_dependencies(chartdir, root=None): """Load the yaml information from a Helm chart directory and its dependencies. @@ -251,14 +281,19 @@ def gen(chartdir, output_format, credits=True, deps=True, update=True): output_format (str): Output format (maps to jinja templates in frigate) credits (bool): Show Frigate credits in documentation deps (bool): Read values from chart dependencies and include them in the config table + update (bool): If false, frigate won't update the charts at runtime Returns: str: Rendered documentation for the Helm chart """ - chart, values = ( - load_chart_with_dependencies(chartdir) if deps else load_chart(chartdir) - ) + if deps: + if update: + chart, values = load_chart_with_dependencies(chartdir) + else: + chart, values = load_prepacked_chart_with_dependencies(chartdir) + else: + chart, values = load_chart(chartdir) templates = Environment(loader=FileSystemLoader([chartdir, TEMPLATES_PATH])) if os.path.isfile(os.path.join(chartdir, DOTFILE_NAME)): From 48f337c5aa114f2bfa659503047e2709c3298e20 Mon Sep 17 00:00:00 2001 From: Rahul Bawa Date: Sun, 15 Nov 2020 18:51:38 +0530 Subject: [PATCH 3/3] Add new test - Added test for pre-packaged chart - Corrected the assertion for a test --- frigate/tests/test_frigate.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/frigate/tests/test_frigate.py b/frigate/tests/test_frigate.py index 7d1c572..9b3f641 100644 --- a/frigate/tests/test_frigate.py +++ b/frigate/tests/test_frigate.py @@ -150,9 +150,8 @@ def test_deps(deps_chart_path): docs = gen(deps_chart_path, "markdown") - assert "simple.image.repository" in docs - [tag_line] = [line for line in docs.splitlines() if "simple.image.tag" in line] - assert "mainline" in tag_line + assert "simple.image.repository" in docs + assert "\n\n\"mainline\"\n\n" in docs def test_squash_duplicates(): @@ -162,3 +161,11 @@ def test_squash_duplicates(): assert len(values) == 1 assert values[0][2] == "world" + + +def test_load_pre_packaged_chart(deps_chart_path): + from frigate.gen import gen + + docs = gen(deps_chart_path, "markdown", update=False) + assert "image.repository" in docs + assert "\n\n\"nginx\"\n\n" in docs