From 31dc563861e0b38140dfc8300542c002c4c9aa26 Mon Sep 17 00:00:00 2001 From: Christian Weichel Date: Wed, 3 Nov 2021 14:28:27 +0000 Subject: [PATCH] [gpctl] Support multiple container for debug logs --- dev/gpctl/cmd/debug-log.go | 62 +++++++++++++++++++++----------- dev/gpctl/pkg/util/kubernetes.go | 20 +++++++++-- 2 files changed, 58 insertions(+), 24 deletions(-) diff --git a/dev/gpctl/cmd/debug-log.go b/dev/gpctl/cmd/debug-log.go index c635affa991a95..29e13edcd22d21 100644 --- a/dev/gpctl/cmd/debug-log.go +++ b/dev/gpctl/cmd/debug-log.go @@ -9,18 +9,21 @@ import ( "context" "fmt" "net/http" + "os" "github.com/gitpod-io/gitpod/common-go/log" "github.com/gitpod-io/gitpod/gpctl/pkg/util" "github.com/spf13/cobra" "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" ) // debugLogCmd represents the debugLogCmd command var debugLogCmd = &cobra.Command{ - Use: "log ", - Short: "Enables the debug log level for a component", - Args: cobra.ExactArgs(1), + Use: "log ", + Short: "Enables the debug log level for a component", + Args: cobra.ExactArgs(1), + Aliases: []string{"logs"}, Run: func(cmd *cobra.Command, args []string) { comp := args[0] if comp == "supervisor" { @@ -37,31 +40,25 @@ var debugLogCmd = &cobra.Command{ return err } - freePort, err := GetFreePort() + pods, err := util.FindPodsForComponent(clientSet, namespace, comp) if err != nil { return err } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + var failed bool + for _, podName := range pods { + err := enableLogDebug(podName, cfg, namespace) + if err != nil { + log.WithError(err).Error("cannot enable debug logging") + failed = true + continue + } - port := fmt.Sprintf("%d:6060", freePort) - podName, err := util.FindAnyPodForComponent(clientSet, namespace, comp) - if err != nil { - return err - } - readychan, errchan := util.ForwardPort(ctx, cfg, namespace, podName, port) - select { - case <-readychan: - case err := <-errchan: - return err - case <-ctx.Done(): - return ctx.Err() + fmt.Println(podName) } - _, err = http.Post(fmt.Sprintf("http://localhost:%d/debug/logging", freePort), "", bytes.NewReader([]byte(`{"level":"debug"}`))) - if err != nil { - return err + if failed { + os.Exit(1) } return nil @@ -73,6 +70,29 @@ var debugLogCmd = &cobra.Command{ }, } +func enableLogDebug(podName string, cfg *rest.Config, namespace string) error { + freePort, err := GetFreePort() + if err != nil { + return err + } + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + port := fmt.Sprintf("%d:6060", freePort) + readychan, errchan := util.ForwardPort(ctx, cfg, namespace, podName, port) + select { + case <-readychan: + case err := <-errchan: + return err + case <-ctx.Done(): + return ctx.Err() + } + + _, err = http.Post(fmt.Sprintf("http://localhost:%d/debug/logging", freePort), "application/json", bytes.NewReader([]byte(`{"level":"debug"}`))) + return err +} + func init() { debugCmd.AddCommand(debugLogCmd) } diff --git a/dev/gpctl/pkg/util/kubernetes.go b/dev/gpctl/pkg/util/kubernetes.go index 64e1762bdbf84a..527ba8f4c70c1e 100644 --- a/dev/gpctl/pkg/util/kubernetes.go +++ b/dev/gpctl/pkg/util/kubernetes.go @@ -48,16 +48,30 @@ func GetKubeconfig(kubeconfig string) (res *rest.Config, namespace string, err e // FindAnyPodForComponent returns the first pod we found for a particular component func FindAnyPodForComponent(clientSet kubernetes.Interface, namespace, label string) (podName string, err error) { + ps, err := FindPodsForComponent(clientSet, namespace, label) + if err != nil { + return "", err + } + return ps[0], nil +} + +// FindPodsForcomponent returns all pods we found for a particular component +func FindPodsForComponent(clientSet kubernetes.Interface, namespace, label string) ([]string, error) { pods, err := clientSet.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{ LabelSelector: fmt.Sprintf("component=%s", label), }) if err != nil { - return "", err + return nil, err } if len(pods.Items) == 0 { - return "", xerrors.Errorf("no pod in %s with label component=%s", namespace, label) + return nil, xerrors.Errorf("no pod in %s with label component=%s", namespace, label) + } + + res := make([]string, len(pods.Items)) + for i, p := range pods.Items { + res[i] = p.Name } - return pods.Items[0].Name, nil + return res, nil } // ForwardPort establishes a TCP port forwarding to a Kubernetes pod