Skip to content

Commit

Permalink
fix: use Testkube logs for telemetry (#4014)
Browse files Browse the repository at this point in the history
* fix: use Testkube logs for telemetry

* fix: golint
  • Loading branch information
vsukhin committed Jun 12, 2023
1 parent 2708834 commit ea5d667
Show file tree
Hide file tree
Showing 45 changed files with 173 additions and 62 deletions.
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/commands/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/commands/agent/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions cmd/kubectl-testkube/commands/artifacts/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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)
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/kubectl-testkube/commands/cloud/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/commands/cloud/disconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 13 additions & 7 deletions cmd/kubectl-testkube/commands/common/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"errors"
"fmt"
"os"
"strconv"
Expand All @@ -10,24 +11,27 @@ 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 flag value %w", err)
}

options := client.Options{
Namespace: namespace,
ApiUri: apiURI,
}

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 == "" {
Expand Down Expand Up @@ -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:
Expand All @@ -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
}
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/commands/common/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 5 additions & 1 deletion cmd/kubectl-testkube/commands/common/validator/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/commands/createticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/commands/debug/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 9 additions & 3 deletions cmd/kubectl-testkube/commands/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/kubectl-testkube/commands/executors/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/commands/executors/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion cmd/kubectl-testkube/commands/executors/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/commands/executors/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion cmd/kubectl-testkube/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion cmd/kubectl-testkube/commands/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/commands/telemetry/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/commands/telemetry/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion cmd/kubectl-testkube/commands/telemetry/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions cmd/kubectl-testkube/commands/tests/abort.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand All @@ -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)
},
Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/commands/tests/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/commands/tests/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down
3 changes: 2 additions & 1 deletion cmd/kubectl-testkube/commands/tests/executions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion cmd/kubectl-testkube/commands/tests/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/kubectl-testkube/commands/tests/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading

0 comments on commit ea5d667

Please sign in to comment.