Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for template plugins #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions example/example_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import getpass
import itertools


def fooify(value):
return "".join(t[1] for t in zip(value, itertools.cycle("foo")))


def is_root(user):
return user == "root"


def current_user():
return getpass.getuser()


PLUGIN_FILTERS = {
"fooify": fooify,
}

PLUGIN_TESTS = {
"root": is_root,
}

PLUGIN_GLOBALS = {
"current_user": current_user,
}
4 changes: 4 additions & 0 deletions example/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ metadata:
name: webapp-deployment
labels:
app: webapp
annotations:
deploy_user: !k/expr current_user()
deployded_as_root: !k/expr current_user() is root
user_foo: !k/expr current_user() | fooify
spec:
replicas: 1
selector:
Expand Down
3 changes: 3 additions & 0 deletions example/prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ context:
- vars/prod.yaml
- provider: default
path: vars/common.yaml

template_plugins:
- example_plugin
27 changes: 24 additions & 3 deletions src/konverter/app.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
from __future__ import annotations

import collections
import importlib
import pathlib
import sys
import typing

from ruamel.yaml import YAML

from .context import ContextProvider
from .yaml import KonverterYAML

if typing.TYPE_CHECKING:
import types

DEFAULT_PROVIDER = {
"default": {"key_path": ".konverter-vault",},
}


class Konverter:
def __init__(self, templates, context, work_dir):
def __init__(self, templates, template_plugins, context, work_dir):
self.templates = templates
self.template_plugins = template_plugins
self.context = context
self.work_dir = work_dir

def render(self, out_file):
yaml = KonverterYAML(self)
yaml = KonverterYAML(self, self.template_plugins)
# We want to have the documents from different files
# separated by "---".
yaml.explicit_start = True
Expand All @@ -44,14 +52,17 @@ def from_dict(cls, config, work_dir):
(pathlib.Path(p) for p in config["templates"]), work_dir
)
)
template_plugins = list(
cls._load_template_plugins(config.get("template_plugins", []), work_dir)
)
providers = dict(
cls._create_providers(
collections.ChainMap(config.get("providers", {}), DEFAULT_PROVIDER),
work_dir,
)
)
context = collections.ChainMap(*cls._load_context(config["context"], providers))
return cls(templates, context, work_dir)
return cls(templates, template_plugins, context, work_dir)

@staticmethod
def _collect_templates(
Expand Down Expand Up @@ -84,3 +95,13 @@ def _load_context(context, providers):
else:
provider, path = ctx["provider"], ctx["path"]
yield providers[provider].load_context(path)

@staticmethod
def _load_template_plugins(
plugin_names: typing.List[str], work_dir: pathlib.Path
) -> typing.Generator[types.ModuleType, None, None]:
path = str(work_dir)
if path not in sys.path:
sys.path.append(path)
for module_name in plugin_names:
yield importlib.import_module(module_name)
19 changes: 18 additions & 1 deletion src/konverter/template.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from __future__ import annotations

import base64
import json
import typing

import jinja2

if typing.TYPE_CHECKING:
import types


def to_json(value: typing.Any) -> str:
return json.dumps(value)
Expand All @@ -18,10 +23,22 @@ def b64encode(value: typing.Union[str, bytes]) -> str:


class JinjaEnvironment(jinja2.Environment):
def __init__(self, *args, **kwargs):
def __init__(
self,
*args,
plugins: typing.Optional[typing.List[types.ModuleType]] = None,
**kwargs,
):
super().__init__(*args, **kwargs)
self.filters["b64encode"] = b64encode
self.filters["to_json"] = to_json
self._enable_plugins(plugins or [])

def _enable_plugins(self, plugins: typing.List[types.ModuleType]):
for plugin in plugins:
self.filters.update(getattr(plugin, "PLUGIN_FILTERS", {}))
self.tests.update(getattr(plugin, "PLUGIN_TESTS", {}))
self.globals.update(getattr(plugin, "PLUGIN_GLOBALS", {}))

def eval_expression(
self, src: str, context: typing.Mapping[str, typing.Any]
Expand Down
11 changes: 9 additions & 2 deletions src/konverter/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from .template import JinjaEnvironment

if typing.TYPE_CHECKING:
import types

from ruamel.yaml.nodes import Node, ScalarNode
from ruamel.yaml.constructor import Constructor
from ruamel.yaml.representer import Representer
Expand Down Expand Up @@ -101,10 +103,15 @@ def to_yaml(cls, representer: Representer, instance: KonvertType, yaml) -> Node:


class KonverterYAML(BaseYAML):
def __init__(self, app: Konverter):
def __init__(
self,
app: Konverter,
plugins: typing.Optional[typing.List[types.ModuleType]] = None,
):
super().__init__()
# FIXME: only pass the context and not the whole app
self.app = app
self.env = JinjaEnvironment(undefined=jinja2.StrictUndefined)
self.env = JinjaEnvironment(plugins=plugins, undefined=jinja2.StrictUndefined)
self.register_type(KonvertExpression)
self.register_type(KonvertTemplate)

Expand Down
8 changes: 8 additions & 0 deletions tests/e2e/expect_plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
global: test
filter: TEST
test: true
template: |-
test
TEST
True
23 changes: 23 additions & 0 deletions tests/e2e/template_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def template_filter(value):
return value.upper()


def template_test(value):
return value == "test"


def template_global():
return "test"


PLUGIN_FILTERS = {
"template_filter": template_filter,
}

PLUGIN_TESTS = {
"template_test": template_test,
}

PLUGIN_GLOBALS = {
"template_global": template_global,
}
7 changes: 7 additions & 0 deletions tests/e2e/templates/plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
global: !k/expr template_global()
filter: !k/expr template_global() | template_filter
test: !k/expr template_global() is template_test
template: !k/template |-
{{ template_global() }}
{{ template_global() | template_filter }}
{{ template_global() is template_test }}
8 changes: 8 additions & 0 deletions tests/e2e/test_plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
templates:
- templates/plugin.yaml

context:
- vars/_generic.yaml

template_plugins:
- template_plugin