Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gpctl] Support multiple container for debug logs #6542

Merged
merged 1 commit into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions dev/gpctl/cmd/debug-log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <component>",
Short: "Enables the debug log level for a component",
Args: cobra.ExactArgs(1),
Use: "log <component>",
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" {
Expand All @@ -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
Expand All @@ -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)
}
20 changes: 17 additions & 3 deletions dev/gpctl/pkg/util/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down