Skip to content

Commit

Permalink
Merge pull request #154 from projectsyn/component-compile-postprocess
Browse files Browse the repository at this point in the history
Run postprocessing filters when compiling a single component
  • Loading branch information
simu authored Jul 31, 2020
2 parents ce43022 + 0dfbc67 commit 7fc1772
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

* Validation of component slug ([#153])
* `component compile` now applies postprocessing filters ([#154])

## [v0.2.2]

Expand Down Expand Up @@ -143,3 +144,4 @@ Initial implementation
[#148]: https://github.com/projectsyn/commodore/pull/148
[#151]: https://github.com/projectsyn/commodore/pull/151
[#153]: https://github.com/projectsyn/commodore/pull/153
[#154]: https://github.com/projectsyn/commodore/pull/154
16 changes: 15 additions & 1 deletion commodore/component/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

import click

from commodore.config import Config
from commodore.config import Config, Component
from commodore.dependency_mgmt import fetch_jsonnet_libs
from commodore.helpers import kapitan_compile, relsymlink
from commodore.postprocess import postprocess_components
from git import Repo
from kapitan.resources import inventory_reclass


libs = [{'name': 'kube-libsonnet',
Expand Down Expand Up @@ -94,6 +97,17 @@ def compile_component(config: Config, component_path, value_files, search_paths,
fake_refs=True,
reveal=True)
click.echo(f" > Component compiled to {output_path / 'compiled/test'}")

# prepare inventory and fake component object for postprocess
inventory = inventory_reclass(temp_dir / 'inventory')['nodes']['test']
component = Component(component_name, Repo(component_path),
'https://fake.repo.url/', 'master')
# We change the working directory to the output_path directory here,
# as postprocess expects to find `compiled/<target>` in the working
# directory.
os.chdir(output_path)
postprocess_components(config, inventory, 'test',
{component_name: component})
finally:
os.chdir(original_working_dir)
if config.trace:
Expand Down
58 changes: 51 additions & 7 deletions tests/test_component_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@
from test_component_new import test_run_component_new_command


def test_run_component_compile_command(tmp_path):
"""
Run the component compile command
"""

os.chdir(tmp_path)
def _prepare_component(tmp_path, component_name='test-component'):
test_run_component_new_command(tmp_path=tmp_path)

component_name = 'test-component'
with open(tmp_path / 'dependencies' / component_name / 'component/main.jsonnet', 'a') as file:
file.write('''
{
Expand All @@ -30,6 +24,33 @@ def test_run_component_compile_command(tmp_path):
}
''')


def _add_postprocessing_filter(tmp_path, component_name='test-component'):
with open(tmp_path / 'dependencies' / component_name / 'postprocess' /
'filters.yml', 'w') as file:
filters = {
'filters': [{
'path': component_name,
'type': 'builtin',
'filter': 'helm_namespace',
'filterargs': {
'namespace': 'test-component-ns',
}
}]
}
yaml.dump(filters, file)


def test_run_component_compile_command(tmp_path):
"""
Run the component compile command
"""

os.chdir(tmp_path)

component_name = 'test-component'
_prepare_component(tmp_path, component_name)

exit_status = os.system(f"commodore component compile -o ./testdir dependencies/{component_name}")
assert exit_status == 0
assert os.path.exists(tmp_path / f"testdir/compiled/test/apps/{component_name}.yaml")
Expand All @@ -41,6 +62,29 @@ def test_run_component_compile_command(tmp_path):
assert target['metadata']['namespace'] == f"syn-{component_name}"


def test_run_component_compile_command_postprocess(tmp_path):
"""
Run the component compile command for a component with a postprocessing
filter
"""

os.chdir(tmp_path)

component_name = 'test-component'
_prepare_component(tmp_path, component_name)
_add_postprocessing_filter(tmp_path, component_name)

exit_status = os.system(f"commodore component compile -o ./testdir dependencies/{component_name}")
assert exit_status == 0
assert os.path.exists(tmp_path / f"testdir/compiled/test/apps/{component_name}.yaml")
rendered_yaml = tmp_path / 'testdir/compiled/test' / component_name / 'test_service_account.yaml'
assert rendered_yaml.exists()
with open(rendered_yaml) as file:
target = yaml.safe_load(file)
assert target['kind'] == 'ServiceAccount'
assert target['metadata']['namespace'] == 'test-component-ns'


def test_no_component_compile_command():
with pytest.raises(ClickException) as excinfo:
compile_component(Config(), './', [], [], './')
Expand Down

0 comments on commit 7fc1772

Please sign in to comment.