Skip to content

Commit

Permalink
Fix #90: Initial work for kapitan init
Browse files Browse the repository at this point in the history
  • Loading branch information
uberspot committed Feb 20, 2019
1 parent a446a8b commit ae0b444
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 1 deletion.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
recursive-include kapitan/reclass/reclass/ *.py
include kapitan/lib/*.libjsonnet
include kapitan/inputs/templates/*
include examples/kubernetes/lib/*.libjsonnet
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ Targets can also be defined inside the `inventory`.

# Usage

Use `kapitan init --init-directory <directory>` to populate a new
directory with the recommended kapitan folder structure in new projects.

For other available options use:

```
$ kapitan -h
usage: kapitan [-h] [--version]
Expand All @@ -272,14 +277,16 @@ Generic templated configuration management for Kubernetes, Terraform and other
things
positional arguments:
{eval,compile,inventory,searchvar,secrets,lint}
{eval,compile,inventory,searchvar,secrets,lint,init}
commands
eval evaluate jsonnet file
compile compile targets
inventory show inventory
searchvar show all inventory files where var is declared
secrets manage secrets
lint linter for inventory and secrets
init initialize a directory with the recommended kapitan
project skeleton.
optional arguments:
-h, --help show this help message and exit
Expand Down
9 changes: 9 additions & 0 deletions kapitan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from kapitan.resources import search_imports, resource_callbacks, inventory_reclass
from kapitan.version import PROJECT_NAME, DESCRIPTION, VERSION
from kapitan.lint import start_lint
from kapitan.initialiser import initialise_skeleton

from kapitan.refs.base import RefController, Revealer
from kapitan.refs.secrets.gpg import GPGSecret
Expand Down Expand Up @@ -210,6 +211,11 @@ def main():
default=from_dot_kapitan('lint', 'inventory-path', './inventory'),
help='set inventory path, default is "./inventory"')

init_parser = subparser.add_parser('init', help='initialize a directory with the recommended kapitan project skeleton.')
init_parser.add_argument('--directory',
default=from_dot_kapitan('init', 'directory', '.'),
help='set path, in which to generate the project skeleton, assumes directory already exists. default is "./"')

args = parser.parse_args()

logger.debug('Running with args: %s', args)
Expand Down Expand Up @@ -287,6 +293,9 @@ def _search_imports(cwd, imp):
elif cmd == 'lint':
start_lint(args.fail_on_warning, args.skip_class_checks, args.inventory_path, args.search_secrets, args.secrets_path, args.compiled_path)

elif cmd == 'init':
initialise_skeleton(args.directory)

elif cmd == 'secrets':
ref_controller = RefController(args.secrets_path)

Expand Down
51 changes: 51 additions & 0 deletions kapitan/initialiser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
#
# Copyright 2019 The Kapitan Authors
#
# 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.

"initialiser module"

import logging
import os
import sys
import shutil
from distutils.dir_util import copy_tree

logger = logging.getLogger(__name__)

def initialise_skeleton(directory):
""" Initialises a directory with a recommended skeleton structure
Args:
directory (string): path which to initialise, directory is assumed to exist
"""

templates_directory = os.path.join(os.path.dirname(
__file__), 'inputs', 'templates')

copy_tree(templates_directory, directory)

# create lib/kube.libjsonnet
kube_jsonnet_lib_path = os.path.join(os.path.dirname(
__file__), '..', 'examples', 'kubernetes', 'lib', 'kube.libjsonnet')
shutil.copy2(kube_jsonnet_lib_path, os.path.join(directory, 'lib'))

logger.info("Populated {} with:".format(directory))
for dirName, subdirList, fileList in os.walk(directory):
logger.info('{}'.format(dirName))
for fname in fileList:
logger.info('\t {}'.format(fname))
# Remove the first entry in the list of sub-directories
# if there are any sub-directories present
if len(subdirList) > 0:
del subdirList[0]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local kube = import "lib/kube.libjsonnet";
local kap = import "lib/kapitan.libjsonnet";
local inventory = kap.inventory();

{
example: inventory.parameters.your_component.some_parameter
}
4 changes: 4 additions & 0 deletions kapitan/inputs/templates/inventory/classes/common.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
kapitan:
vars:
target: ${target_name}
29 changes: 29 additions & 0 deletions kapitan/inputs/templates/inventory/classes/my_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
parameters:
your_component:
some_parameter: true

kapitan:

compile:
- output_path: jsonnet_output
input_type: jsonnet
output_type: yaml
input_paths:
- components/my_component/my_component.jsonnet

- output_path: scripts
input_type: jinja2
input_paths:
- templates/scripts

- output_path: .
output_type: yaml
input_type: jinja2
input_paths:
- templates/docs

# - output_path: manifests
# input_type: kadet
# output_type: yaml
# input_paths:
# - components/nginx/
6 changes: 6 additions & 0 deletions kapitan/inputs/templates/inventory/targets/my_target.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
classes:
- common
- my_component

parameters:
target_name: my_target
14 changes: 14 additions & 0 deletions kapitan/inputs/templates/templates/docs/my_readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# My Readme

Target: {{ target }}

# INVENTORY

```
{{inventory.classes | yaml }}
```


```
{{inventory.parameters | yaml }}
```
8 changes: 8 additions & 0 deletions kapitan/inputs/templates/templates/scripts/my_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash -eu

{% set i = inventory.parameters %}
DIR=$(dirname ${BASH_SOURCE[0]})

echo "Running for target {{ i.target_name }}"
echo ${DIR}

49 changes: 49 additions & 0 deletions tests/test_initialiser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3
#
# Copyright 2019 The Kapitan Authors
#
# 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.


"init tests"

import logging
import unittest
import tempfile
import os

from kapitan.initialiser import initialise_skeleton

logging.basicConfig(level=logging.CRITICAL, format="%(message)s")
logger = logging.getLogger(__name__)


class InitTest(unittest.TestCase):
def test_init(self):
with tempfile.TemporaryDirectory() as tmp_dir:
initialise_skeleton(directory=tmp_dir)

template_dir = os.path.join(
os.getcwd(), 'kapitan', 'inputs', 'templates')

diff_files = []

for root, dirs, files in os.walk(tmp_dir):
diff_files += files

for root, dirs, files in os.walk(template_dir):
for f in files:
if f in diff_files:
diff_files.remove(f)

self.assertEqual(len(diff_files), 1)

0 comments on commit ae0b444

Please sign in to comment.