Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: implement compose_target_name #1138

Closed
wants to merge 8 commits into from
2 changes: 1 addition & 1 deletion examples/kubernetes/inventory/classes/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ parameters:

kapitan:
vars:
target: ${target_name}
target: ${_reclass_:name:full}
namespace: ${target_name}
managed_by: kapitan
secrets:
Expand Down
3 changes: 0 additions & 3 deletions examples/kubernetes/inventory/classes/component/busybox.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
parameters:
kapitan:
vars:
target: ${target_name}
namespace: ${target_name}
compile:
- output_path: ./copy
input_type: copy
Expand Down
29 changes: 13 additions & 16 deletions examples/kubernetes/inventory/classes/component/elasticsearch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,17 @@ parameters:
masters: ${elasticsearch:masters}

kapitan:
vars:
target: ${target_name}
namespace: ${target_name}
compile:
- output_path: manifests
input_type: jsonnet
input_paths:
- components/elasticsearch/main.jsonnet
output_type: yml
- output_path: scripts
input_type: jinja2
input_paths:
- scripts
- output_path: .
input_type: jinja2
input_paths:
- docs/elasticsearch/README.md
- output_path: manifests
input_type: jsonnet
input_paths:
- components/elasticsearch/main.jsonnet
output_type: yml
- output_path: scripts
input_type: jinja2
input_paths:
- scripts
- output_path: .
input_type: jinja2
input_paths:
- docs/elasticsearch/README.md
3 changes: 0 additions & 3 deletions examples/kubernetes/inventory/classes/component/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ parameters:
version: "11.0"

kapitan:
vars:
target: ${target_name}
namespace: ${target_name}
compile:
- output_path: ./labels
input_type: jinja2
Expand Down
52 changes: 26 additions & 26 deletions examples/kubernetes/inventory/classes/component/mysql.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
parameters:
kapitan:
compile:
- output_path: manifests
input_type: jsonnet
prune: true
input_paths:
- components/mysql/main.jsonnet
output_type: yml
- output_path: scripts
input_type: jinja2
input_paths:
- scripts
- output_path: .
output_type: yml
input_type: jinja2
input_paths:
- docs/mysql/README.md
- output_path: manifests
input_type: jsonnet
prune: true
input_paths:
- components/mysql/main.jsonnet
output_type: yml
- output_path: scripts
input_type: jinja2
input_paths:
- scripts
- output_path: .
output_type: yml
input_type: jinja2
input_paths:
- docs/mysql/README.md
validate:
- type: kubernetes # mkdocs (1)!
output_paths: # mkdocs (2)!
- manifests/mysql_secret.yml
kind: secret # temporarily replaced with 'deployment' during test # mkdocs (3)!
version: 1.14.0 # optional, defaults to 1.14.0 # mkdocs (4)!
- type: kubernetes
output_paths:
- manifests/mysql_service_jsonnet.yml
- manifests/mysql_service_simple.yml
kind: service
version: 1.14.0
- type: kubernetes # mkdocs (1)!
output_paths: # mkdocs (2)!
- manifests/mysql_secret.yml
kind: secret # temporarily replaced with 'deployment' during test # mkdocs (3)!
version: 1.14.0 # optional, defaults to 1.14.0 # mkdocs (4)!
- type: kubernetes
output_paths:
- manifests/mysql_service_jsonnet.yml
- manifests/mysql_service_simple.yml
kind: service
version: 1.14.0
# For vaultkv secrets it is important to declare auth in parameters
secrets:
vaultkv:
Expand Down
10 changes: 5 additions & 5 deletions examples/kubernetes/inventory/classes/component/namespace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ parameters:
namespace: ${target_name}
kapitan:
compile:
- output_path: pre-deploy
input_type: jsonnet
output_type: yml
input_paths:
- components/namespace/main.jsonnet
- output_path: pre-deploy
input_type: jsonnet
output_type: yml
input_paths:
- components/namespace/main.jsonnet

