Skip to content

Commit

Permalink
Merge pull request #1142 from projectsyn/feat/reclass-applications
Browse files Browse the repository at this point in the history
Expose `applications` and `exports` fields in `InventoryTarget` and Jsonnet `inventory()` callback
  • Loading branch information
ademariag authored Mar 4, 2024
2 parents dcb5da7 + bdcf0cd commit b219af9
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 4 deletions.
44 changes: 44 additions & 0 deletions examples/kubernetes/compiled/jsonnet-env/jsonnet-env/env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
applications:
- a
- b
- c
classes:
- common
- jsonnet-env
exports: {}
parameters:
_reclass_:
environment: base
name:
full: jsonnet-env
parts:
- jsonnet-env
path: jsonnet-env
short: jsonnet-env
a: aaaaa
b: bbbbb
c: ccccc
kapitan:
compile:
- input_params: {}
input_paths:
- components/jsonnet-env/env.jsonnet
input_type: jsonnet
output_path: jsonnet-env
output_type: yml
secrets:
awskms:
key: alias/nameOfKey
gkms:
key: projects/<project>/locations/<location>/keyRings/<keyRing>/cryptoKeys/<key>
gpg:
recipients:
- fingerprint: D9234C61F58BEB3ED8552A57E28DC07A3CBFAE7C
name: [email protected]
target_full_path: jsonnet-env
vars:
managed_by: kapitan
namespace: jsonnet-env
target: jsonnet-env
namespace: jsonnet-env
target_name: jsonnet-env
11 changes: 11 additions & 0 deletions examples/kubernetes/components/jsonnet-env/env.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local kap = import 'lib/kapitan.libjsonnet';
local inventory = kap.inventory();

{
env: {
applications: inventory.applications,
classes: inventory.classes,
parameters: inventory.parameters,
exports: inventory.exports,
},
}
12 changes: 12 additions & 0 deletions examples/kubernetes/inventory/classes/jsonnet-env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
applications:
- a
- b
- c

classes:
- common

parameters:
a: aaaaa
b: bbbbb
c: ccccc
12 changes: 12 additions & 0 deletions examples/kubernetes/inventory/targets/jsonnet-env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
classes:
- jsonnet-env

parameters:
target_name: jsonnet-env
kapitan:
compile:
- output_path: jsonnet-env
input_type: jsonnet
input_paths:
- components/jsonnet-env/env.jsonnet
output_type: yml
9 changes: 6 additions & 3 deletions kapitan/inventory/inv_reclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@


class ReclassInventory(Inventory):

def render_targets(self, targets: list = None, ignore_class_notfound: bool = False):
"""
Runs a reclass inventory in inventory_path
Expand All @@ -28,7 +27,7 @@ def render_targets(self, targets: list = None, ignore_class_notfound: bool = Fal
reclass_config = get_reclass_config(self.inventory_path)
reclass_config.setdefault("ignore_class_notfound", ignore_class_notfound)
reclass_config["compose_node_name"] = self.compose_target_name

try:
storage = reclass.get_storage(
reclass_config["storage_type"],
Expand All @@ -44,6 +43,8 @@ def render_targets(self, targets: list = None, ignore_class_notfound: bool = Fal
for target_name, rendered_target in rendered_inventory["nodes"].items():
self.targets[target_name].parameters = rendered_target["parameters"]
self.targets[target_name].classes = rendered_target["classes"]
self.targets[target_name].applications = rendered_target["applications"]
self.targets[target_name].exports = rendered_target["exports"]

except ReclassException as e:
if isinstance(e, NotFoundError):
Expand Down Expand Up @@ -79,7 +80,9 @@ def get_reclass_config(inventory_path: str) -> dict:
for key, value in config.items():
reclass_config[key] = value
else:
logger.debug(f"Reclass config: Empty config file at {cfg_file}. Using reclass inventory config defaults")
logger.debug(
f"Reclass config: Empty config file at {cfg_file}. Using reclass inventory config defaults"
)
else:
logger.debug("Inventory reclass: No config file found. Using reclass inventory config defaults")

Expand Down
2 changes: 2 additions & 0 deletions kapitan/inventory/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class InventoryTarget:
composed_name: str
parameters: dict = field(default_factory=dict)
classes: list = field(default_factory=list)
applications: list = field(default_factory=list)
exports: list = field(default_factory=list)


class Inventory(ABC):
Expand Down
3 changes: 2 additions & 1 deletion kapitan/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"kapitan resources"

import base64
import dataclasses
import gzip
import io
import json
Expand Down Expand Up @@ -278,7 +279,7 @@ def inventory(search_paths: list, target_name: str = None, inventory_path: str =

if target_name:
target = inv.get_target(target_name)
return {"parameters": target.parameters, "classes": target.classes}
return dataclasses.asdict(target)

return inv.inventory

Expand Down
18 changes: 18 additions & 0 deletions tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,24 @@ def test_compile_target_with_label(self):
sys.argv = ["kapitan", "compile"]
main()

def test_compile_jsonnet_env(self):
shutil.rmtree("compiled")
sys.argv = ["kapitan", "compile", "-t", "jsonnet-env"]
main()
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))
self.assertEqual(set(env.keys()), {"applications", "parameters", "classes", "exports"})
self.assertEqual(env["applications"], ["a", "b", "c"])
self.assertEqual(env["classes"], ["common", "jsonnet-env"])
self.assertTrue("a" in env["parameters"])
self.assertEqual(env["parameters"]["a"], "aaaaa")
self.assertTrue("b" in env["parameters"])
self.assertEqual(env["parameters"]["b"], "bbbbb")
self.assertTrue("c" in env["parameters"])
self.assertEqual(env["parameters"]["c"], "ccccc")
self.assertEqual(env["exports"], {})

def tearDown(self):
os.chdir(os.getcwd() + "/../../")
reset_cache()
Expand Down
44 changes: 44 additions & 0 deletions tests/test_kubernetes_compiled/jsonnet-env/jsonnet-env/env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
applications:
- a
- b
- c
classes:
- common
- jsonnet-env
exports: {}
parameters:
_reclass_:
environment: base
name:
full: jsonnet-env
parts:
- jsonnet-env
path: jsonnet-env
short: jsonnet-env
a: aaaaa
b: bbbbb
c: ccccc
kapitan:
compile:
- input_params: {}
input_paths:
- components/jsonnet-env/env.jsonnet
input_type: jsonnet
output_path: jsonnet-env
output_type: yml
secrets:
awskms:
key: alias/nameOfKey
gkms:
key: projects/<project>/locations/<location>/keyRings/<keyRing>/cryptoKeys/<key>
gpg:
recipients:
- fingerprint: D9234C61F58BEB3ED8552A57E28DC07A3CBFAE7C
name: [email protected]
target_full_path: jsonnet-env
vars:
managed_by: kapitan
namespace: jsonnet-env
target: jsonnet-env
namespace: jsonnet-env
target_name: jsonnet-env

0 comments on commit b219af9

Please sign in to comment.