Skip to content

Commit

Permalink
fix jsonnet
Browse files Browse the repository at this point in the history
  • Loading branch information
ademariag committed Sep 8, 2024
1 parent 438493d commit cb35451
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 41 deletions.
3 changes: 2 additions & 1 deletion kapitan/inputs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def compile_input_path(self, input_path, comp_obj, ext_vars, **kwargs):
output_path = comp_obj.output_path
output_type = comp_obj.output_type
prune_output = comp_obj.prune
ext_vars_dict = ext_vars.model_dump(by_alias=True)

logger.debug("Compiling %s", input_path)
try:
Expand All @@ -76,7 +77,7 @@ def compile_input_path(self, input_path, comp_obj, ext_vars, **kwargs):
self.compile_file(
input_path,
_compile_path,
ext_vars,
ext_vars_dict,
output=output_type,
target_name=target_name,
prune_output=prune_output,
Expand Down
12 changes: 5 additions & 7 deletions kapitan/inputs/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from kapitan.helm_cli import helm_cli
from kapitan.inputs.base import CompiledFile, InputType
from kapitan.inputs.kadet import BaseModel, BaseObj, Dict
from kapitan.inventory.model import KapitanCompileHelmConfig

logger = logging.getLogger(__name__)

Expand All @@ -30,7 +31,7 @@


class Helm(InputType):
def __init__(self, compile_path, search_paths, ref_controller, args):
def __init__(self, compile_path, search_paths, ref_controller, args: KapitanCompileHelmConfig):
super().__init__("helm", compile_path, search_paths, ref_controller)

self.helm_values_files = args.helm_values_files
Expand All @@ -39,13 +40,10 @@ def __init__(self, compile_path, search_paths, ref_controller, args):
self.file_path = None

self.helm_values_file = None
if "helm_values" in args:
self.helm_values_file = write_helm_values_file(args["helm_values"])
if args.helm_values:
self.helm_values_file = write_helm_values_file(args.helm_values)

self.kube_version = None
if "kube_version" in args:
logger.warning("passing kube_version is deprecated. Use api_versions helm flag instead.")
self.kube_version = args["kube_version"]
self.kube_version = args.kube_version

def compile_file(self, file_path, compile_path, ext_vars, **kwargs):
"""
Expand Down
8 changes: 4 additions & 4 deletions kapitan/inputs/jinja2.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, compile_path, search_paths, ref_controller, args):
self.stripped_postfix = args.suffix_stripped
self.input_params = args.input_params

def compile_file(self, file_path, compile_path, ext_vars, **kwargs):
def compile_file(self, file_path, compile_path, ext_vars={}, **kwargs):
"""
Write items in path as jinja2 rendered files to compile_path.
path can be either a file or directory.
Expand All @@ -41,10 +41,10 @@ def compile_file(self, file_path, compile_path, ext_vars, **kwargs):
input_params.setdefault("compile_path", compile_path)

# set ext_vars and inventory for jinja2 context
context = ext_vars.model_dump()
context = ext_vars.copy()

context["inventory_global"] = cached.inv.inventory
context["inventory"] = cached.inv.inventory[target_name]
context["inventory_global"] = cached.global_inv
context["inventory"] = cached.global_inv[target_name]
context["input_params"] = input_params

jinja2_filters = kwargs.get("jinja2_filters")
Expand Down
2 changes: 1 addition & 1 deletion kapitan/inputs/jsonnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import os
import sys

from kapitan import cached
from kapitan.errors import CompileError
from kapitan.inputs.base import CompiledFile, InputType
from kapitan.resources import resource_callbacks, search_imports
Expand Down Expand Up @@ -81,6 +80,7 @@ def _search_imports(cwd, imp):
return search_imports(cwd, imp, self.search_paths)

json_output = None

if self.use_go:
json_output = go_jsonnet_file(
file_path,
Expand Down
2 changes: 1 addition & 1 deletion kapitan/inventory/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class InventoryTarget(BaseModel):
parameters: KapitanInventoryParameters = KapitanInventoryParameters()
classes: list = list()
applications: list = list()
exports: Dict = {}
exports: dict = {}


class Inventory(ABC):
Expand Down
3 changes: 3 additions & 0 deletions kapitan/inventory/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@ class KapitanCompileHelmConfig(KapitanCompileBaseConfig):
helm_values_files: Optional[List[str]] = []
helm_path: Optional[str] = None
input_paths: List[str]
kube_version: Optional[str] = None


class KapitanCompileJsonnetConfig(KapitanCompileBaseConfig):
input_type: Literal[InputTypes.JSONNET] = InputTypes.JSONNET
output_type: OutputType = OutputType.JSON
input_paths: List[str]
input_value: Optional[dict] = None
prune: bool = False


class KapitanCompileKadetConfig(KapitanCompileBaseConfig):
Expand Down Expand Up @@ -156,6 +158,7 @@ class KapitanDependencyHelmConfig(KapitanDependencyBaseConfig):
chart_name: str
version: Optional[str] = None
helm_path: Optional[str] = None
force_fetch: Optional[bool] = False


class KapitanDependencyGitConfig(KapitanDependencyBaseConfig):
Expand Down
4 changes: 2 additions & 2 deletions kapitan/refs/secrets/gpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ def from_params(cls, data, ref_params):

target_inv = cached.inv.get_parameters(target_name)

if "secrets" not in target_inv["kapitan"]:
if not target_inv.kapitan.secrets:
raise KapitanError(
f"parameters.kapitan.secrets not defined in inventory of target {target_name}"
)

recipients = target_inv["kapitan"]["secrets"]["gpg"]["recipients"]
recipients = target_inv.kapitan.secrets.gpg.recipients

return cls(data, recipients, **ref_params.kwargs)
except KeyError:
Expand Down
2 changes: 1 addition & 1 deletion kapitan/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def inventory(search_paths: list = [], target_name: str = None, inventory_path:

if target_name:
target = inv.get_target(target_name)
return target.model_dump()
return target.model_dump(by_alias=True)

return inv.inventory

Expand Down
23 changes: 9 additions & 14 deletions kapitan/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,11 @@ def compile_targets(
fetch_objs = []
# iterate through targets
for target in target_objs:
try:
# get value of "force_fetch" property
dependencies = target.dependencies
# dependencies is still a list
for entry in dependencies:
force_fetch = entry["force_fetch"]
if force_fetch:
fetch_objs.append(target)
except KeyError:
# targets may have no "dependencies" or "force_fetch" key
continue
for entry in target.dependencies:
force_fetch = entry.force_fetch
if entry.force_fetch:
fetch_objs.append(target)

# fetch dependencies from targets with force_fetch set to true
if fetch_objs:
fetch_dependencies(output_path, fetch_objs, dep_cache_dir, True, pool)
Expand Down Expand Up @@ -186,10 +180,11 @@ def compile_targets(
logger.exception(e)
else:
logger.error(e)
sys.exit(1)
raise CompileError(f"Error compiling targets: {e}")

shutil.rmtree(temp_path)
logger.debug("Removed %s", temp_path)
finally:
shutil.rmtree(temp_path)
logger.debug("Removed %s", temp_path)


def load_target_inventory(inventory, requested_targets, ignore_class_not_found=False):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import contextlib
import glob
import io
import logging
import os
import shutil
import sys
Expand All @@ -24,6 +25,8 @@
from kapitan.inventory import InventoryBackends
from kapitan.utils import directory_hash

logger = logging.getLogger(__name__)

TEST_PWD = os.getcwd()
TEST_RESOURCES_PATH = os.path.join(os.getcwd(), "tests/test_resources")
TEST_DOCKER_PATH = os.path.join(os.getcwd(), "examples/docker/")
Expand Down Expand Up @@ -209,6 +212,7 @@ def test_compile_jsonnet_env(self):
self.assertTrue(os.path.exists("compiled/jsonnet-env/jsonnet-env/env.yml"))
with open("compiled/jsonnet-env/jsonnet-env/env.yml", "r", encoding="utf-8") as f:
env = dict(yaml.safe_load(f))
logger.error(env)
self.assertEqual(set(env.keys()), {"applications", "parameters", "classes", "exports"})
self.assertEqual(env["applications"], ["a", "b", "c"])
self.assertEqual(env["classes"], ["common", "jsonnet-env"])
Expand Down
19 changes: 13 additions & 6 deletions tests/test_helm_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from kapitan.cli import main
from kapitan.inputs.helm import Helm, HelmChart
from kapitan.inputs.kadet import BaseObj
from kapitan.inventory.model import KapitanCompileHelmConfig


class HelmInputTest(unittest.TestCase):
Expand All @@ -26,10 +27,12 @@ def setUp(self):
def test_render_chart(self):
temp_dir = tempfile.mkdtemp()
chart_path = "charts/acs-engine-autoscaler"
helm = Helm(None, None, None, {})
_, error_message = helm.render_chart(
chart_path, temp_dir, None, {"name": "acs-engine-autoscaler"}, None, None
helm_params = {"name": "acs-engine-autoscaler"}
helm_config = KapitanCompileHelmConfig(
input_paths=[chart_path], helm_params=helm_params, output_path=temp_dir
)
helm = Helm(None, None, None, helm_config)
_, error_message = helm.render_chart(chart_path, temp_dir, None, helm_params, None, None)
self.assertFalse(error_message)
self.assertTrue(
os.path.isfile(os.path.join(temp_dir, "acs-engine-autoscaler", "templates", "secrets.yaml"))
Expand All @@ -38,11 +41,15 @@ def test_render_chart(self):
os.path.isfile(os.path.join(temp_dir, "acs-engine-autoscaler", "templates", "deployment.yaml"))
)

def test_error_invalid_char_dir(self):
def test_error_invalid_chart_dir(self):
chart_path = "./non-existent"
temp_dir = tempfile.mkdtemp()
helm = Helm(None, None, None, {})
_, error_message = helm.render_chart(chart_path, temp_dir, None, {"name": "mychart"}, None, None)
helm_params = {"name": "mychart"}
helm_config = KapitanCompileHelmConfig(
input_paths=[chart_path], output_path=temp_dir, helm_params=helm_params
)
helm = Helm(None, None, None, helm_config)
_, error_message = helm.render_chart(chart_path, temp_dir, None, helm_params, None, None)
self.assertTrue("path" in error_message and "not found" in error_message)

def test_compile_chart(self):
Expand Down
11 changes: 7 additions & 4 deletions tests/test_omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,20 @@ def test_load_and_resolve_single_target(self):

# Manually create a new Target
target = inventory.target_class(name=target_name, path="minikube-es.yml")

logger.error(f"Loading target {target_name} from {target.path}")
logger.error(target.parameters)
# Adds target to Inventory
inventory.targets.update({target_name: target})

# Loads the target using the inventory
inventory.load_target(target)

self.assertDictEqual(target_kapitan_metadata["_kapitan_"], target.parameters._kapitan_)
self.assertEqual(target.parameters._kapitan_.name.short, "minikube")
# Check if the target is loaded correctly
metadata = target.parameters.model_dump(by_alias=True)["_kapitan_"]
self.assertDictEqual(target_kapitan_metadata["_kapitan_"], metadata)
self.assertEqual(metadata["name"]["short"], "minikube")
self.assertEqual(target.parameters.target_name, "minikube-es")
self.assertEqual(target.parameters.kubectl.insecure_skip_tls_verify, False)
self.assertEqual(target.parameters.kubectl["insecure_skip_tls_verify"], False)

def tearDown(self) -> None:
shutil.rmtree(self.temp_dir)
Expand Down

0 comments on commit cb35451

Please sign in to comment.