10 changes: 5 additions & 5 deletions examples/kubernetes/inventory/classes/component/nginx-kadet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ parameters:
replicas: 2
kapitan:
compile:
- output_path: manifests
input_type: kadet
output_type: yml
input_paths:
- components/nginx-kadet/
- output_path: manifests
input_type: kadet
output_type: yml
input_paths:
- components/nginx-kadet/
labels:
type: kadet
2 changes: 1 addition & 1 deletion examples/kubernetes/inventory/targets/all-glob.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ parameters:
storage: 10G
users:
root:
password: ?{base64:targets/${target_name}/mysql/password||randomstr|base64}
password: ?{base64:targets/${target_name}/mysql/password||random:str|base64}
password_sha256: ?{base64:targets/${target_name}/mysql/password_sha256||reveal:targets/${target_name}/mysql/password|sha256|base64}
password_subvar: ?{base64:targets/${target_name}/mysql/[email protected]}
password_sha256_subvar: ?{base64:targets/${target_name}/mysql/[email protected]_sha256}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ classes:
- component.nginx-common

parameters:
# These parameters are redundand because automatically set by `common`
## target_name: minikube-nginx-helm
## namespace: ${target_name}
target_name: minikube-nginx-helm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ classes:
- component.nginx-common

parameters:
# These parameters are redundand because automatically set by `common`
## target_name: minikube-nginx-jsonnet
## namespace: ${target_name}
target_name: minikube-nginx-jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ classes:
- component.nginx-common

parameters:
# These parameters are redundand because automatically set by `common`
## target_name: minikube-nginx-kadet
## namespace: ${target_name}

target_name: minikube-nginx-kadet
6 changes: 4 additions & 2 deletions examples/kubernetes/inventory/targets/removal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ classes:
- common

parameters:
target_name: removal

kapitan:
compile:
- input_type: copy
Expand All @@ -11,5 +13,5 @@ parameters:
# test removal of a file
- input_type: remove
input_paths:
- compiled/${kapitan:vars:target}/copy_target
output_path: .
- compiled/${_reclass_:name:path}/copy_target
output_path: .
3 changes: 2 additions & 1 deletion kapitan/cached.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# SPDX-License-Identifier: Apache-2.0

"cached module"
from argparse import Namespace

inv = {}
inv_cache = {}
Expand All @@ -16,7 +17,7 @@
dot_kapitan = {}
ref_controller_obj = None
revealer_obj = None
args = {} # args won't need resetting
args = Namespace() # args won't need resetting
inv_sources = set()


Expand Down
34 changes: 17 additions & 17 deletions kapitan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def trigger_compile(args):
args.targets,
args.labels,
ref_controller,
prune=(args.prune),
prune=args.prune,
indent=args.indent,
reveal=args.reveal,
cache=args.cache,
Expand All @@ -92,9 +92,9 @@ def trigger_compile(args):
validate=args.validate,
schemas_path=args.schemas_path,
jinja2_filters=args.jinja2_filters,
verbose=hasattr(args, "verbose") and args.verbose,
verbose=args.verbose,
use_go_jsonnet=args.use_go_jsonnet,
compose_node_name=args.compose_node_name,
compose_target_name=args.compose_target_name or args.compose_node_name,
)


Expand All @@ -111,6 +111,12 @@ def build_parser():
choices=AVAILABLE_BACKENDS.keys(),
help="Select the inventory backend to use (default=reclass)",
)
inventory_backend_parser.add_argument(
"--compose-target-name",
help="Create same subfolder structure from inventory/targets inside compiled folder",
action="store_true",
default=from_dot_kapitan("inventory_backend", "compose-target-name", False),
)

eval_parser = subparser.add_parser("eval", aliases=["e"], help="evaluate jsonnet file")
eval_parser.add_argument("jsonnet_file", type=str)
Expand Down Expand Up @@ -283,10 +289,7 @@ def build_parser():
default=from_dot_kapitan("compile", "use-go-jsonnet", False),
)

