From 64dce8c96860e47c3b70e03323a1bbc54f5742ad Mon Sep 17 00:00:00 2001 From: David Backeus Date: Wed, 9 Oct 2024 16:20:31 +0200 Subject: [PATCH] k run: allow it to run from cronjobs as well as deployments --- k | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/k b/k index b071473..14519d3 100755 --- a/k +++ b/k @@ -964,16 +964,23 @@ def run require "json" deployments_json = read_kubectl "get deployments -o json -l argocd.argoproj.io/instance=#{application}" - deployments = JSON.parse(deployments_json).fetch("items") - abort "Couldn't find any deployments for the application #{application}" if deployments.empty? - - require "securerandom" + resources = JSON.parse(deployments_json).fetch("items") + if resources.empty? + cronjobs_json = read_kubectl "get cronjobs -o json -l argocd.argoproj.io/instance=#{application}" + resources = JSON.parse(cronjobs_json).fetch("items") + abort "Couldn't find any deployments or cronjobs for the application #{application}" if resources.empty? + end - deployment = - deployments.find { |d| d.fetch("metadata").fetch("name").end_with?("-web") } || - deployments.first - pod_spec = deployment.fetch("spec").fetch("template").fetch("spec") - default_container = deployment.dig("spec", "template", "metadata", "annotations", "kubectl.kubernetes.io/default-container") + resource = + resources.find { |d| d.fetch("metadata").fetch("name").end_with?("-web") } || + resources.first + pod_spec = + if resource["kind"] == "Deployment" + resource.fetch("spec").fetch("template").fetch("spec") + else + resource.fetch("spec").fetch("jobTemplate").fetch("spec").fetch("template").fetch("spec") + end + default_container = resource.dig("spec", "template", "metadata", "annotations", "kubectl.kubernetes.io/default-container") run_container = if default_container 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 end other_containers = pod_spec.fetch("containers") - [run_container] + require "securerandom" + pod_name = "#{application}-run-#{SecureRandom.hex(3)}" pod_template = {