From e264c6ca79a75c34956c4fa011a7caa5fb5c2533 Mon Sep 17 00:00:00 2001 From: Vladislav Sukhin Date: Mon, 12 Jun 2023 14:38:55 +0300 Subject: [PATCH 1/2] fix: refactor get client method --- cmd/kubectl-testkube/commands/agent.go | 4 +++- cmd/kubectl-testkube/commands/agent/debug.go | 4 +++- .../commands/artifacts/artifacts.go | 12 ++++++++--- .../commands/cloud/connect.go | 3 ++- .../commands/cloud/disconnect.go | 4 +++- .../commands/common/client.go | 20 ++++++++++++------- .../commands/common/helper.go | 4 +++- .../commands/common/validator/version.go | 6 +++++- cmd/kubectl-testkube/commands/createticket.go | 4 +++- cmd/kubectl-testkube/commands/debug/info.go | 4 +++- cmd/kubectl-testkube/commands/download.go | 12 ++++++++--- .../commands/executors/create.go | 3 ++- .../commands/executors/delete.go | 4 +++- .../commands/executors/get.go | 3 ++- .../commands/executors/update.go | 4 +++- cmd/kubectl-testkube/commands/root.go | 6 +++++- cmd/kubectl-testkube/commands/status.go | 3 ++- .../commands/telemetry/disable.go | 4 +++- .../commands/telemetry/enable.go | 4 +++- .../commands/telemetry/status.go | 3 ++- cmd/kubectl-testkube/commands/tests/abort.go | 10 ++++++---- cmd/kubectl-testkube/commands/tests/create.go | 4 +++- cmd/kubectl-testkube/commands/tests/delete.go | 4 +++- .../commands/tests/executions.go | 3 ++- cmd/kubectl-testkube/commands/tests/get.go | 3 ++- cmd/kubectl-testkube/commands/tests/run.go | 3 ++- cmd/kubectl-testkube/commands/tests/update.go | 4 +++- cmd/kubectl-testkube/commands/tests/watch.go | 3 ++- .../commands/testsources/create.go | 3 ++- .../commands/testsources/delete.go | 4 +++- .../commands/testsources/get.go | 3 ++- .../commands/testsources/update.go | 4 +++- .../commands/testsuites/abort.go | 10 ++++++---- .../commands/testsuites/create.go | 4 +++- .../commands/testsuites/delete.go | 4 +++- .../commands/testsuites/executions.go | 6 ++++-- .../commands/testsuites/get.go | 4 +++- .../commands/testsuites/run.go | 3 ++- .../commands/testsuites/update.go | 4 +++- .../commands/testsuites/watch.go | 4 +++- cmd/kubectl-testkube/commands/version.go | 4 +++- .../commands/webhooks/create.go | 3 ++- .../commands/webhooks/delete.go | 4 +++- cmd/kubectl-testkube/commands/webhooks/get.go | 3 ++- 44 files changed, 151 insertions(+), 61 deletions(-) diff --git a/cmd/kubectl-testkube/commands/agent.go b/cmd/kubectl-testkube/commands/agent.go index 1bc516fd72e..490806240ee 100644 --- a/cmd/kubectl-testkube/commands/agent.go +++ b/cmd/kubectl-testkube/commands/agent.go @@ -13,7 +13,9 @@ func NewAgentCmd() *cobra.Command { Use: "agent", Short: "Testkube Cloud Agent related commands", Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + info, err := client.GetServerInfo() if err != nil { info.Version = info.Version + " " + err.Error() diff --git a/cmd/kubectl-testkube/commands/agent/debug.go b/cmd/kubectl-testkube/commands/agent/debug.go index 4eb39eb8991..38f5766a803 100644 --- a/cmd/kubectl-testkube/commands/agent/debug.go +++ b/cmd/kubectl-testkube/commands/agent/debug.go @@ -28,7 +28,9 @@ func NewAgentDebugCmd() *cobra.Command { common.UiPrintContext(cfg) - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + i, err := client.GetServerInfo() if err != nil { ui.Errf("Error %v", err) diff --git a/cmd/kubectl-testkube/commands/artifacts/artifacts.go b/cmd/kubectl-testkube/commands/artifacts/artifacts.go index 59df6cc9d5b..2a9a0548576 100644 --- a/cmd/kubectl-testkube/commands/artifacts/artifacts.go +++ b/cmd/kubectl-testkube/commands/artifacts/artifacts.go @@ -31,7 +31,9 @@ func NewListArtifactsCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { executionID = args[0] cmd.SilenceUsage = true - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + execution, err := client.GetExecution(executionID) var artifacts testkube.Artifacts var errArtifacts error @@ -65,7 +67,9 @@ func NewDownloadSingleArtifactsCmd() *cobra.Command { filename := args[1] destination := args[2] - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + f, err := client.DownloadFile(executionID, filename, destination) ui.ExitOnError("downloading file"+filename, err) @@ -88,7 +92,9 @@ func NewDownloadAllArtifactsCmd() *cobra.Command { Args: validator.ExecutionName, Run: func(cmd *cobra.Command, args []string) { executionID := args[0] - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + tests.DownloadArtifacts(executionID, downloadDir, format, masks, client) }, } diff --git a/cmd/kubectl-testkube/commands/cloud/connect.go b/cmd/kubectl-testkube/commands/cloud/connect.go index 7f661b190ca..f4e9819f2c5 100644 --- a/cmd/kubectl-testkube/commands/cloud/connect.go +++ b/cmd/kubectl-testkube/commands/cloud/connect.go @@ -36,7 +36,8 @@ func NewConnectCmd() *cobra.Command { // create new cloud uris opts.CloudUris = common.NewCloudUris(opts.CloudRootDomain) - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) info, err := client.GetServerInfo() firstInstall := err != nil && strings.Contains(err.Error(), "not found") diff --git a/cmd/kubectl-testkube/commands/cloud/disconnect.go b/cmd/kubectl-testkube/commands/cloud/disconnect.go index 78758227b81..67b11ea8aa8 100644 --- a/cmd/kubectl-testkube/commands/cloud/disconnect.go +++ b/cmd/kubectl-testkube/commands/cloud/disconnect.go @@ -33,7 +33,9 @@ func NewDisconnectCmd() *cobra.Command { return } - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + info, err := client.GetServerInfo() firstInstall := err != nil && strings.Contains(err.Error(), "not found") if err != nil && !firstInstall { diff --git a/cmd/kubectl-testkube/commands/common/client.go b/cmd/kubectl-testkube/commands/common/client.go index 0cc098324f6..0ddbb5f2be0 100644 --- a/cmd/kubectl-testkube/commands/common/client.go +++ b/cmd/kubectl-testkube/commands/common/client.go @@ -1,6 +1,7 @@ package common import ( + "errors" "fmt" "os" "strconv" @@ -10,16 +11,17 @@ import ( "github.com/kubeshop/testkube/cmd/kubectl-testkube/config" "github.com/kubeshop/testkube/pkg/api/v1/client" - "github.com/kubeshop/testkube/pkg/ui" ) // GetClient returns api client -func GetClient(cmd *cobra.Command) (client.Client, string) { +func GetClient(cmd *cobra.Command) (client.Client, string, error) { clientType := cmd.Flag("client").Value.String() namespace := cmd.Flag("namespace").Value.String() apiURI := cmd.Flag("api-uri").Value.String() oauthEnabled, err := strconv.ParseBool(cmd.Flag("oauth-enabled").Value.String()) - ui.ExitOnError("parsing flag value", err) + if err != nil { + return nil, "", fmt.Errorf("parsing fla value %w", err) + } options := client.Options{ Namespace: namespace, @@ -27,7 +29,9 @@ func GetClient(cmd *cobra.Command) (client.Client, string) { } cfg, err := config.Load() - ui.ExitOnError("loading config file", err) + if err != nil { + return nil, "", fmt.Errorf("loading config file %w", err) + } // set kubeconfig as default config type if cfg.ContextType == "" { @@ -61,7 +65,7 @@ func GetClient(cmd *cobra.Command) (client.Client, string) { } if options.Token == nil { - ui.ExitOnError("oauth token is empty, please configure your oauth settings first") + return nil, "", errors.New("oauth token is empty, please configure your oauth settings first") } } case config.ContextTypeCloud: @@ -74,7 +78,9 @@ func GetClient(cmd *cobra.Command) (client.Client, string) { } c, err := client.GetClient(client.ClientType(clientType), options) - ui.ExitOnError("setting up client type", err) + if err != nil { + return nil, "", fmt.Errorf("setting up client type %w", err) + } - return c, namespace + return c, namespace, nil } diff --git a/cmd/kubectl-testkube/commands/common/helper.go b/cmd/kubectl-testkube/commands/common/helper.go index d667732ae60..e5574327a8c 100644 --- a/cmd/kubectl-testkube/commands/common/helper.go +++ b/cmd/kubectl-testkube/commands/common/helper.go @@ -215,7 +215,9 @@ func KubectlScaleDeployment(namespace, deployment string, replicas int) (string, } func RunMigrations(cmd *cobra.Command) (hasMigrations bool, err error) { - client, _ := GetClient(cmd) + client, _, err := GetClient(cmd) + ui.ExitOnError("getting client", err) + info, err := client.GetServerInfo() ui.ExitOnError("getting server info", err) diff --git a/cmd/kubectl-testkube/commands/common/validator/version.go b/cmd/kubectl-testkube/commands/common/validator/version.go index 05ae5f8395b..12adeba6af3 100644 --- a/cmd/kubectl-testkube/commands/common/validator/version.go +++ b/cmd/kubectl-testkube/commands/common/validator/version.go @@ -17,7 +17,11 @@ var ErrOldClientVersion = fmt.Errorf("client version is older than api version, func PersistentPreRunVersionCheck(cmd *cobra.Command, clientVersion string) { // version validation // if client version is less than server version show warning - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + if err != nil { + return + } + info, err := client.GetServerInfo() if err != nil { // omit check of versions if we can't get server info diff --git a/cmd/kubectl-testkube/commands/createticket.go b/cmd/kubectl-testkube/commands/createticket.go index b1b6e2b8e7a..58ebb99c8e9 100644 --- a/cmd/kubectl-testkube/commands/createticket.go +++ b/cmd/kubectl-testkube/commands/createticket.go @@ -15,7 +15,9 @@ func NewCreateTicketCmd() *cobra.Command { Short: "Create bug ticket", Long: "Create an issue of type bug in the Testkube repository", Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + debug, err := debuginfo.GetDebugInfo(client) ui.ExitOnError("get debug info", err) ui.ExitOnError("opening GitHub ticket", github.OpenTicket(debug)) diff --git a/cmd/kubectl-testkube/commands/debug/info.go b/cmd/kubectl-testkube/commands/debug/info.go index 4147b47dabe..df8214139af 100644 --- a/cmd/kubectl-testkube/commands/debug/info.go +++ b/cmd/kubectl-testkube/commands/debug/info.go @@ -19,7 +19,9 @@ func NewShowDebugInfoCmd() *cobra.Command { Short: "Show debug info", Long: "Get all the necessary information to debug an issue in Testkube", Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + debug, err := GetDebugInfo(client) ui.ExitOnError("get debug info", err) diff --git a/cmd/kubectl-testkube/commands/download.go b/cmd/kubectl-testkube/commands/download.go index 2b1b099d4c0..56b36ebb0ed 100644 --- a/cmd/kubectl-testkube/commands/download.go +++ b/cmd/kubectl-testkube/commands/download.go @@ -51,7 +51,9 @@ func NewListArtifactsCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { executionID = args[0] cmd.SilenceUsage = true - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + artifacts, err := client.GetExecutionArtifacts(executionID) ui.ExitOnError("getting artifacts ", err) @@ -78,7 +80,9 @@ func NewDownloadSingleArtifactsCmd() *cobra.Command { filename := args[1] destination := args[2] - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + f, err := client.DownloadFile(executionID, filename, destination) ui.ExitOnError("downloading file"+filename, err) @@ -105,7 +109,9 @@ func NewDownloadAllArtifactsCmd() *cobra.Command { Args: validator.ExecutionName, Run: func(cmd *cobra.Command, args []string) { executionID := args[0] - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + tests.DownloadArtifacts(executionID, downloadDir, format, masks, client) }, } diff --git a/cmd/kubectl-testkube/commands/executors/create.go b/cmd/kubectl-testkube/commands/executors/create.go index 5ebc6b2e77a..1dbf04372cd 100644 --- a/cmd/kubectl-testkube/commands/executors/create.go +++ b/cmd/kubectl-testkube/commands/executors/create.go @@ -35,7 +35,8 @@ func NewCreateExecutorCmd() *cobra.Command { namespace := cmd.Flag("namespace").Value.String() var client apiClient.Client if !crdOnly { - client, namespace = common.GetClient(cmd) + client, namespace, err = common.GetClient(cmd) + ui.ExitOnError("getting client", err) executor, _ := client.GetExecutor(name) if name == executor.Name { diff --git a/cmd/kubectl-testkube/commands/executors/delete.go b/cmd/kubectl-testkube/commands/executors/delete.go index 56efb7889a6..041a1e026eb 100644 --- a/cmd/kubectl-testkube/commands/executors/delete.go +++ b/cmd/kubectl-testkube/commands/executors/delete.go @@ -18,7 +18,9 @@ func NewDeleteExecutorCmd() *cobra.Command { Short: "Delete Executor", Long: `Delete Executor Resource, pass name to delete by name`, Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + if len(args) > 0 { name = args[0] err := client.DeleteExecutor(name) diff --git a/cmd/kubectl-testkube/commands/executors/get.go b/cmd/kubectl-testkube/commands/executors/get.go index 7aa396a52d9..47f3f2fd9a0 100644 --- a/cmd/kubectl-testkube/commands/executors/get.go +++ b/cmd/kubectl-testkube/commands/executors/get.go @@ -25,7 +25,8 @@ func NewGetExecutorCmd() *cobra.Command { Short: "Gets executor details", Long: `Gets executor, you can change output format`, Run: func(cmd *cobra.Command, args []string) { - client, namespace := common.GetClient(cmd) + client, namespace, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) firstEntry := true if len(args) > 0 { diff --git a/cmd/kubectl-testkube/commands/executors/update.go b/cmd/kubectl-testkube/commands/executors/update.go index 16a1655da27..bc8f08ab1c2 100644 --- a/cmd/kubectl-testkube/commands/executors/update.go +++ b/cmd/kubectl-testkube/commands/executors/update.go @@ -24,7 +24,9 @@ func UpdateExecutorCmd() *cobra.Command { ui.Failf("pass valid name (in '--name' flag)") } - client, namespace := common.GetClient(cmd) + client, namespace, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + executor, _ := client.GetExecutor(name) if name != executor.Name { ui.Failf("Executor with name '%s' not exists in namespace %s", name, namespace) diff --git a/cmd/kubectl-testkube/commands/root.go b/cmd/kubectl-testkube/commands/root.go index cdd19ba9ebb..571b2d07b19 100644 --- a/cmd/kubectl-testkube/commands/root.go +++ b/cmd/kubectl-testkube/commands/root.go @@ -66,7 +66,11 @@ var RootCmd = &cobra.Command{ clientCfg, err := config.Load() ui.WarnOnError("loading config", err) - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + if err != nil { + return + } + serverCfg, err := client.GetConfig() ui.WarnOnError("getting config", err) diff --git a/cmd/kubectl-testkube/commands/status.go b/cmd/kubectl-testkube/commands/status.go index 14e9d0040fd..3063fb4d487 100644 --- a/cmd/kubectl-testkube/commands/status.go +++ b/cmd/kubectl-testkube/commands/status.go @@ -28,7 +28,8 @@ func NewStatusCmd() *cobra.Command { ui.PrintDisabled("Telemetry on CLI", "disabled") } - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) config, err := client.GetConfig() ui.ExitOnError(" Getting API config failed", err) diff --git a/cmd/kubectl-testkube/commands/telemetry/disable.go b/cmd/kubectl-testkube/commands/telemetry/disable.go index b9c80a7c2b8..1d852af530c 100644 --- a/cmd/kubectl-testkube/commands/telemetry/disable.go +++ b/cmd/kubectl-testkube/commands/telemetry/disable.go @@ -29,7 +29,9 @@ func NewDisableTelemetryCmd() *cobra.Command { ui.PrintDisabled("Telemetry on CLI", "disabled") } - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + _, err = client.UpdateConfig(testkube.Config{EnableTelemetry: false}) if err != nil { diff --git a/cmd/kubectl-testkube/commands/telemetry/enable.go b/cmd/kubectl-testkube/commands/telemetry/enable.go index 65fce7feb16..490816a79f5 100644 --- a/cmd/kubectl-testkube/commands/telemetry/enable.go +++ b/cmd/kubectl-testkube/commands/telemetry/enable.go @@ -29,7 +29,9 @@ func NewEnableTelemetryCmd() *cobra.Command { ui.PrintEnabled("Telemetry on CLI", "enabled") } - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + _, err = client.UpdateConfig(testkube.Config{EnableTelemetry: true}) if err != nil { diff --git a/cmd/kubectl-testkube/commands/telemetry/status.go b/cmd/kubectl-testkube/commands/telemetry/status.go index d66d26f2f2d..316b9468bb7 100644 --- a/cmd/kubectl-testkube/commands/telemetry/status.go +++ b/cmd/kubectl-testkube/commands/telemetry/status.go @@ -24,7 +24,8 @@ func NewStatusTelemetryCmd() *cobra.Command { ui.PrintDisabled("Telemetry on CLI", "disabled") } - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) config, err := client.GetConfig() ui.ExitOnError(" Getting API config failed", err) diff --git a/cmd/kubectl-testkube/commands/tests/abort.go b/cmd/kubectl-testkube/commands/tests/abort.go index 0fe6aa240a3..760a9308110 100644 --- a/cmd/kubectl-testkube/commands/tests/abort.go +++ b/cmd/kubectl-testkube/commands/tests/abort.go @@ -18,9 +18,10 @@ func NewAbortExecutionCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { executionID := args[0] - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) - err := client.AbortExecution("test", executionID) + err = client.AbortExecution("test", executionID) ui.ExitOnError(fmt.Sprintf("aborting execution %s", executionID), err) ui.SuccessAndExit("Succesfully aborted test", executionID) }, @@ -35,9 +36,10 @@ func NewAbortExecutionsCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { testName := args[0] - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) - err := client.AbortExecutions(testName) + err = client.AbortExecutions(testName) ui.ExitOnError(fmt.Sprintf("aborting executions of test %s", testName), err) ui.SuccessAndExit("Succesfully aborted all executions of the test", testName) }, diff --git a/cmd/kubectl-testkube/commands/tests/create.go b/cmd/kubectl-testkube/commands/tests/create.go index 55df52277ea..12615339311 100644 --- a/cmd/kubectl-testkube/commands/tests/create.go +++ b/cmd/kubectl-testkube/commands/tests/create.go @@ -89,7 +89,9 @@ func NewCreateTestsCmd() *cobra.Command { namespace := cmd.Flag("namespace").Value.String() var client client.Client if !crdOnly { - client, namespace = common.GetClient(cmd) + client, namespace, err = common.GetClient(cmd) + ui.ExitOnError("getting client", err) + test, _ := client.GetTest(testName) if testName == test.Name { diff --git a/cmd/kubectl-testkube/commands/tests/delete.go b/cmd/kubectl-testkube/commands/tests/delete.go index c99a49fa97f..c66ee64735f 100644 --- a/cmd/kubectl-testkube/commands/tests/delete.go +++ b/cmd/kubectl-testkube/commands/tests/delete.go @@ -19,7 +19,9 @@ func NewDeleteTestsCmd() *cobra.Command { Short: "Delete Test", Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + namespace := cmd.Flag("namespace").Value.String() if deleteAll { err := client.DeleteTests("") diff --git a/cmd/kubectl-testkube/commands/tests/executions.go b/cmd/kubectl-testkube/commands/tests/executions.go index d34631e61b7..aec8fb9f80c 100644 --- a/cmd/kubectl-testkube/commands/tests/executions.go +++ b/cmd/kubectl-testkube/commands/tests/executions.go @@ -26,7 +26,8 @@ func NewGetExecutionCmd() *cobra.Command { Short: "Lists or gets test executions", Long: `Getting list of execution for given test name or recent executions if there is no test name passed`, Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) if len(args) == 1 { executionID := args[0] diff --git a/cmd/kubectl-testkube/commands/tests/get.go b/cmd/kubectl-testkube/commands/tests/get.go index c883a1b2a60..2b169035af9 100644 --- a/cmd/kubectl-testkube/commands/tests/get.go +++ b/cmd/kubectl-testkube/commands/tests/get.go @@ -27,7 +27,8 @@ func NewGetTestsCmd() *cobra.Command { Long: `Getting all available tests from given namespace - if no namespace given "testkube" namespace is used`, Run: func(cmd *cobra.Command, args []string) { namespace := cmd.Flag("namespace").Value.String() - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) var name string firstEntry := true diff --git a/cmd/kubectl-testkube/commands/tests/run.go b/cmd/kubectl-testkube/commands/tests/run.go index c044d8ee3dd..bf8be4e4250 100644 --- a/cmd/kubectl-testkube/commands/tests/run.go +++ b/cmd/kubectl-testkube/commands/tests/run.go @@ -106,7 +106,8 @@ func NewRunTestCmd() *cobra.Command { } var executions []testkube.Execution - client, namespace := common.GetClient(cmd) + client, namespace, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) options := apiv1.ExecuteTestOptions{ ExecutionVariables: variables, diff --git a/cmd/kubectl-testkube/commands/tests/update.go b/cmd/kubectl-testkube/commands/tests/update.go index 0a9d53c4804..28ad44163eb 100644 --- a/cmd/kubectl-testkube/commands/tests/update.go +++ b/cmd/kubectl-testkube/commands/tests/update.go @@ -69,7 +69,9 @@ func NewUpdateTestsCmd() *cobra.Command { ui.Failf("pass valid test name (in '--name' flag)") } - client, namespace := common.GetClient(cmd) + client, namespace, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + test, _ := client.GetTest(testName) if testName != test.Name { ui.Failf("Test with name '%s' not exists in namespace %s", testName, namespace) diff --git a/cmd/kubectl-testkube/commands/tests/watch.go b/cmd/kubectl-testkube/commands/tests/watch.go index 54d368ccbb5..8abd7e646ff 100644 --- a/cmd/kubectl-testkube/commands/tests/watch.go +++ b/cmd/kubectl-testkube/commands/tests/watch.go @@ -16,7 +16,8 @@ func NewWatchExecutionCmd() *cobra.Command { Long: `Gets test execution details, until it's in success/error state, blocks until gets complete state`, Args: validator.ExecutionName, Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) executionID := args[0] execution, err := client.GetExecution(executionID) diff --git a/cmd/kubectl-testkube/commands/testsources/create.go b/cmd/kubectl-testkube/commands/testsources/create.go index 6b4cdc9bd00..c451fddd83a 100644 --- a/cmd/kubectl-testkube/commands/testsources/create.go +++ b/cmd/kubectl-testkube/commands/testsources/create.go @@ -47,7 +47,8 @@ func NewCreateTestSourceCmd() *cobra.Command { namespace := cmd.Flag("namespace").Value.String() var client apiv1.Client if !crdOnly { - client, namespace = common.GetClient(cmd) + client, namespace, err = common.GetClient(cmd) + ui.ExitOnError("getting client", err) testsource, _ := client.GetTestSource(name) if name == testsource.Name { diff --git a/cmd/kubectl-testkube/commands/testsources/delete.go b/cmd/kubectl-testkube/commands/testsources/delete.go index 95727e7b848..1e6baa4325e 100644 --- a/cmd/kubectl-testkube/commands/testsources/delete.go +++ b/cmd/kubectl-testkube/commands/testsources/delete.go @@ -19,7 +19,9 @@ func NewDeleteTestSourceCmd() *cobra.Command { Short: "Delete test source", Long: `Delete test source, pass test source name which should be deleted`, Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + if len(args) > 0 { name = args[0] err := client.DeleteTestSource(name) diff --git a/cmd/kubectl-testkube/commands/testsources/get.go b/cmd/kubectl-testkube/commands/testsources/get.go index 659bf9cdaeb..59b7425a080 100644 --- a/cmd/kubectl-testkube/commands/testsources/get.go +++ b/cmd/kubectl-testkube/commands/testsources/get.go @@ -25,7 +25,8 @@ func NewGetTestSourceCmd() *cobra.Command { Short: "Get test source details", Long: `Get test source, you can change output format, to get single details pass name as first arg`, Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) firstEntry := true if len(args) > 0 { diff --git a/cmd/kubectl-testkube/commands/testsources/update.go b/cmd/kubectl-testkube/commands/testsources/update.go index b92dba32915..4d460ad067a 100644 --- a/cmd/kubectl-testkube/commands/testsources/update.go +++ b/cmd/kubectl-testkube/commands/testsources/update.go @@ -36,7 +36,9 @@ func UpdateTestSourceCmd() *cobra.Command { ui.Failf("pass valid name (in '--name' flag)") } - client, namespace := common.GetClient(cmd) + client, namespace, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + testSource, _ := client.GetTestSource(name) if name != testSource.Name { ui.Failf("Test source with name '%s' not exists in namespace %s", name, namespace) diff --git a/cmd/kubectl-testkube/commands/testsuites/abort.go b/cmd/kubectl-testkube/commands/testsuites/abort.go index 18a79183ae4..7a76bd6ba00 100644 --- a/cmd/kubectl-testkube/commands/testsuites/abort.go +++ b/cmd/kubectl-testkube/commands/testsuites/abort.go @@ -19,9 +19,10 @@ func NewAbortTestSuiteExecutionCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { executionID := args[0] - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) - err := client.AbortTestSuiteExecution(executionID) + err = client.AbortTestSuiteExecution(executionID) ui.ExitOnError(fmt.Sprintf("aborting testsuite execution %s", executionID), err) ui.SuccessAndExit("Succesfully aborted test suite", executionID) @@ -38,9 +39,10 @@ func NewAbortTestSuiteExecutionsCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { testSuiteName := args[0] - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) - err := client.AbortTestSuiteExecutions(testSuiteName) + err = client.AbortTestSuiteExecutions(testSuiteName) ui.ExitOnError(fmt.Sprintf("aborting testsuite executions for test suite %s", testSuiteName), err) ui.SuccessAndExit("Succesfully aborted all test suite executions", testSuiteName) diff --git a/cmd/kubectl-testkube/commands/testsuites/create.go b/cmd/kubectl-testkube/commands/testsuites/create.go index 1f1a6f16649..018ff38a907 100644 --- a/cmd/kubectl-testkube/commands/testsuites/create.go +++ b/cmd/kubectl-testkube/commands/testsuites/create.go @@ -46,7 +46,9 @@ func NewCreateTestSuitesCmd() *cobra.Command { } if !crdOnly { - client, namespace := common.GetClient(cmd) + client, namespace, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + test, _ := client.GetTestSuite(options.Name) if options.Name == test.Name { ui.Failf("TestSuite with name '%s' already exists in namespace %s", options.Name, namespace) diff --git a/cmd/kubectl-testkube/commands/testsuites/delete.go b/cmd/kubectl-testkube/commands/testsuites/delete.go index 4d21b944bc6..cd012440eed 100644 --- a/cmd/kubectl-testkube/commands/testsuites/delete.go +++ b/cmd/kubectl-testkube/commands/testsuites/delete.go @@ -19,7 +19,9 @@ func NewDeleteTestSuiteCmd() *cobra.Command { Short: "Delete test suite", Long: `Delete test suite by name`, Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + namespace := cmd.Flag("namespace").Value.String() if deleteAll { err := client.DeleteTestSuites("") diff --git a/cmd/kubectl-testkube/commands/testsuites/executions.go b/cmd/kubectl-testkube/commands/testsuites/executions.go index 4ac6b8092ff..9b4357dc284 100644 --- a/cmd/kubectl-testkube/commands/testsuites/executions.go +++ b/cmd/kubectl-testkube/commands/testsuites/executions.go @@ -25,7 +25,8 @@ func NewTestSuiteExecutionCmd() *cobra.Command { Short: "Gets TestSuite Execution details", Long: `Gets TestSuite Execution details by ID, or list if id is not passed`, Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) if len(args) > 0 { executionID := args[0] @@ -35,7 +36,8 @@ func NewTestSuiteExecutionCmd() *cobra.Command { ui.ExitOnError("rendering obj", err) uiShellTestSuiteGetCommandBlock(execution.Id) } else { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) executions, err := client.ListTestSuiteExecutions(testSuiteName, limit, strings.Join(selectors, ",")) diff --git a/cmd/kubectl-testkube/commands/testsuites/get.go b/cmd/kubectl-testkube/commands/testsuites/get.go index 883f1868ee1..bf8b0a5b490 100644 --- a/cmd/kubectl-testkube/commands/testsuites/get.go +++ b/cmd/kubectl-testkube/commands/testsuites/get.go @@ -26,7 +26,9 @@ func NewGetTestSuiteCmd() *cobra.Command { Short: "Get test suite by name", Long: `Getting test suite from given namespace - if no namespace given "testkube" namespace is used`, Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + firstEntry := true if len(args) > 0 { ui.NL() diff --git a/cmd/kubectl-testkube/commands/testsuites/run.go b/cmd/kubectl-testkube/commands/testsuites/run.go index b1d7655df26..702e62343bb 100644 --- a/cmd/kubectl-testkube/commands/testsuites/run.go +++ b/cmd/kubectl-testkube/commands/testsuites/run.go @@ -41,7 +41,8 @@ func NewRunTestSuiteCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { startTime := time.Now() - client, namespace := common.GetClient(cmd) + client, namespace, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) var executions []testkube.TestSuiteExecution diff --git a/cmd/kubectl-testkube/commands/testsuites/update.go b/cmd/kubectl-testkube/commands/testsuites/update.go index 6dc4cf187e5..7e02ce52af9 100644 --- a/cmd/kubectl-testkube/commands/testsuites/update.go +++ b/cmd/kubectl-testkube/commands/testsuites/update.go @@ -40,7 +40,9 @@ func UpdateTestSuitesCmd() *cobra.Command { ui.Failf("pass valid test suite name (in '--name' flag)") } - client, namespace := common.GetClient(cmd) + client, namespace, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + testSuite, _ := client.GetTestSuite(testSuiteName) if testSuiteName != testSuite.Name { ui.Failf("TestSuite with name '%s' not exists in namespace %s", testSuiteName, namespace) diff --git a/cmd/kubectl-testkube/commands/testsuites/watch.go b/cmd/kubectl-testkube/commands/testsuites/watch.go index 313c82d8d07..e93d73ce058 100644 --- a/cmd/kubectl-testkube/commands/testsuites/watch.go +++ b/cmd/kubectl-testkube/commands/testsuites/watch.go @@ -19,7 +19,9 @@ func NewWatchTestSuiteExecutionCmd() *cobra.Command { Args: validator.ExecutionName, Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + startTime := time.Now() executionID := args[0] diff --git a/cmd/kubectl-testkube/commands/version.go b/cmd/kubectl-testkube/commands/version.go index 1d3d769089c..1040e90e515 100644 --- a/cmd/kubectl-testkube/commands/version.go +++ b/cmd/kubectl-testkube/commands/version.go @@ -15,7 +15,9 @@ func NewVersionCmd() *cobra.Command { Short: "Shows version and build info", Long: `Shows version and build info`, Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + info, err := client.GetServerInfo() if err != nil { info.Version = info.Version + " " + err.Error() diff --git a/cmd/kubectl-testkube/commands/webhooks/create.go b/cmd/kubectl-testkube/commands/webhooks/create.go index c25ee1b1854..81d91c35756 100644 --- a/cmd/kubectl-testkube/commands/webhooks/create.go +++ b/cmd/kubectl-testkube/commands/webhooks/create.go @@ -49,7 +49,8 @@ func NewCreateWebhookCmd() *cobra.Command { var client apiv1.Client if !crdOnly { - client, namespace = common.GetClient(cmd) + client, namespace, err = common.GetClient(cmd) + ui.ExitOnError("getting client", err) webhook, _ := client.GetWebhook(name) if name == webhook.Name { diff --git a/cmd/kubectl-testkube/commands/webhooks/delete.go b/cmd/kubectl-testkube/commands/webhooks/delete.go index 1db3fb29dcf..6bd139abe79 100644 --- a/cmd/kubectl-testkube/commands/webhooks/delete.go +++ b/cmd/kubectl-testkube/commands/webhooks/delete.go @@ -20,7 +20,9 @@ func NewDeleteWebhookCmd() *cobra.Command { Short: "Delete webhook", Long: `Delete webhook, pass webhook name which should be deleted`, Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) + if len(args) > 0 { name = args[0] err := client.DeleteWebhook(name) diff --git a/cmd/kubectl-testkube/commands/webhooks/get.go b/cmd/kubectl-testkube/commands/webhooks/get.go index 206599f03d9..1ea05b02f56 100644 --- a/cmd/kubectl-testkube/commands/webhooks/get.go +++ b/cmd/kubectl-testkube/commands/webhooks/get.go @@ -24,7 +24,8 @@ func NewGetWebhookCmd() *cobra.Command { Short: "Get webhook details", Long: `Get webhook, you can change output format, to get single details pass name as first arg`, Run: func(cmd *cobra.Command, args []string) { - client, _ := common.GetClient(cmd) + client, _, err := common.GetClient(cmd) + ui.ExitOnError("getting client", err) firstEntry := true if len(args) > 0 { From 4f480dc224bdee331c01c26ebedf0a5b402031d1 Mon Sep 17 00:00:00 2001 From: Vladislav Sukhin Date: Mon, 12 Jun 2023 14:42:33 +0300 Subject: [PATCH 2/2] fix: error message --- cmd/kubectl-testkube/commands/common/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/kubectl-testkube/commands/common/client.go b/cmd/kubectl-testkube/commands/common/client.go index 0ddbb5f2be0..a9878c333f4 100644 --- a/cmd/kubectl-testkube/commands/common/client.go +++ b/cmd/kubectl-testkube/commands/common/client.go @@ -20,7 +20,7 @@ func GetClient(cmd *cobra.Command) (client.Client, string, error) { apiURI := cmd.Flag("api-uri").Value.String() oauthEnabled, err := strconv.ParseBool(cmd.Flag("oauth-enabled").Value.String()) if err != nil { - return nil, "", fmt.Errorf("parsing fla value %w", err) + return nil, "", fmt.Errorf("parsing flag value %w", err) } options := client.Options{