Skip to content

Commit

Permalink
Fix apply ordering of created and replaced resources
Browse files Browse the repository at this point in the history
  • Loading branch information
timothysmith0609 committed Jan 16, 2025
1 parent 6236b52 commit 1bd6f54
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/krane/resource_deployer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def deploy!(resources, verify_result, prune)
end
raise FatalDeploymentError unless success
else
deploy_all_resources(resources, prune: prune, verify: false)
deploy_all_resources(resources, prune: prune, verify: false, annotate_individuals: true)
logger.summary.add_action("deployed #{resources.length} #{'resource'.pluralize(resources.length)}")
warning = <<~MSG
Deploy result verification is disabled for this deploy.
Expand Down Expand Up @@ -78,7 +78,7 @@ def deploy_all_resources(resources, prune: false, verify:, record_summary: true)
end
measure_method(:deploy_all_resources, 'normal_resources.duration')

def deploy_resources(resources, prune: false, verify:, record_summary: true)
def deploy_resources(resources, prune: false, verify:, record_summary: true, annotate_individuals: false)
return if resources.empty?
deploy_started_at = Time.now.utc

Expand All @@ -96,7 +96,6 @@ def deploy_resources(resources, prune: false, verify:, record_summary: true)
applyables, individuals = resources.partition { |r| r.deploy_method == :apply }
# Prunable resources should also applied so that they can be pruned
pruneable_types = @prune_allowlist.map { |t| t.split("/").last }
applyables += individuals.select { |r| pruneable_types.include?(r.type) && !r.deploy_method_override }

individuals.each do |individual_resource|
individual_resource.deploy_started_at = Time.now.utc
Expand All @@ -122,6 +121,10 @@ def deploy_resources(resources, prune: false, verify:, record_summary: true)

apply_all(applyables, prune)

if annotate_individuals
update_last_applied_annotations(individuals)
end

if verify
watcher = Krane::ResourceWatcher.new(resources: resources, deploy_started_at: deploy_started_at,
timeout: @global_timeout, task_config: @task_config, sha: @current_sha)
Expand Down Expand Up @@ -252,6 +255,19 @@ def create_resource(resource)
[err, status]
end

def update_last_applied_annotations(resources)
resources.each do |resource|
err, status = set_last_applied_annotation(resource)
raise FatalDeploymentError, "Failed to set last applied annotation: #{err}" unless status.success?
end
end

def set_last_applied_annotation(resource)
_, err, status = kubectl.run("kubectl", "apply", "set-last-applied", "-f", resource.file_path, log_failure: false,
output_is_sensitive: resource.sensitive_template_content?, use_namespace: !resource.global?)
[err, status]
end

# Inspect the file referenced in the kubectl stderr
# to make it easier for developer to understand what's going on
def find_bad_files_from_kubectl_output(line)
Expand Down

0 comments on commit 1bd6f54

Please sign in to comment.