# compose-node-name should be used in conjunction with reclass
# config "compose_node_name: true". This allows us to make the same subfolder
# structure in the inventory folder inside the compiled folder
# https:/kapicorp/kapitan/issues/932
# deprecated
compile_parser.add_argument(
"--compose-node-name",
help="Create same subfolder structure from inventory/targets inside compiled folder",
Expand Down Expand Up @@ -655,22 +658,19 @@ def main():

logger.debug("Running with args: %s", args)

try:
cmd = sys.argv[1]
except IndexError:
if len(sys.argv) < 2:
parser.print_help()
sys.exit(1)

# cache args where key is subcommand
assert "name" in args, "All cli commands must have provided default name"
cached.args[args.name] = args
if "inventory_backend" in args:
cached.args["inventory-backend"] = args.inventory_backend
cached.args = args

if hasattr(args, "verbose") and args.verbose:
setup_logging(level=logging.DEBUG, force=True)
logging_level = logging.DEBUG
elif hasattr(args, "quiet") and args.quiet:
setup_logging(level=logging.CRITICAL, force=True)
logging_level = logging.CRITICAL
else:
logging_level = logging.INFO
setup_logging(level=logging_level, force=True)

# call chosen command
args.func(args)
Expand Down
10 changes: 5 additions & 5 deletions kapitan/inputs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def compile_input_path(self, input_path, comp_obj, ext_vars, **kwargs):

logger.debug("Compiling %s", input_path)
try:
if kwargs.get("compose_node_name", False):
if kwargs.get("compose_target_name", False):
_compile_path = os.path.join(self.compile_path, target_name.replace(".", "/"), output_path)
else:
_compile_path = os.path.join(self.compile_path, target_name, output_path)
Expand All @@ -87,10 +87,10 @@ def compile_input_path(self, input_path, comp_obj, ext_vars, **kwargs):
def make_compile_dirs(self, target_name, output_path, **kwargs):
"""make compile dirs, skips if dirs exist"""
_compile_path = os.path.join(self.compile_path, target_name, output_path)
if kwargs.get("compose_node_name", False):
os.makedirs(_compile_path.replace(".", "/"), exist_ok=True)
else:
os.makedirs(_compile_path, exist_ok=True)
if kwargs.get("compose_target_name", False):
_compile_path = _compile_path.replace(".", "/")

os.makedirs(_compile_path, exist_ok=True)

def compile_file(self, file_path, compile_path, ext_vars, **kwargs):
"""implements compilation for file_path to compile_path with ext_vars"""
Expand Down
2 changes: 1 addition & 1 deletion kapitan/inputs/jinja2_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def load_jinja2_filters_from_file(env, jinja2_filters):
# Custom filters
def reveal_maybe(ref_tag):
"Will reveal ref_tag if valid and --reveal flag is used"
if cached.args["compile"].reveal:
if cached.args.reveal:
return cached.revealer_obj.reveal_raw(ref_tag)
else:
return ref_tag
Expand Down
5 changes: 2 additions & 3 deletions kapitan/inputs/kadet.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
kadet.ABORT_EXCEPTION_TYPE = CompileError

logger = logging.getLogger(__name__)
inventory_path = cached.args.get(
"inventory_path"
) # XXX think about this as it probably breaks usage as library
inventory_path = vars(cached.args).get("inventory_path")
# XXX think about this as it probably breaks usage as library
search_paths = contextvars.ContextVar("current search_paths in thread")
current_target = contextvars.ContextVar("current target in thread")

Expand Down
5 changes: 3 additions & 2 deletions kapitan/inventory/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def search_targets(self) -> dict:
# check for same name
if self.targets.get(target.name):
raise InventoryError(
f"Conflicting targets {target.name}: {target.path} and {self.targets[target.name].path}"
f"Conflicting targets {target.name}: {target.path} and {self.targets[target.name].path}. "
f"Consider using '--compose-target-name'."
)

self.targets[target.name] = target
Expand Down Expand Up @@ -113,7 +114,7 @@ def get_targets(self, target_names: list, ignore_class_not_found: bool = False)

self.render_targets(targets_to_render, ignore_class_not_found)

return {name: target for name, target in self.targets.items() if name in target_names}
return {target_name: self.targets[target_name] for target_name in target_names}

def get_parameters(self, target_names: Union[str, list], ignore_class_not_found: bool = False) -> dict:
"""
Expand Down
Loading
Loading