Skip to content

Commit 9a96889

Browse files
authored
Merge pull request #17 from p-wall/annotation-tracking
support annotation-based tracking
2 parents f97dc6c + d7f2827 commit 9a96889

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

k

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ def kubectl?(command, plugin: nil)
9999
Open3.capture3(cmd).last.success?
100100
end
101101

102+
def resources_for_argocd_application(kind, application)
103+
yaml_output = read_kubectl("get #{kind} -o yaml")
104+
resources = YAML.load(yaml_output).fetch("items")
105+
resources.select do |item|
106+
annotations = item.dig("metadata", "annotations") || {}
107+
tracking_id = annotations["argocd.argoproj.io/tracking-id"]
108+
109+
tracking_id&.start_with?("#{application}:") || # ArgoCD v3
110+
item.dig("metadata", "labels", "argocd.argoproj.io/instance") == application # ArgoCD v2
111+
end
112+
end
113+
102114
def read_kubectl(command, plugin: nil)
103115
require_plugin!(plugin) if plugin
104116
`kubectl #{plugin} --context #{KUBECTL_CONTEXT} #{command.strip}` # strip to allow leading newline
@@ -302,9 +314,8 @@ module ClickHouse
302314

303315
if cluster.empty?
304316
# Fall back looking for ClickHouse clusters in an application with the same name
305-
clusters = read_kubectl("get clickhouseinstallations -l argocd.argoproj.io/instance=#{cluster_or_application} -o name")
306-
.lines
307-
.map { |c| c.split("/").last.chomp }
317+
resources = resources_for_argocd_application("clickhouseinstallations", cluster_or_application)
318+
clusters = resources.map { |r| r.dig("metadata", "name") }
308319

309320
abort "Error: couldn't find a ClickHouse cluster named '#{cluster_or_application}'" if clusters.empty?
310321

@@ -675,8 +686,7 @@ def logs
675686
kail_executable = ENV["KAIL_PATH"] || "kail"
676687
abort "Please install kail (brew install kail) for logs support" unless system("which #{kail_executable} > /dev/null")
677688

678-
deployments_json = read_kubectl "get deployments -o yaml -l argocd.argoproj.io/instance=#{application}"
679-
deployments = YAML.load(deployments_json).fetch("items")
689+
deployments = resources_for_argocd_application("deployments", application)
680690
abort "Couldn't find any deployments for the application #{application}" if deployments.empty?
681691

682692
deployment_names = deployments.map { |deployment| deployment.fetch("metadata").fetch("name") }
@@ -695,8 +705,7 @@ def logs_search
695705
application = ARGV.delete_at(0)
696706
abort "Must pass name of application, eg. k logs:search <application> [<type1>, <type2>...]" unless application
697707

698-
deployments_json = read_kubectl "get deployments -o yaml -l argocd.argoproj.io/instance=#{application}"
699-
deployments = YAML.load(deployments_json).fetch("items")
708+
deployments = resources_for_argocd_application("deployments", application)
700709
abort "Couldn't find any deployments for the application #{application}" if deployments.empty?
701710

702711
deployment_names = deployments.map { |deployment| deployment.fetch("metadata").fetch("name") }
@@ -731,10 +740,9 @@ module Pg
731740
cluster = read_kubectl("get cluster.postgresql.cnpg.io #{cluster_or_application} -o name --ignore-not-found").chomp
732741

733742
if cluster.empty?
734-
# Fall back looking for ClickHouse clusters in an application with the same name
735-
clusters = read_kubectl("get cluster.postgresql.cnpg.io -l argocd.argoproj.io/instance=#{cluster_or_application} -o name")
736-
.lines
737-
.map { |c| c.split("/").last.chomp }
743+
# Fall back looking for Postgres clusters in an application with the same name
744+
resources = resources_for_argocd_application("cluster.postgresql.cnpg.io", cluster_or_application)
745+
clusters = resources.map { |r| r.dig("metadata", "name") }
738746

739747
abort "Error: couldn't find a Postgres cluster named '#{cluster_or_application}'" if clusters.empty?
740748

@@ -1142,11 +1150,9 @@ def run
11421150
abort "Must pass name of application, eg. k run <application> <command> [--disable-timeout]" unless application
11431151
abort "Must pass command to run, eg. k run <application> <command> [--disable-timeout]" if ARGV.empty?
11441152

1145-
deployments_json = read_kubectl "get deployments -o yaml -l argocd.argoproj.io/instance=#{application}"
1146-
resources = YAML.load(deployments_json).fetch("items")
1153+
resources = resources_for_argocd_application("deployments", application)
11471154
if resources.empty?
1148-
cronjobs_json = read_kubectl "get cronjobs -o yaml -l argocd.argoproj.io/instance=#{application}"
1149-
resources = YAML.load(cronjobs_json).fetch("items")
1155+
resources = resources_for_argocd_application("cronjobs", application)
11501156
abort "Couldn't find any deployments or cronjobs for the application #{application}" if resources.empty?
11511157
end
11521158

@@ -1489,18 +1495,12 @@ EOF)
14891495
# NOTE: This assumes that the application is using deployments or cron_jobs to run the image
14901496
# If some other resource type is used, this will time out.
14911497
1.upto(120) do |second|
1492-
deployments = read_kubectl %(
1493-
get deployments \
1494-
-l argocd.argoproj.io/instance=#{application} \
1495-
-o=jsonpath="{.items[*].spec.template.spec.containers[*].image}"
1496-
)
1498+
deployment_resources = resources_for_argocd_application("deployments", application)
1499+
deployments = deployment_resources.map { |r| r.dig("spec", "template", "spec", "containers").map { |c| c["image"] } }.flatten.join(" ")
14971500
break if deployments.include?(new_image)
14981501

1499-
cron_jobs = read_kubectl %(
1500-
get cronjobs \
1501-
-l argocd.argoproj.io/instance=#{application} \
1502-
-o=jsonpath="{.items[*].spec.jobTemplate.spec.template.spec.containers[*].image}"
1503-
)
1502+
cronjob_resources = resources_for_argocd_application("cronjobs", application)
1503+
cron_jobs = cronjob_resources.map { |r| r.dig("spec", "jobTemplate", "spec", "template", "spec", "containers").map { |c| c["image"] } }.flatten.join(" ")
15041504
break if cron_jobs.include?(new_image)
15051505

15061506
abort "Error: Timed out waiting for new image deployment" if second == 120

0 commit comments

Comments
 (0)