Example #1
0
def test_find_templates():
    templates = find(os.path.join(HERE, "fixtures/templates"))
    assert len(templates) == 2
    assert all([t.endswith(".py") for t, v in templates.items()])

    with pytest.raises(ValueError):
        find(os.path.join(HERE, "fixtures/values"))
Example #2
0
def test_render():
    templates = find(os.path.join(HERE, "fixtures/templates"))

    with pytest.raises(KeyError):
        render({}, templates)

    rendering = render(
        {"name": "test", "internalPort": 80, "externalPort": 80}, templates
    )

    assert len(rendering) == len(templates)
Example #3
0
def cli():
    """kuku: Kubernetes templating tool.

    Usage:
      kuku apply [-v] [-f <FILE>]... [-s <key=value>]... <TEMPLATES_DIR>
      kuku delete [-v] [-f <FILE>]... [-s <key=value>]... <TEMPLATES_DIR>
      kuku render [-v] [-f <FILE>]... [-s <key=value>]... <TEMPLATES_DIR>
      kuku (-h | --help)
      kuku --version

    Options:
      -h --help                     Show this screen.
      -v --verbose                  Dump debug info to stderr.
      --version                     Show version.
      -s KEY=VALUE --set KEY=VALUE  Set values on the command line (accepts multiple options or separate values with commas: Ex: -s key1=val1,key2=val2).
      -f FILE --file FILE           Specify values in a YAML file (accepts multiple options).

    Notes:
      Resolution of values: --set overrides values in --file by merging. The last value wins.
    """

    # Parse cli arguments from docstring
    arguments = docopt(str(cli.__doc__), version=__version__)

    # Load k8s config -- needed to the correct API versions of k8s
    config.load_kube_config()

    # Find all templates
    try:
        templates = find(arguments["<TEMPLATES_DIR>"])
    except ValueError as e:
        print(e)
        exit(1)
        raise

    try:
        # Resolve values
        context = resolve(arguments["--set"], arguments["--file"])
    except ValueError as e:
        print(e)
        exit(1)
        raise

    # Render templates with resolved context
    rendering = render(context, templates)

    if not context.is_complete:
        print(f"Missing values: {context.missing_keys}")
        exit(1)

    output = dump(rendering)
    if arguments["render"]:
        # print yaml
        print(output)

    if arguments["apply"]:
        print("Not implemented yet. Use kuku render ... | kubectl apply -f-")
        exit(1)

    if arguments["delete"]:
        print("Not implemented yet. Use kuku render ... | kubectl delete -f-")
        exit(1)

    if arguments["--verbose"]:
        print(output, file=sys.stderr)