Skip to content

Commit

Permalink
feat: improved debug cmd (#5617)
Browse files Browse the repository at this point in the history
* feat: improved debug cmd

* feat: added possibility to move around different debug sets

* chore: rename features to show

* fix: unused code cleanup

* fix: proper error message instead of exit 1
  • Loading branch information
exu committed Jun 28, 2024
1 parent 3f0f41f commit 3ff1c4f
Show file tree
Hide file tree
Showing 13 changed files with 481 additions and 224 deletions.
45 changes: 3 additions & 42 deletions cmd/kubectl-testkube/commands/agent/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,13 @@ package agent

import (
"github.com/spf13/cobra"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/config"
"github.com/kubeshop/testkube/pkg/ui"
)

func NewDebugAgentCmd() *cobra.Command {

cmd := &cobra.Command{
Use: "debug",
Short: "Debug Agent info",
Run: func(cmd *cobra.Command, args []string) {
cfg, err := config.Load()
ui.ExitOnError("loading config file", err)
ui.NL()

if cfg.ContextType != config.ContextTypeCloud {
ui.Errf("Agent debug is only available for cloud context")
ui.NL()
ui.ShellCommand("Please try command below to set your context into Cloud mode", `testkube set context -o <org> -e <env> -k <api-key> `)
ui.NL()
return
}

common.UiPrintContext(cfg)

client, _, err := common.GetClient(cmd)
ui.ExitOnError("getting client", err)

i, err := client.GetServerInfo()
if err != nil {
ui.Errf("Error %v", err)
ui.NL()
ui.Info("Possible reasons:")
ui.Warn("- Please check if your agent organization and environment are set correctly")
ui.Warn("- Please check if your API token is set correctly")
ui.NL()
} else {
ui.Warn("Agent correctly connected to cloud:\n")
ui.InfoGrid(map[string]string{
"Agent version ": i.Version,
"Agent namespace": i.Namespace,
})
}
ui.NL()
},
Use: "debug",
Short: "Debug Agent info",
Deprecated: "use `testkube debug agent` instead",
}

return cmd
Expand Down
28 changes: 28 additions & 0 deletions cmd/kubectl-testkube/commands/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,31 @@ func ProcessMasterFlags(cmd *cobra.Command, opts *HelmOptions, cfg *config.Data)
func IsBothEnabledAndDisabledSet(cmd *cobra.Command) bool {
return cmd.Flag("enable-webhooks").Changed && cmd.Flag("disable-webhooks").Changed
}

// CommaList is a custom flag type for features
type CommaList []string

func (s CommaList) String() string {
return strings.Join(s, ",")
}
func (s *CommaList) Type() string {
return "[]string"
}

func (s *CommaList) Set(value string) error {
*s = strings.Split(value, ",")
return nil
}

// Enabled returns true if the feature is enabled, defaults to all
func (s *CommaList) Enabled(value string) bool {
if len(*s) == 0 {
return true
}
for _, f := range *s {
if f == value {
return true
}
}
return false
}
110 changes: 108 additions & 2 deletions cmd/kubectl-testkube/commands/common/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func uiGetToken(tokenChan chan cloudlogin.Tokens) (string, string, error) {
return token.IDToken, token.RefreshToken, nil
}

func KubectlPrintLogs(namespace string, labels map[string]string) error {
func KubectlLogs(namespace string, labels map[string]string) error {
kubectl, err := exec.LookPath("kubectl")
if err != nil {
return err
Expand Down Expand Up @@ -491,6 +491,24 @@ func KubectlPrintEvents(namespace string) error {
return process.ExecuteAndStreamOutput(kubectl, args...)
}

func KubectlDescribePods(namespace string) error {
kubectl, err := exec.LookPath("kubectl")
if err != nil {
return err
}

args := []string{
"describe",
"pods",
"-n", namespace,
}

ui.ShellCommand(kubectl, args...)
ui.NL()

return process.ExecuteAndStreamOutput(kubectl, args...)
}

func KubectlPrintPods(namespace string) error {
kubectl, err := exec.LookPath("kubectl")
if err != nil {
Expand All @@ -510,7 +528,7 @@ func KubectlPrintPods(namespace string) error {
return process.ExecuteAndStreamOutput(kubectl, args...)
}

func KubectlPrintStorageClass(namespace string) error {
func KubectlGetStorageClass(namespace string) error {
kubectl, err := exec.LookPath("kubectl")
if err != nil {
return err
Expand All @@ -526,3 +544,91 @@ func KubectlPrintStorageClass(namespace string) error {

return process.ExecuteAndStreamOutput(kubectl, args...)
}

func KubectlGetServices(namespace string) error {
kubectl, err := exec.LookPath("kubectl")
if err != nil {
return err
}

args := []string{
"get",
"services",
"-n", namespace,
}

ui.ShellCommand(kubectl, args...)
ui.NL()

return process.ExecuteAndStreamOutput(kubectl, args...)
}

func KubectlDescribeServices(namespace string) error {
kubectl, err := exec.LookPath("kubectl")
if err != nil {
return err
}

args := []string{
"get",
"services",
"-n", namespace,
"-o", "yaml",
}

ui.ShellCommand(kubectl, args...)
ui.NL()

return process.ExecuteAndStreamOutput(kubectl, args...)
}

func KubectlGetIngresses(namespace string) error {
kubectl, err := exec.LookPath("kubectl")
if err != nil {
return err
}

args := []string{
"get",
"ingresses",
"-n", namespace,
}

ui.ShellCommand(kubectl, args...)
ui.NL()

return process.ExecuteAndStreamOutput(kubectl, args...)
}

func KubectlDescribeIngresses(namespace string) error {
kubectl, err := exec.LookPath("kubectl")
if err != nil {
return err
}

args := []string{
"get",
"ingresses",
"-n", namespace,
"-o", "yaml",
}

ui.ShellCommand(kubectl, args...)
ui.NL()

return process.ExecuteAndStreamOutput(kubectl, args...)
}

func UiGetNamespace(cmd *cobra.Command, defaultNamespace string) string {
var namespace string
var err error

if cmd.Flag("namespace").Changed {
namespace, err = cmd.Flags().GetString("namespace")
ui.ExitOnError("getting namespace", err)
} else {
namespace = ui.TextInput("Please provide namespace for Control Plane", defaultNamespace)
}

return namespace
}
67 changes: 4 additions & 63 deletions cmd/kubectl-testkube/commands/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,19 @@ package commands
import (
"github.com/spf13/cobra"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common/validator"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/debug"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/config"
"github.com/kubeshop/testkube/pkg/ui"
)

// NewDebugCmd creates the 'testkube debug' command
func NewDebugCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "debug",
Aliases: []string{"dbg", "d"},
Short: "Print environment information for debugging",
Run: func(cmd *cobra.Command, args []string) {
Short: "Print debugging info",
}

cfg, err := config.Load()
ui.ExitOnError("loading config file", err)

if cfg.ContextType == config.ContextTypeCloud {
cfg, err := config.Load()
ui.ExitOnError("loading config file", err)
ui.NL()

if cfg.ContextType != config.ContextTypeCloud {
ui.Errf("Agent debug is only available for cloud context")
ui.NL()
ui.ShellCommand("Please try command below to set your context into Cloud mode", `testkube set context -o <org> -e <env> -k <api-key> `)
ui.NL()
return
}

common.UiPrintContext(cfg)

client, _, err := common.GetClient(cmd)
ui.ExitOnError("getting client", err)

i, err := client.GetServerInfo()
if err != nil {
ui.Errf("Error %v", err)
ui.NL()
ui.Info("Possible reasons:")
ui.Warn("- Please check if your agent organization and environment are set correctly")
ui.Warn("- Please check if your API token is set correctly")
ui.NL()
} else {
ui.Warn("Agent correctly connected to cloud:\n")
ui.InfoGrid(map[string]string{
"Agent version ": i.Version,
"Agent namespace": i.Namespace,
})
}
ui.NL()
} else {
client, _, err := common.GetClient(cmd)
ui.ExitOnError("getting client", err)

d, err := debug.GetDebugInfo(client)
ui.ExitOnError("get debug info", err)

debug.PrintDebugInfo(d)

}
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
cfg, err := config.Load()
ui.ExitOnError("loading config", err)
common.UiContextHeader(cmd, cfg)

validator.PersistentPreRunVersionCheck(cmd, common.Version)
}}

cmd.AddCommand(debug.NewShowDebugInfoCmd())
cmd.AddCommand(debug.NewDebugOssCmd())
cmd.AddCommand(debug.NewDebugAgentCmd())
cmd.AddCommand(debug.NewDebugControlPlaneCmd())

return cmd
Expand Down
Loading

0 comments on commit 3ff1c4f

Please sign in to comment.