Skip to content

Commit 64dce8c

Browse files
committed
k run: allow it to run from cronjobs as well as deployments
1 parent 6ead6d4 commit 64dce8c

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

k

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -964,16 +964,23 @@ def run
964964
require "json"
965965

966966
deployments_json = read_kubectl "get deployments -o json -l argocd.argoproj.io/instance=#{application}"
967-
deployments = JSON.parse(deployments_json).fetch("items")
968-
abort "Couldn't find any deployments for the application #{application}" if deployments.empty?
969-
970-
require "securerandom"
967+
resources = JSON.parse(deployments_json).fetch("items")
968+
if resources.empty?
969+
cronjobs_json = read_kubectl "get cronjobs -o json -l argocd.argoproj.io/instance=#{application}"
970+
resources = JSON.parse(cronjobs_json).fetch("items")
971+
abort "Couldn't find any deployments or cronjobs for the application #{application}" if resources.empty?
972+
end
971973

972-
deployment =
973-
deployments.find { |d| d.fetch("metadata").fetch("name").end_with?("-web") } ||
974-
deployments.first
975-
pod_spec = deployment.fetch("spec").fetch("template").fetch("spec")
976-
default_container = deployment.dig("spec", "template", "metadata", "annotations", "kubectl.kubernetes.io/default-container")
974+
resource =
975+
resources.find { |d| d.fetch("metadata").fetch("name").end_with?("-web") } ||
976+
resources.first
977+
pod_spec =
978+
if resource["kind"] == "Deployment"
979+
resource.fetch("spec").fetch("template").fetch("spec")
980+
else
981+
resource.fetch("spec").fetch("jobTemplate").fetch("spec").fetch("template").fetch("spec")
982+
end
983+
default_container = resource.dig("spec", "template", "metadata", "annotations", "kubectl.kubernetes.io/default-container")
977984
run_container =
978985
if default_container
979986
pod_spec.fetch("containers").find { |c| c.fetch("name") == default_container } or abort "Error: The deployment did not include a container named #{default_container}"
@@ -982,6 +989,8 @@ def run
982989
end
983990
other_containers = pod_spec.fetch("containers") - [run_container]
984991

992+
require "securerandom"
993+
985994
pod_name = "#{application}-run-#{SecureRandom.hex(3)}"
986995

987996
pod_template = {

0 commit comments

Comments
 (0)