From f651a6553422c1a60c20ecbcc66af84a695afb86 Mon Sep 17 00:00:00 2001 From: Tomer Brisker Date: Tue, 28 Jun 2022 11:56:49 +0300 Subject: [PATCH 1/4] Add GetClusterKey to runtime --- pkg/rosa/runtime.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/pkg/rosa/runtime.go b/pkg/rosa/runtime.go index 6da545be76..dcb9e83d31 100644 --- a/pkg/rosa/runtime.go +++ b/pkg/rosa/runtime.go @@ -11,11 +11,12 @@ import ( ) type Runtime struct { - Reporter *reporter.Object - Logger *logrus.Logger - OCMClient *ocm.Client - AWSClient aws.Client - Creator *aws.Creator + Reporter *reporter.Object + Logger *logrus.Logger + OCMClient *ocm.Client + AWSClient aws.Client + Creator *aws.Creator + ClusterKey string } func NewRuntime() *Runtime { @@ -55,3 +56,14 @@ func (r *Runtime) Cleanup() { } } } + +// Load the cluster key provided by the user into the runtime and return it +func (r *Runtime) GetClusterKey() string { + clusterKey, err := ocm.GetClusterKey() + if err != nil { + r.Reporter.Errorf("%s", err) + os.Exit(1) + } + r.ClusterKey = clusterKey + return clusterKey +} From b75c278951f25eb7d34e9b80f9267b19150e6ef8 Mon Sep 17 00:00:00 2001 From: Tomer Brisker Date: Tue, 28 Jun 2022 12:03:31 +0300 Subject: [PATCH 2/4] Migrate commands to use runtime GetClusterKey --- cmd/create/admin/cmd.go | 6 +----- cmd/create/idp/cmd.go | 6 +----- cmd/create/ingress/cmd.go | 8 ++------ cmd/create/machinepool/cmd.go | 6 +----- cmd/create/oidcprovider/cmd.go | 6 +----- cmd/create/operatorroles/cmd.go | 6 +----- cmd/describe/admin/cmd.go | 6 +----- cmd/describe/cluster/cmd.go | 6 +----- cmd/dlt/admin/cmd.go | 6 +----- cmd/dlt/cluster/cmd.go | 6 +----- cmd/dlt/idp/cmd.go | 6 +----- cmd/dlt/ingress/cmd.go | 6 +----- cmd/dlt/machinepool/cmd.go | 6 +----- cmd/dlt/oidcprovider/cmd.go | 6 +----- cmd/dlt/upgrade/cmd.go | 6 +----- cmd/edit/addon/cmd.go | 6 +----- cmd/edit/cluster/cmd.go | 6 +----- cmd/edit/ingress/cmd.go | 8 ++------ cmd/edit/machinepool/cmd.go | 6 +----- cmd/grant/user/cmd.go | 6 +----- cmd/hibernate/cluster/cmd.go | 6 +----- cmd/install/addon/cmd.go | 6 +----- cmd/list/gates/cmd.go | 6 +----- cmd/list/idp/cmd.go | 6 +----- cmd/list/ingress/cmd.go | 6 +----- cmd/list/machinepool/cmd.go | 6 +----- cmd/list/upgrade/cmd.go | 6 +----- cmd/list/user/cmd.go | 6 +----- cmd/logs/install/cmd.go | 6 +----- cmd/logs/uninstall/cmd.go | 6 +----- cmd/resume/cluster/cmd.go | 6 +----- cmd/revoke/user/cmd.go | 6 +----- cmd/uninstall/addon/cmd.go | 6 +----- cmd/upgrade/cluster/cmd.go | 6 +----- cmd/upgrade/operatorroles/cmd.go | 6 +----- 35 files changed, 37 insertions(+), 177 deletions(-) diff --git a/cmd/create/admin/cmd.go b/cmd/create/admin/cmd.go index e960237bc7..f97594c9a4 100644 --- a/cmd/create/admin/cmd.go +++ b/cmd/create/admin/cmd.go @@ -62,11 +62,7 @@ func run(cmd *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/create/idp/cmd.go b/cmd/create/idp/cmd.go index afddbc7c83..cff6b0f036 100644 --- a/cmd/create/idp/cmd.go +++ b/cmd/create/idp/cmd.go @@ -299,11 +299,7 @@ func run(cmd *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/create/ingress/cmd.go b/cmd/create/ingress/cmd.go index 318b393077..3334e86ab8 100644 --- a/cmd/create/ingress/cmd.go +++ b/cmd/create/ingress/cmd.go @@ -77,12 +77,8 @@ func run(cmd *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } - + clusterKey := r.GetClusterKey() + var err error labelMatch := args.labelMatch if interactive.Enabled() { labelMatch, err = interactive.GetString(interactive.Input{ diff --git a/cmd/create/machinepool/cmd.go b/cmd/create/machinepool/cmd.go index f6d6d886ea..b9b398681b 100644 --- a/cmd/create/machinepool/cmd.go +++ b/cmd/create/machinepool/cmd.go @@ -172,11 +172,7 @@ func run(cmd *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/create/oidcprovider/cmd.go b/cmd/create/oidcprovider/cmd.go index 4ccb9e25f8..b7afc4d1e9 100644 --- a/cmd/create/oidcprovider/cmd.go +++ b/cmd/create/oidcprovider/cmd.go @@ -73,11 +73,7 @@ func run(cmd *cobra.Command, argv []string) { } } - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() mode, err := aws.GetMode() if err != nil { diff --git a/cmd/create/operatorroles/cmd.go b/cmd/create/operatorroles/cmd.go index b672407590..2ea6f3a252 100644 --- a/cmd/create/operatorroles/cmd.go +++ b/cmd/create/operatorroles/cmd.go @@ -95,11 +95,7 @@ func run(cmd *cobra.Command, argv []string) { } } - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() mode, err := aws.GetMode() if err != nil { diff --git a/cmd/describe/admin/cmd.go b/cmd/describe/admin/cmd.go index cc0fe6dff7..a61dd7908e 100644 --- a/cmd/describe/admin/cmd.go +++ b/cmd/describe/admin/cmd.go @@ -44,11 +44,7 @@ func run(cmd *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/describe/cluster/cmd.go b/cmd/describe/cluster/cmd.go index b23f3c42f9..1248781dcd 100644 --- a/cmd/describe/cluster/cmd.go +++ b/cmd/describe/cluster/cmd.go @@ -62,11 +62,7 @@ func run(cmd *cobra.Command, argv []string) { if len(argv) == 1 && !cmd.Flag("cluster").Changed { clusterKey = argv[0] } else { - clusterKey, err = ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey = r.GetClusterKey() } // Try to find the cluster: diff --git a/cmd/dlt/admin/cmd.go b/cmd/dlt/admin/cmd.go index b5a7a6c949..e3d8a5facf 100644 --- a/cmd/dlt/admin/cmd.go +++ b/cmd/dlt/admin/cmd.go @@ -45,11 +45,7 @@ func run(cmd *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/dlt/cluster/cmd.go b/cmd/dlt/cluster/cmd.go index 68909cfc0c..3df6051ea4 100644 --- a/cmd/dlt/cluster/cmd.go +++ b/cmd/dlt/cluster/cmd.go @@ -63,11 +63,7 @@ func run(cmd *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() if !confirm.Confirm("delete cluster %s", clusterKey) { os.Exit(0) diff --git a/cmd/dlt/idp/cmd.go b/cmd/dlt/idp/cmd.go index 01a39105c7..01e9350979 100644 --- a/cmd/dlt/idp/cmd.go +++ b/cmd/dlt/idp/cmd.go @@ -57,11 +57,7 @@ func run(_ *cobra.Command, argv []string) { idpName := argv[0] - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/dlt/ingress/cmd.go b/cmd/dlt/ingress/cmd.go index c82caedd52..968d20e299 100644 --- a/cmd/dlt/ingress/cmd.go +++ b/cmd/dlt/ingress/cmd.go @@ -71,11 +71,7 @@ func run(_ *cobra.Command, argv []string) { os.Exit(1) } - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/dlt/machinepool/cmd.go b/cmd/dlt/machinepool/cmd.go index c5a3df2e99..2d64452326 100644 --- a/cmd/dlt/machinepool/cmd.go +++ b/cmd/dlt/machinepool/cmd.go @@ -65,11 +65,7 @@ func run(_ *cobra.Command, argv []string) { os.Exit(1) } - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() if machinePoolID == "Default" { r.Reporter.Errorf("Machine pool '%s' cannot be deleted from cluster '%s'", machinePoolID, clusterKey) diff --git a/cmd/dlt/oidcprovider/cmd.go b/cmd/dlt/oidcprovider/cmd.go index 073537e6d2..529da93dad 100644 --- a/cmd/dlt/oidcprovider/cmd.go +++ b/cmd/dlt/oidcprovider/cmd.go @@ -56,11 +56,7 @@ func run(cmd *cobra.Command, argv []string) { ocm.SetClusterKey(argv[0]) } - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() mode, err := aws.GetMode() if err != nil { diff --git a/cmd/dlt/upgrade/cmd.go b/cmd/dlt/upgrade/cmd.go index 53d18f20c9..3d1eaf8884 100644 --- a/cmd/dlt/upgrade/cmd.go +++ b/cmd/dlt/upgrade/cmd.go @@ -43,11 +43,7 @@ func run(cmd *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/edit/addon/cmd.go b/cmd/edit/addon/cmd.go index 02b44dc09f..fa7db1fc5e 100644 --- a/cmd/edit/addon/cmd.go +++ b/cmd/edit/addon/cmd.go @@ -68,11 +68,7 @@ func run(cmd *cobra.Command, argv []string) { argv = cmd.Flags().Args() addOnID := argv[0] - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/edit/cluster/cmd.go b/cmd/edit/cluster/cmd.go index ab8356ea2f..e1c6bc31c1 100644 --- a/cmd/edit/cluster/cmd.go +++ b/cmd/edit/cluster/cmd.go @@ -132,11 +132,7 @@ func run(cmd *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Enable interactive mode if no flags have been set if !interactive.Enabled() { diff --git a/cmd/edit/ingress/cmd.go b/cmd/edit/ingress/cmd.go index 372721895e..2a7773e4cf 100644 --- a/cmd/edit/ingress/cmd.go +++ b/cmd/edit/ingress/cmd.go @@ -97,12 +97,8 @@ func run(cmd *cobra.Command, argv []string) { os.Exit(1) } - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } - + clusterKey := r.GetClusterKey() + var err error labelMatch := args.labelMatch routeSelectors := make(map[string]string) if interactive.Enabled() { diff --git a/cmd/edit/machinepool/cmd.go b/cmd/edit/machinepool/cmd.go index 5ea4eb7cae..63e15652dc 100644 --- a/cmd/edit/machinepool/cmd.go +++ b/cmd/edit/machinepool/cmd.go @@ -124,11 +124,7 @@ func run(cmd *cobra.Command, argv []string) { os.Exit(1) } - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/grant/user/cmd.go b/cmd/grant/user/cmd.go index e02b3b8967..c30317e460 100644 --- a/cmd/grant/user/cmd.go +++ b/cmd/grant/user/cmd.go @@ -75,11 +75,7 @@ func run(_ *cobra.Command, argv []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() username := args.username if !ocm.IsValidUsername(username) { diff --git a/cmd/hibernate/cluster/cmd.go b/cmd/hibernate/cluster/cmd.go index f8f07392ec..356d9ff223 100644 --- a/cmd/hibernate/cluster/cmd.go +++ b/cmd/hibernate/cluster/cmd.go @@ -44,11 +44,7 @@ func run(cmd *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Get the cluster to check the state cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) diff --git a/cmd/install/addon/cmd.go b/cmd/install/addon/cmd.go index 8d0b4367b4..e916295a36 100644 --- a/cmd/install/addon/cmd.go +++ b/cmd/install/addon/cmd.go @@ -72,11 +72,7 @@ func run(cmd *cobra.Command, argv []string) { argv = cmd.Flags().Args() addOnID := argv[0] - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/list/gates/cmd.go b/cmd/list/gates/cmd.go index 47a826b2f9..f6174cadf9 100644 --- a/cmd/list/gates/cmd.go +++ b/cmd/list/gates/cmd.go @@ -110,11 +110,7 @@ func run(cmd *cobra.Command, _ []string) { r = r.WithAWS() ocm.SetClusterKey(args.clusterKey) - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/list/idp/cmd.go b/cmd/list/idp/cmd.go index 955b681699..bec9d33f1c 100644 --- a/cmd/list/idp/cmd.go +++ b/cmd/list/idp/cmd.go @@ -49,11 +49,7 @@ func run(_ *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/list/ingress/cmd.go b/cmd/list/ingress/cmd.go index 3e04ac795a..3ea12b1c10 100644 --- a/cmd/list/ingress/cmd.go +++ b/cmd/list/ingress/cmd.go @@ -49,11 +49,7 @@ func run(_ *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/list/machinepool/cmd.go b/cmd/list/machinepool/cmd.go index 0326d98a77..eb1b1188aa 100644 --- a/cmd/list/machinepool/cmd.go +++ b/cmd/list/machinepool/cmd.go @@ -49,11 +49,7 @@ func run(_ *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/list/upgrade/cmd.go b/cmd/list/upgrade/cmd.go index 1b47601237..b27c0a3078 100644 --- a/cmd/list/upgrade/cmd.go +++ b/cmd/list/upgrade/cmd.go @@ -46,11 +46,7 @@ func run(_ *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/list/user/cmd.go b/cmd/list/user/cmd.go index 8c93dcc1cc..c5fecdc47a 100644 --- a/cmd/list/user/cmd.go +++ b/cmd/list/user/cmd.go @@ -47,11 +47,7 @@ func run(_ *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/logs/install/cmd.go b/cmd/logs/install/cmd.go index 78254751ea..7b07d68af3 100644 --- a/cmd/logs/install/cmd.go +++ b/cmd/logs/install/cmd.go @@ -85,11 +85,7 @@ func run(cmd *cobra.Command, argv []string) { clusterKey = argv[0] watch = true } else { - clusterKey, err = ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey = r.GetClusterKey() } // Try to find the cluster: diff --git a/cmd/logs/uninstall/cmd.go b/cmd/logs/uninstall/cmd.go index 237e11bfdc..6cc0740f67 100644 --- a/cmd/logs/uninstall/cmd.go +++ b/cmd/logs/uninstall/cmd.go @@ -85,11 +85,7 @@ func run(cmd *cobra.Command, argv []string) { clusterKey = argv[0] watch = true } else { - clusterKey, err = ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey = r.GetClusterKey() } // Try to find the cluster: diff --git a/cmd/resume/cluster/cmd.go b/cmd/resume/cluster/cmd.go index 24714b4ed4..eb7a406a72 100644 --- a/cmd/resume/cluster/cmd.go +++ b/cmd/resume/cluster/cmd.go @@ -44,11 +44,7 @@ func run(cmd *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Get the cluster to check the state cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) if err != nil { diff --git a/cmd/revoke/user/cmd.go b/cmd/revoke/user/cmd.go index 4d153698cc..dc1a37cdea 100644 --- a/cmd/revoke/user/cmd.go +++ b/cmd/revoke/user/cmd.go @@ -75,11 +75,7 @@ func run(_ *cobra.Command, argv []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() username := args.username if !ocm.IsValidUsername(username) { diff --git a/cmd/uninstall/addon/cmd.go b/cmd/uninstall/addon/cmd.go index 790270e3fa..534fbf66c0 100644 --- a/cmd/uninstall/addon/cmd.go +++ b/cmd/uninstall/addon/cmd.go @@ -56,11 +56,7 @@ func run(_ *cobra.Command, argv []string) { addOnID := argv[0] - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() // Try to find the cluster: r.Reporter.Debugf("Loading cluster '%s'", clusterKey) diff --git a/cmd/upgrade/cluster/cmd.go b/cmd/upgrade/cluster/cmd.go index daf1b49b76..25fec9dbb7 100644 --- a/cmd/upgrade/cluster/cmd.go +++ b/cmd/upgrade/cluster/cmd.go @@ -110,11 +110,7 @@ func run(cmd *cobra.Command, _ []string) { r := rosa.NewRuntime().WithAWS().WithOCM() defer r.Cleanup() - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() mode, err := aws.GetMode() if err != nil { diff --git a/cmd/upgrade/operatorroles/cmd.go b/cmd/upgrade/operatorroles/cmd.go index 70fa386e0a..316cb0dab1 100644 --- a/cmd/upgrade/operatorroles/cmd.go +++ b/cmd/upgrade/operatorroles/cmd.go @@ -91,11 +91,7 @@ func run(cmd *cobra.Command, argv []string) error { os.Exit(1) } - clusterKey, err := ocm.GetClusterKey() - if err != nil { - r.Reporter.Errorf("%s", err) - os.Exit(1) - } + clusterKey := r.GetClusterKey() defaultPolicyVersion, err := r.OCMClient.GetDefaultVersion() if err != nil { From ef9507c7703486d72ff6ee58e7ff05fd53f97344 Mon Sep 17 00:00:00 2001 From: Tomer Brisker Date: Tue, 28 Jun 2022 12:48:31 +0300 Subject: [PATCH 3/4] Add FetchCluster method to runtime --- pkg/rosa/runtime.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pkg/rosa/runtime.go b/pkg/rosa/runtime.go index dcb9e83d31..edabdcaa0f 100644 --- a/pkg/rosa/runtime.go +++ b/pkg/rosa/runtime.go @@ -3,6 +3,7 @@ package rosa import ( "os" + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/openshift/rosa/pkg/aws" "github.com/openshift/rosa/pkg/logging" "github.com/openshift/rosa/pkg/ocm" @@ -17,6 +18,7 @@ type Runtime struct { AWSClient aws.Client Creator *aws.Creator ClusterKey string + Cluster *cmv1.Cluster } func NewRuntime() *Runtime { @@ -67,3 +69,30 @@ func (r *Runtime) GetClusterKey() string { r.ClusterKey = clusterKey return clusterKey } + +func (r *Runtime) FetchCluster() *cmv1.Cluster { + if r.Cluster != nil { + return r.Cluster + } + + // We don't want to lazy init the OCM client since it requires cleanup + if r.OCMClient == nil { + r.Reporter.Errorf("Tried to fetch a cluster without initializing the OCM client, exiting.") + os.Exit(1) + } + if r.ClusterKey == "" { + r.GetClusterKey() + } + if r.Creator == nil { + r.WithAWS() + } + + r.Reporter.Debugf("Loading cluster '%s'", r.ClusterKey) + cluster, err := r.OCMClient.GetCluster(r.ClusterKey, r.Creator) + if err != nil { + r.Reporter.Errorf("Failed to get cluster '%s': %v", r.ClusterKey, err) + os.Exit(1) + } + r.Cluster = cluster + return cluster +} From bd48305f1520e9cad58382d7b168f5a38435f42d Mon Sep 17 00:00:00 2001 From: Tomer Brisker Date: Tue, 28 Jun 2022 14:52:52 +0300 Subject: [PATCH 4/4] Migrate commands to fetch cluster using runtime --- cmd/create/admin/cmd.go | 10 ++-------- cmd/create/idp/cmd.go | 10 ++-------- cmd/create/ingress/cmd.go | 9 +-------- cmd/create/machinepool/cmd.go | 10 ++-------- cmd/create/oidcprovider/cmd.go | 9 +-------- cmd/create/operatorroles/cmd.go | 9 +-------- cmd/describe/admin/cmd.go | 9 +-------- cmd/describe/cluster/cmd.go | 9 +-------- cmd/describe/installation/cmd.go | 12 ++++++------ cmd/dlt/admin/cmd.go | 9 +-------- cmd/dlt/idp/cmd.go | 9 +-------- cmd/dlt/ingress/cmd.go | 9 +-------- cmd/dlt/machinepool/cmd.go | 9 +-------- cmd/dlt/upgrade/cmd.go | 9 +-------- cmd/edit/addon/cmd.go | 9 +-------- cmd/edit/cluster/cmd.go | 7 +------ cmd/edit/ingress/cmd.go | 9 +-------- cmd/edit/machinepool/cmd.go | 9 ++------- cmd/grant/user/cmd.go | 9 +-------- cmd/hibernate/cluster/cmd.go | 9 ++------- cmd/install/addon/cmd.go | 9 +-------- cmd/list/addon/cmd.go | 9 +-------- cmd/list/gates/cmd.go | 9 +-------- cmd/list/idp/cmd.go | 9 +-------- cmd/list/ingress/cmd.go | 9 +-------- cmd/list/machinepool/cmd.go | 9 +-------- cmd/list/upgrade/cmd.go | 9 +-------- cmd/list/user/cmd.go | 10 ++-------- cmd/logs/install/cmd.go | 9 +-------- cmd/logs/uninstall/cmd.go | 9 +-------- cmd/resume/cluster/cmd.go | 9 ++------- cmd/revoke/user/cmd.go | 9 +-------- cmd/uninstall/addon/cmd.go | 11 ++--------- cmd/upgrade/cluster/cmd.go | 9 +-------- cmd/upgrade/operatorroles/cmd.go | 9 +-------- 35 files changed, 48 insertions(+), 274 deletions(-) diff --git a/cmd/create/admin/cmd.go b/cmd/create/admin/cmd.go index f97594c9a4..a721e91e2e 100644 --- a/cmd/create/admin/cmd.go +++ b/cmd/create/admin/cmd.go @@ -64,14 +64,7 @@ func run(cmd *cobra.Command, _ []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) @@ -87,6 +80,7 @@ func run(cmd *cobra.Command, _ []string) { // No cluster admin yet: proceed to create it. var password string + var err error passwordArg := args.passwordArg if len(passwordArg) == 0 { r.Reporter.Debugf("Generating random password") diff --git a/cmd/create/idp/cmd.go b/cmd/create/idp/cmd.go index cff6b0f036..6cc1bc70c6 100644 --- a/cmd/create/idp/cmd.go +++ b/cmd/create/idp/cmd.go @@ -301,14 +301,7 @@ func run(cmd *cobra.Command, _ []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) @@ -325,6 +318,7 @@ func run(cmd *cobra.Command, _ []string) { "Any optional fields can be left empty and a default will be selected.") } + var err error if interactive.Enabled() { if idpType == "" { idpType = validIdps[0] diff --git a/cmd/create/ingress/cmd.go b/cmd/create/ingress/cmd.go index 3334e86ab8..8b4eabc108 100644 --- a/cmd/create/ingress/cmd.go +++ b/cmd/create/ingress/cmd.go @@ -100,14 +100,7 @@ func run(cmd *cobra.Command, _ []string) { os.Exit(1) } - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.AWS().PrivateLink() { r.Reporter.Errorf("Cluster '%s' is PrivateLink and does not support creating new ingresses", clusterKey) os.Exit(1) diff --git a/cmd/create/machinepool/cmd.go b/cmd/create/machinepool/cmd.go index b9b398681b..00b5301c3b 100644 --- a/cmd/create/machinepool/cmd.go +++ b/cmd/create/machinepool/cmd.go @@ -174,14 +174,7 @@ func run(cmd *cobra.Command, _ []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) @@ -205,6 +198,7 @@ func run(cmd *cobra.Command, _ []string) { os.Exit(1) } + var err error // Machine pool name: name := strings.Trim(args.name, " \t") if name == "" && !interactive.Enabled() { diff --git a/cmd/create/oidcprovider/cmd.go b/cmd/create/oidcprovider/cmd.go index b7afc4d1e9..b21eb42a1a 100644 --- a/cmd/create/oidcprovider/cmd.go +++ b/cmd/create/oidcprovider/cmd.go @@ -86,14 +86,7 @@ func run(cmd *cobra.Command, argv []string) { interactive.Enable() } - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.AWS().STS().RoleARN() == "" { r.Reporter.Errorf("Cluster '%s' is not an STS cluster.", clusterKey) os.Exit(1) diff --git a/cmd/create/operatorroles/cmd.go b/cmd/create/operatorroles/cmd.go index 2ea6f3a252..8c71f98a10 100644 --- a/cmd/create/operatorroles/cmd.go +++ b/cmd/create/operatorroles/cmd.go @@ -108,14 +108,7 @@ func run(cmd *cobra.Command, argv []string) { interactive.Enable() } - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.AWS().STS().RoleARN() == "" { r.Reporter.Errorf("Cluster '%s' is not an STS cluster.", clusterKey) os.Exit(1) diff --git a/cmd/describe/admin/cmd.go b/cmd/describe/admin/cmd.go index a61dd7908e..bb1082d5c1 100644 --- a/cmd/describe/admin/cmd.go +++ b/cmd/describe/admin/cmd.go @@ -46,14 +46,7 @@ func run(cmd *cobra.Command, _ []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) diff --git a/cmd/describe/cluster/cmd.go b/cmd/describe/cluster/cmd.go index 1248781dcd..c188116344 100644 --- a/cmd/describe/cluster/cmd.go +++ b/cmd/describe/cluster/cmd.go @@ -65,14 +65,7 @@ func run(cmd *cobra.Command, argv []string) { clusterKey = r.GetClusterKey() } - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() var str string if output.HasFlag() { err = output.Print(cluster) diff --git a/cmd/describe/installation/cmd.go b/cmd/describe/installation/cmd.go index 3b936aa81c..7a990e791a 100644 --- a/cmd/describe/installation/cmd.go +++ b/cmd/describe/installation/cmd.go @@ -23,6 +23,7 @@ import ( "github.com/spf13/cobra" cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" + "github.com/openshift/rosa/pkg/ocm" "github.com/openshift/rosa/pkg/rosa" ) @@ -69,23 +70,22 @@ func run(_ *cobra.Command, argv []string) { "Expected the cluster to be specified with the --cluster flag") os.Exit(1) } + ocm.SetClusterKey(args.clusterKey) + if args.installationKey == "" { r.Reporter.Errorf( "Expected the add-on installation to be specified with the --addon flag") os.Exit(1) } - if err := describeAddonInstallation(r, args.clusterKey, args.installationKey); err != nil { + if err := describeAddonInstallation(r, args.installationKey); err != nil { r.Reporter.Errorf("Failed to describe add-on installation: %v", err) os.Exit(1) } } -func describeAddonInstallation(r *rosa.Runtime, clusterKey string, installationKey string) error { - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - return err - } +func describeAddonInstallation(r *rosa.Runtime, installationKey string) error { + cluster := r.FetchCluster() installation, err := r.OCMClient.GetAddOnInstallation(cluster.ID(), installationKey) if err != nil { diff --git a/cmd/dlt/admin/cmd.go b/cmd/dlt/admin/cmd.go index e3d8a5facf..d0a895704c 100644 --- a/cmd/dlt/admin/cmd.go +++ b/cmd/dlt/admin/cmd.go @@ -47,14 +47,7 @@ func run(cmd *cobra.Command, _ []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) diff --git a/cmd/dlt/idp/cmd.go b/cmd/dlt/idp/cmd.go index 01e9350979..536d8f5772 100644 --- a/cmd/dlt/idp/cmd.go +++ b/cmd/dlt/idp/cmd.go @@ -59,14 +59,7 @@ func run(_ *cobra.Command, argv []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() // Try to find the identity provider: r.Reporter.Debugf("Loading identity provider '%s'", idpName) idps, err := r.OCMClient.GetIdentityProviders(cluster.ID()) diff --git a/cmd/dlt/ingress/cmd.go b/cmd/dlt/ingress/cmd.go index 968d20e299..10f009faad 100644 --- a/cmd/dlt/ingress/cmd.go +++ b/cmd/dlt/ingress/cmd.go @@ -73,14 +73,7 @@ func run(_ *cobra.Command, argv []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() // Try to find the ingress: r.Reporter.Debugf("Loading ingresses for cluster '%s'", clusterKey) ingresses, err := r.OCMClient.GetIngresses(cluster.ID()) diff --git a/cmd/dlt/machinepool/cmd.go b/cmd/dlt/machinepool/cmd.go index 2d64452326..883dd292d1 100644 --- a/cmd/dlt/machinepool/cmd.go +++ b/cmd/dlt/machinepool/cmd.go @@ -72,14 +72,7 @@ func run(_ *cobra.Command, argv []string) { os.Exit(1) } - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() // Try to find the machine pool: r.Reporter.Debugf("Loading machine pools for cluster '%s'", clusterKey) machinePools, err := r.OCMClient.GetMachinePools(cluster.ID()) diff --git a/cmd/dlt/upgrade/cmd.go b/cmd/dlt/upgrade/cmd.go index 3d1eaf8884..dcefe2daba 100644 --- a/cmd/dlt/upgrade/cmd.go +++ b/cmd/dlt/upgrade/cmd.go @@ -45,14 +45,7 @@ func run(cmd *cobra.Command, _ []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) diff --git a/cmd/edit/addon/cmd.go b/cmd/edit/addon/cmd.go index fa7db1fc5e..c1588b8bc9 100644 --- a/cmd/edit/addon/cmd.go +++ b/cmd/edit/addon/cmd.go @@ -70,14 +70,7 @@ func run(cmd *cobra.Command, argv []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) diff --git a/cmd/edit/cluster/cmd.go b/cmd/edit/cluster/cmd.go index e1c6bc31c1..593f217d72 100644 --- a/cmd/edit/cluster/cmd.go +++ b/cmd/edit/cluster/cmd.go @@ -148,12 +148,7 @@ func run(cmd *cobra.Command, _ []string) { } } - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } + cluster := r.FetchCluster() // Validate flags: expiration, err := validateExpiration() diff --git a/cmd/edit/ingress/cmd.go b/cmd/edit/ingress/cmd.go index 2a7773e4cf..8aa0cdc7af 100644 --- a/cmd/edit/ingress/cmd.go +++ b/cmd/edit/ingress/cmd.go @@ -136,14 +136,7 @@ func run(cmd *cobra.Command, argv []string) { private = &privArg } - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.AWS().PrivateLink() { r.Reporter.Errorf("Cluster '%s' is PrivateLink and does not support updating ingresses", clusterKey) os.Exit(1) diff --git a/cmd/edit/machinepool/cmd.go b/cmd/edit/machinepool/cmd.go index 63e15652dc..b201993673 100644 --- a/cmd/edit/machinepool/cmd.go +++ b/cmd/edit/machinepool/cmd.go @@ -126,14 +126,9 @@ func run(cmd *cobra.Command, argv []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } + cluster := r.FetchCluster() + var err error // Editing the default machine pool is a different process if machinePoolID == "Default" { if cmd.Flags().Changed("labels") { diff --git a/cmd/grant/user/cmd.go b/cmd/grant/user/cmd.go index c30317e460..27867e0564 100644 --- a/cmd/grant/user/cmd.go +++ b/cmd/grant/user/cmd.go @@ -109,14 +109,7 @@ func run(_ *cobra.Command, argv []string) { os.Exit(1) } - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) diff --git a/cmd/hibernate/cluster/cmd.go b/cmd/hibernate/cluster/cmd.go index 356d9ff223..ab9713907f 100644 --- a/cmd/hibernate/cluster/cmd.go +++ b/cmd/hibernate/cluster/cmd.go @@ -46,12 +46,7 @@ func run(cmd *cobra.Command, _ []string) { clusterKey := r.GetClusterKey() - // Get the cluster to check the state - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Hibernating a cluster is only supported for 'Ready' clusters."+ @@ -64,7 +59,7 @@ func run(cmd *cobra.Command, _ []string) { os.Exit(1) } - err = r.OCMClient.HibernateCluster(cluster.ID()) + err := r.OCMClient.HibernateCluster(cluster.ID()) if err != nil { r.Reporter.Errorf("Failed to update cluster: %v", err) os.Exit(1) diff --git a/cmd/install/addon/cmd.go b/cmd/install/addon/cmd.go index e916295a36..52b232bfe8 100644 --- a/cmd/install/addon/cmd.go +++ b/cmd/install/addon/cmd.go @@ -74,14 +74,7 @@ func run(cmd *cobra.Command, argv []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) diff --git a/cmd/list/addon/cmd.go b/cmd/list/addon/cmd.go index 9f996de05c..52990e446d 100644 --- a/cmd/list/addon/cmd.go +++ b/cmd/list/addon/cmd.go @@ -97,14 +97,7 @@ func run(_ *cobra.Command, _ []string) { os.Exit(0) } - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) diff --git a/cmd/list/gates/cmd.go b/cmd/list/gates/cmd.go index f6174cadf9..b8478c57ba 100644 --- a/cmd/list/gates/cmd.go +++ b/cmd/list/gates/cmd.go @@ -111,14 +111,7 @@ func run(cmd *cobra.Command, _ []string) { ocm.SetClusterKey(args.clusterKey) clusterKey := r.GetClusterKey() - - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } + cluster := r.FetchCluster() if cluster.State() != v1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) diff --git a/cmd/list/idp/cmd.go b/cmd/list/idp/cmd.go index bec9d33f1c..4b3b45d814 100644 --- a/cmd/list/idp/cmd.go +++ b/cmd/list/idp/cmd.go @@ -51,14 +51,7 @@ func run(_ *cobra.Command, _ []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) diff --git a/cmd/list/ingress/cmd.go b/cmd/list/ingress/cmd.go index 3ea12b1c10..0ad1074397 100644 --- a/cmd/list/ingress/cmd.go +++ b/cmd/list/ingress/cmd.go @@ -51,14 +51,7 @@ func run(_ *cobra.Command, _ []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) diff --git a/cmd/list/machinepool/cmd.go b/cmd/list/machinepool/cmd.go index eb1b1188aa..0d1b0576e9 100644 --- a/cmd/list/machinepool/cmd.go +++ b/cmd/list/machinepool/cmd.go @@ -51,14 +51,7 @@ func run(_ *cobra.Command, _ []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) diff --git a/cmd/list/upgrade/cmd.go b/cmd/list/upgrade/cmd.go index b27c0a3078..d349db581a 100644 --- a/cmd/list/upgrade/cmd.go +++ b/cmd/list/upgrade/cmd.go @@ -48,14 +48,7 @@ func run(_ *cobra.Command, _ []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) diff --git a/cmd/list/user/cmd.go b/cmd/list/user/cmd.go index c5fecdc47a..b74b413ec9 100644 --- a/cmd/list/user/cmd.go +++ b/cmd/list/user/cmd.go @@ -49,20 +49,14 @@ func run(_ *cobra.Command, _ []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) } var clusterAdmins []*cmv1.User + var err error r.Reporter.Debugf("Loading users for cluster '%s'", clusterKey) // Load cluster-admins for this cluster clusterAdmins, err = r.OCMClient.GetUsers(cluster.ID(), "cluster-admins") diff --git a/cmd/logs/install/cmd.go b/cmd/logs/install/cmd.go index 7b07d68af3..66ebf6b150 100644 --- a/cmd/logs/install/cmd.go +++ b/cmd/logs/install/cmd.go @@ -88,14 +88,7 @@ func run(cmd *cobra.Command, argv []string) { clusterKey = r.GetClusterKey() } - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() == cmv1.ClusterStateReady { r.Reporter.Infof("Cluster '%s' has been successfully installed", clusterKey) os.Exit(0) diff --git a/cmd/logs/uninstall/cmd.go b/cmd/logs/uninstall/cmd.go index 6cc0740f67..f345ff5ebc 100644 --- a/cmd/logs/uninstall/cmd.go +++ b/cmd/logs/uninstall/cmd.go @@ -88,14 +88,7 @@ func run(cmd *cobra.Command, argv []string) { clusterKey = r.GetClusterKey() } - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateUninstalling && !watch { r.Reporter.Warnf("Cluster '%s' is not currently uninstalling", clusterKey) os.Exit(1) diff --git a/cmd/resume/cluster/cmd.go b/cmd/resume/cluster/cmd.go index eb7a406a72..a9b85278d6 100644 --- a/cmd/resume/cluster/cmd.go +++ b/cmd/resume/cluster/cmd.go @@ -45,12 +45,7 @@ func run(cmd *cobra.Command, _ []string) { defer r.Cleanup() clusterKey := r.GetClusterKey() - // Get the cluster to check the state - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateHibernating { r.Reporter.Errorf("Resuming a cluster from hibernation is only supported for clusters in "+ @@ -61,7 +56,7 @@ func run(cmd *cobra.Command, _ []string) { if !confirm.Confirm("resume cluster %s", clusterKey) { os.Exit(1) } - err = r.OCMClient.ResumeCluster(cluster.ID()) + err := r.OCMClient.ResumeCluster(cluster.ID()) if err != nil { r.Reporter.Errorf("Failed to update cluster: %v", err) os.Exit(1) diff --git a/cmd/revoke/user/cmd.go b/cmd/revoke/user/cmd.go index dc1a37cdea..3699ec1b05 100644 --- a/cmd/revoke/user/cmd.go +++ b/cmd/revoke/user/cmd.go @@ -109,14 +109,7 @@ func run(_ *cobra.Command, argv []string) { os.Exit(1) } - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() // Try to find the user: r.Reporter.Debugf("Loading '%s' users for cluster '%s'", role, clusterKey) user, err := r.OCMClient.GetUser(cluster.ID(), role, username) diff --git a/cmd/uninstall/addon/cmd.go b/cmd/uninstall/addon/cmd.go index 534fbf66c0..ed026542b8 100644 --- a/cmd/uninstall/addon/cmd.go +++ b/cmd/uninstall/addon/cmd.go @@ -58,14 +58,7 @@ func run(_ *cobra.Command, argv []string) { clusterKey := r.GetClusterKey() - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) @@ -82,7 +75,7 @@ func run(_ *cobra.Command, argv []string) { } r.Reporter.Debugf("Uninstalling add-on '%s' from cluster '%s'", addOnID, clusterKey) - err = r.OCMClient.UninstallAddOn(cluster.ID(), addOnID) + err := r.OCMClient.UninstallAddOn(cluster.ID(), addOnID) if err != nil { r.Reporter.Errorf("Failed to remove add-on installation '%s' from cluster '%s': %s", addOnID, clusterKey, err) os.Exit(1) diff --git a/cmd/upgrade/cluster/cmd.go b/cmd/upgrade/cluster/cmd.go index 25fec9dbb7..9291c26000 100644 --- a/cmd/upgrade/cluster/cmd.go +++ b/cmd/upgrade/cluster/cmd.go @@ -118,14 +118,7 @@ func run(cmd *cobra.Command, _ []string) { os.Exit(1) } - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() if cluster.State() != cmv1.ClusterStateReady { r.Reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) os.Exit(1) diff --git a/cmd/upgrade/operatorroles/cmd.go b/cmd/upgrade/operatorroles/cmd.go index 316cb0dab1..cfb5e72cec 100644 --- a/cmd/upgrade/operatorroles/cmd.go +++ b/cmd/upgrade/operatorroles/cmd.go @@ -99,14 +99,7 @@ func run(cmd *cobra.Command, argv []string) error { os.Exit(1) } - // Try to find the cluster: - r.Reporter.Debugf("Loading cluster '%s'", clusterKey) - cluster, err := r.OCMClient.GetCluster(clusterKey, r.Creator) - if err != nil { - r.Reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) - os.Exit(1) - } - + cluster := r.FetchCluster() /** we dont want to give this option to the end-user. Adding this as a support for srep if needed. */