@@ -99,6 +99,16 @@ def kubectl?(command, plugin: nil)
9999 Open3 . capture3 ( cmd ) . last . success?
100100end
101101
102+ def get_tracked_resources ( 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+ tracking_id &.start_with? ( "#{ application } :" )
109+ end
110+ end
111+
102112def read_kubectl ( command , plugin : nil )
103113 require_plugin! ( plugin ) if plugin
104114 `kubectl #{ plugin } --context #{ KUBECTL_CONTEXT } #{ command . strip } ` # strip to allow leading newline
@@ -302,9 +312,8 @@ module ClickHouse
302312
303313 if cluster . empty?
304314 # 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 }
315+ resources = get_tracked_resources ( "clickhouseinstallations" , cluster_or_application )
316+ clusters = resources . map { |r | r . dig ( "metadata" , "name" ) }
308317
309318 abort "Error: couldn't find a ClickHouse cluster named '#{ cluster_or_application } '" if clusters . empty?
310319
@@ -675,8 +684,7 @@ def logs
675684 kail_executable = ENV [ "KAIL_PATH" ] || "kail"
676685 abort "Please install kail (brew install kail) for logs support" unless system ( "which #{ kail_executable } > /dev/null" )
677686
678- deployments_json = read_kubectl "get deployments -o yaml -l argocd.argoproj.io/instance=#{ application } "
679- deployments = YAML . load ( deployments_json ) . fetch ( "items" )
687+ deployments = get_tracked_resources ( "deployments" , application )
680688 abort "Couldn't find any deployments for the application #{ application } " if deployments . empty?
681689
682690 deployment_names = deployments . map { |deployment | deployment . fetch ( "metadata" ) . fetch ( "name" ) }
@@ -695,8 +703,7 @@ def logs_search
695703 application = ARGV . delete_at ( 0 )
696704 abort "Must pass name of application, eg. k logs:search <application> [<type1>, <type2>...]" unless application
697705
698- deployments_json = read_kubectl "get deployments -o yaml -l argocd.argoproj.io/instance=#{ application } "
699- deployments = YAML . load ( deployments_json ) . fetch ( "items" )
706+ deployments = get_tracked_resources ( "deployments" , application )
700707 abort "Couldn't find any deployments for the application #{ application } " if deployments . empty?
701708
702709 deployment_names = deployments . map { |deployment | deployment . fetch ( "metadata" ) . fetch ( "name" ) }
@@ -731,10 +738,9 @@ module Pg
731738 cluster = read_kubectl ( "get cluster.postgresql.cnpg.io #{ cluster_or_application } -o name --ignore-not-found" ) . chomp
732739
733740 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 }
741+ # Fall back looking for Postgres clusters in an application with the same name
742+ resources = get_tracked_resources ( "cluster.postgresql.cnpg.io" , cluster_or_application )
743+ clusters = resources . map { |r | r . dig ( "metadata" , "name" ) }
738744
739745 abort "Error: couldn't find a Postgres cluster named '#{ cluster_or_application } '" if clusters . empty?
740746
@@ -1142,11 +1148,9 @@ def run
11421148 abort "Must pass name of application, eg. k run <application> <command> [--disable-timeout]" unless application
11431149 abort "Must pass command to run, eg. k run <application> <command> [--disable-timeout]" if ARGV . empty?
11441150
1145- deployments_json = read_kubectl "get deployments -o yaml -l argocd.argoproj.io/instance=#{ application } "
1146- resources = YAML . load ( deployments_json ) . fetch ( "items" )
1151+ resources = get_tracked_resources ( "deployments" , application )
11471152 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" )
1153+ resources = get_tracked_resources ( "cronjobs" , application )
11501154 abort "Couldn't find any deployments or cronjobs for the application #{ application } " if resources . empty?
11511155 end
11521156
@@ -1489,18 +1493,12 @@ EOF)
14891493 # NOTE: This assumes that the application is using deployments or cron_jobs to run the image
14901494 # If some other resource type is used, this will time out.
14911495 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- )
1496+ deployment_resources = get_tracked_resources ( "deployments" , application )
1497+ deployments = deployment_resources . map { |r | r . dig ( "spec" , "template" , "spec" , "containers" ) . map { |c | c [ "image" ] } } . flatten . join ( " " )
14971498 break if deployments . include? ( new_image )
14981499
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- )
1500+ cronjob_resources = get_tracked_resources ( "cronjobs" , application )
1501+ cron_jobs = cronjob_resources . map { |r | r . dig ( "spec" , "jobTemplate" , "spec" , "template" , "spec" , "containers" ) . map { |c | c [ "image" ] } } . flatten . join ( " " )
15041502 break if cron_jobs . include? ( new_image )
15051503
15061504 abort "Error: Timed out waiting for new image deployment" if second == 120
0 commit comments