@@ -305,8 +305,8 @@ module ClickHouse
305
305
abort "Error: couldn't find a ClickHouse cluster named '#{ cluster_or_application } '" if clusters . empty?
306
306
307
307
if clusters . length == 1
308
- puts "Defaulted to ClickHouse cluster #{ bold ( clusters . first ) } in application #{ bold ( cluster_or_application ) } ."
309
- puts
308
+ STDERR . puts gray ( "Defaulted to ClickHouse cluster #{ bold ( clusters . first ) } in application #{ bold ( cluster_or_application ) } ." )
309
+ STDERR . puts
310
310
cluster = clusters . first
311
311
else
312
312
puts "Multiple ClickHouse clusters found in application #{ cluster_or_application } , please specify one of:"
@@ -723,6 +723,31 @@ def logs_search
723
723
end
724
724
725
725
module Pg
726
+ def self . cluster_by_name ( cluster_or_application )
727
+ cluster = read_kubectl ( "get cluster.postgresql.cnpg.io #{ cluster_or_application } -o name --ignore-not-found" ) . chomp
728
+
729
+ if cluster . empty?
730
+ # Fall back looking for ClickHouse clusters in an application with the same name
731
+ clusters = read_kubectl ( "get cluster.postgresql.cnpg.io -l argocd.argoproj.io/instance=#{ cluster_or_application } -o name" )
732
+ . lines
733
+ . map { |c | c . split ( "/" ) . last . chomp }
734
+
735
+ abort "Error: couldn't find a Postgres cluster named '#{ cluster_or_application } '" if clusters . empty?
736
+
737
+ if clusters . length == 1
738
+ cluster = clusters . first
739
+ STDERR . puts gray ( "Defaulted to Postgres cluster #{ bold ( cluster ) } in application #{ bold ( cluster_or_application ) } ." )
740
+ STDERR . puts
741
+ else
742
+ puts "Multiple Postgres clusters found in application #{ cluster_or_application } , please specify one of:"
743
+ puts clusters
744
+ abort
745
+ end
746
+ end
747
+
748
+ cluster . split ( "/" ) . last . chomp
749
+ end
750
+
726
751
def self . secret_for_cluster ( cluster_name , user = nil )
727
752
unless user . nil? || user == "superuser"
728
753
abort "ERROR: CloudNativePG clusters only support default (ie. do not specify) or superuser users"
@@ -767,6 +792,7 @@ module Pg
767
792
end
768
793
769
794
def self . query_on_primary ( cluster , query )
795
+ cluster = cluster_by_name ( cluster )
770
796
secret = secret_for_cluster ( cluster )
771
797
772
798
require "base64"
@@ -781,7 +807,6 @@ def pg
781
807
puts ""
782
808
puts "POSTGRES COMMANDS:"
783
809
puts "pg:failover <cluster-name>"
784
- puts "pg:patroni <cluster-name> <patroni-command>"
785
810
puts "pg:password <cluster-name> [superuser]"
786
811
puts "pg:pods"
787
812
puts "pg:primaries"
@@ -803,54 +828,43 @@ def pg_failover
803
828
cluster = ARGV . delete_at ( 0 )
804
829
abort "Must pass name of cluster, eg. k pg:failover <cluster-name>" unless cluster
805
830
806
- if kubectl? ( "get cluster.postgresql.cnpg.io #{ cluster } " )
807
- # CloudNativePG
808
- status_yaml = read_kubectl ( "status -o yaml #{ cluster } " , plugin : "cnpg" )
809
- abort "Error: cluster '#{ cluster } ' not found" if status_yaml . empty?
810
-
811
- # {
812
- # "cluster" => {
813
- # "status" => {
814
- # "instancesReportedState" => {
815
- # "<cluster-name>-1": {
816
- # "isPrimary": true,
817
- # "timeLineID": 1
818
- # },
819
- # "<cluster-name>-2": {
820
- # "isPrimary": false,
821
- # "timeLineID": 1
822
- # }
823
- # }
824
- # ...
825
- # }
826
- # ...
827
- # }
828
- # ...
829
- # }
830
- status = YAML . load ( status_yaml )
831
- instance_state = status . dig ( "cluster" , "status" , "instancesReportedState" )
832
- non_primary_instance = instance_state . find { |_ , state | !state [ "isPrimary" ] }
833
- abort "No non-primary instance found for cluster '#{ cluster } '" unless non_primary_instance
834
-
835
- puts "Promoting #{ non_primary_instance . first } to primary..."
836
- kubectl "promote #{ cluster } #{ non_primary_instance . first } " , plugin : "cnpg"
837
- else
838
- abort "Error: cluster '#{ cluster } ' not found"
839
- end
840
- end
841
-
842
- def pg_patroni
843
- cluster = ARGV . delete_at ( 0 )
844
- abort "Must pass name of cluster, eg. k pg:patroni <cluster-name> <command>" unless cluster
845
-
846
- patroni_command = ARGV . delete_at ( 0 ) || "--help"
847
-
848
- Pg . exec_on_primary ( cluster , "patronictl #{ patroni_command } " )
831
+ cluster = Pg . cluster_by_name ( cluster )
832
+
833
+ status_yaml = read_kubectl ( "status -o yaml #{ cluster } " , plugin : "cnpg" )
834
+
835
+ # {
836
+ # "cluster" => {
837
+ # "status" => {
838
+ # "instancesReportedState" => {
839
+ # "<cluster-name>-1": {
840
+ # "isPrimary": true,
841
+ # "timeLineID": 1
842
+ # },
843
+ # "<cluster-name>-2": {
844
+ # "isPrimary": false,
845
+ # "timeLineID": 1
846
+ # }
847
+ # }
848
+ # ...
849
+ # }
850
+ # ...
851
+ # }
852
+ # ...
853
+ # }
854
+ status = YAML . load ( status_yaml )
855
+ instance_state = status . dig ( "cluster" , "status" , "instancesReportedState" )
856
+ non_primary_instance = instance_state . find { |_ , state | !state [ "isPrimary" ] }
857
+ abort "No non-primary instance found for cluster '#{ cluster } '" unless non_primary_instance
858
+
859
+ puts "Promoting #{ non_primary_instance . first } to primary..."
860
+ kubectl "promote #{ cluster } #{ non_primary_instance . first } " , plugin : "cnpg"
849
861
end
850
862
851
863
def pg_password
852
864
cluster = ARGV . delete_at ( 0 )
853
- abort "Must pass name of cluster, eg. k pg:password <cluster-name> [<user>]" unless cluster
865
+ abort "Must pass name of cluster, eg. k pg:password <cluster-name> [superuser]" unless cluster
866
+
867
+ cluster = Pg . cluster_by_name ( cluster )
854
868
855
869
user = ARGV . delete_at ( 0 )
856
870
876
890
877
891
def pg_psql
878
892
cluster_name = ARGV . delete_at ( 0 )
879
- abort "Must pass name of cluster, eg. k pg:psql <cluster-name> [<user>]" unless cluster_name
893
+ abort "Must pass name of cluster, eg. k pg:psql <cluster-name> [superuser]" unless cluster_name
894
+
895
+ cluster_name = Pg . cluster_by_name ( cluster_name )
880
896
881
897
user = ARGV . delete_at ( 0 )
882
898
@@ -895,7 +911,17 @@ def pg_resources
895
911
cluster = ARGV . delete_at ( 0 )
896
912
abort "Must pass name of cluster, eg. k pg:resources <cluster-name>" unless cluster
897
913
898
- kubectl "get all --selector=cnpg.io/cluster=#{ cluster } "
914
+ cluster = Pg . cluster_by_name ( cluster )
915
+
916
+ puts gray ( "===" ) + " " + bold ( "Pods" )
917
+ puts
918
+ kubectl "get pods --selector=cnpg.io/cluster=#{ cluster } "
919
+ puts
920
+ puts gray ( "===" ) + " " + bold ( "Services" )
921
+ puts
922
+ kubectl "get services --selector=cnpg.io/cluster=#{ cluster } "
923
+ puts
924
+ puts gray ( "===" ) + " " + bold ( "Secrets" )
899
925
puts
900
926
kubectl "get secrets --selector=cnpg.io/cluster=#{ cluster } "
901
927
end
@@ -904,6 +930,8 @@ def pg_url
904
930
cluster_name = ARGV . delete_at ( 0 )
905
931
abort "Must pass name of cluster, eg. k pg:url <cluster-name> [<user>]" unless cluster_name
906
932
933
+ cluster_name = Pg . cluster_by_name ( cluster_name )
934
+
907
935
user = ARGV . delete_at ( 0 )
908
936
secret = Pg . secret_for_cluster ( cluster_name , user )
909
937
0 commit comments