Skip to content

Commit

Permalink
feat: Don't make hooks part of diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock committed Oct 5, 2021
1 parent 5ce2e99 commit 49a0461
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
5 changes: 5 additions & 0 deletions kluctl/cli/command_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ def format_command_result_text(command_result: CommandResult):
changes = x["changes"]
result += "%s\n" % pretty_changes(get_object_ref(object), changes)

if command_result.hook_objects:
result += "Applied hooks:\n"
for x in command_result.hook_objects:
result += " %s\n" % get_long_object_name(x)

if command_result.orphan_objects:
result += "Orphan objects:\n"
for ref in command_result.orphan_objects:
Expand Down
22 changes: 13 additions & 9 deletions kluctl/deployment/apply_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ def __init__(self, deployment_collection, k8s_cluster, force_apply, replace_on_e
self.abort_on_error = abort_on_error

self.applied_objects = {}
self.applied_hook_objects = {}
self.abort_signal = False
self.error_refs = {}
self.mutex = threading.Lock()

def handle_result(self, applied_object, patch_warnings):
def handle_result(self, applied_object, patch_warnings, hook):
with self.mutex:
ref = get_object_ref(applied_object)
self.applied_objects[ref] = applied_object
if hook:
self.applied_hook_objects[ref] = applied_object
else:
self.applied_objects[ref] = applied_object
self.deployment_collection.add_warnings(ref, patch_warnings)

def handle_error(self, ref, error):
Expand All @@ -48,7 +52,7 @@ def had_error(self, ref):
def delete_object(self, ref):
self.k8s_cluster.delete_single_object(ref, force_dry_run=self.dry_run, ignore_not_found=True)

def apply_object(self, x, replaced):
def apply_object(self, x, replaced, hook):
logger.debug(f" {get_long_object_name(x)}")

x2 = self.k8s_cluster.fix_object_for_patch(x)
Expand All @@ -65,12 +69,12 @@ def apply_object(self, x, replaced):
if self.dry_run and replaced and get_object_ref(x) in self.deployment_collection.remote_objects:
# Let's simulate that this object was deleted in dry-run mode. If we'd actually try a dry-run apply with
# this object, it might fail as it is expected to not exist.
self.handle_result(x2, [])
self.handle_result(x2, [], hook)
return

try:
r, patch_warnings = self.k8s_cluster.patch_object(x2, force_dry_run=self.dry_run, force_apply=True)
self.handle_result(r, patch_warnings)
self.handle_result(r, patch_warnings, hook)
except ResourceNotFoundError as e:
ref = get_object_ref(x)
self.handle_error(ref, self.k8s_cluster.get_status_message(e))
Expand All @@ -89,7 +93,7 @@ def apply_object(self, x, replaced):
resource_version = get_dict_value(remote_object, "metadata.resourceVersion")
x2 = set_dict_value(x, "metadata.resourceVersion", resource_version, do_clone=True)
r, patch_warnings = self.k8s_cluster.replace_object(x2, force_dry_run=self.dry_run)
self.handle_result(r, patch_warnings)
self.handle_result(r, patch_warnings, hook)
except ApiException as e2:
self.handle_error(ref, self.k8s_cluster.get_status_message(e2))

Expand All @@ -103,9 +107,9 @@ def apply_object(self, x, replaced):
self.k8s_cluster.delete_single_object(ref, force_dry_run=self.dry_run, ignore_not_found=True)
if not self.dry_run:
r, patch_warnings = self.k8s_cluster.patch_object(x, force_apply=True)
self.handle_result(r, patch_warnings)
self.handle_result(r, patch_warnings, hook)
else:
self.handle_result(x, [])
self.handle_result(x, [], hook)
except ApiException as e2:
self.handle_error(ref, self.k8s_cluster.get_status_message(e2))

Expand Down Expand Up @@ -158,7 +162,7 @@ def apply_kustomize_deployment(self, d):
apply_objects.append(o)
self.do_log(d, logging.INFO, "Applying %d objects" % len(d.objects))
for o in apply_objects:
self.apply_object(o, False)
self.apply_object(o, False, False)

if inital_deploy:
hook_util.run_hooks(d, ["post-deploy-initial", "post-deploy"])
Expand Down
19 changes: 12 additions & 7 deletions kluctl/deployment/deployment_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class DeployErrorItem:
class CommandResult:
new_objects: list
changed_objects: list
hook_objects: list
orphan_objects: list
errors: list
warnings: list
Expand Down Expand Up @@ -142,21 +143,25 @@ def prepare(self, k8s_cluster):

def deploy(self, k8s_cluster, force_apply, replace_on_error, force_replace_on_error, abort_on_error):
self.clear_errors_and_warnings()
applied_objects = self.do_apply(k8s_cluster, force_apply, replace_on_error, force_replace_on_error,
False, abort_on_error)
applied_objects, applied_hook_objects = self.do_apply(k8s_cluster,
force_apply, replace_on_error, force_replace_on_error,
False, abort_on_error)
new_objects, changed_objects = self.do_diff(k8s_cluster, applied_objects, False, False, False, False)
orphan_objects = self.find_orphan_objects(k8s_cluster)
return CommandResult(new_objects=new_objects, changed_objects=changed_objects,
applied_hook_objects = list(applied_hook_objects.values())
return CommandResult(new_objects=new_objects, changed_objects=changed_objects, hook_objects=applied_hook_objects,
orphan_objects=orphan_objects,
errors=list(self.errors), warnings=list(self.warnings))

def diff(self, k8s_cluster, force_apply, replace_on_error, force_replace_on_error, ignore_tags, ignore_labels, ignore_annotations, ignore_order):
self.clear_errors_and_warnings()
applied_objects = self.do_apply(k8s_cluster, force_apply, replace_on_error, force_replace_on_error,
True, False)
applied_objects, applied_hook_objects = self.do_apply(k8s_cluster,
force_apply, replace_on_error, force_replace_on_error,
True, False)
new_objects, changed_objects = self.do_diff(k8s_cluster, applied_objects, ignore_tags, ignore_labels, ignore_annotations, ignore_order)
orphan_objects = self.find_orphan_objects(k8s_cluster)
return CommandResult(new_objects=new_objects, changed_objects=changed_objects,
applied_hook_objects = list(applied_hook_objects.values())
return CommandResult(new_objects=new_objects, changed_objects=changed_objects, hook_objects=applied_hook_objects,
orphan_objects=orphan_objects,
errors=list(self.errors), warnings=list(self.warnings))

Expand Down Expand Up @@ -273,7 +278,7 @@ def do_apply(self, k8s_cluster, force_apply, replace_on_error, force_replace_on_
dry_run = dry_run
apply_util = ApplyUtil(self, k8s_cluster, force_apply, replace_on_error, force_replace_on_error, dry_run, abort_on_error)
apply_util.apply_deployments()
return apply_util.applied_objects
return apply_util.applied_objects, apply_util.applied_hook_objects

def do_diff(self, k8s_cluster, applied_objects, ignore_tags, ignore_labels, ignore_annotations, ignore_order):
diff_objects = {}
Expand Down
2 changes: 1 addition & 1 deletion kluctl/deployment/hooks_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def do_log(level, str):
for h in apply_objects:
replaced = "before-hook-creation" in h.delete_policies
do_log(logging.DEBUG, "Applying hook %s" % get_long_object_name(h.object))
self.apply_util.apply_object(h.object, replaced)
self.apply_util.apply_object(h.object, replaced, True)

wait_results = {}
for h in l:
Expand Down

0 comments on commit 49a0461

Please sign in to comment.