Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/gpctl/cmd/debug-log.go
2498 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"bytes"
9
"context"
10
"fmt"
11
"net/http"
12
"os"
13
14
"github.com/gitpod-io/gitpod/common-go/log"
15
"github.com/gitpod-io/gitpod/gpctl/pkg/util"
16
"github.com/spf13/cobra"
17
"k8s.io/client-go/kubernetes"
18
"k8s.io/client-go/rest"
19
)
20
21
// debugLogCmd represents the debugLogCmd command
22
var debugLogCmd = &cobra.Command{
23
Use: "log <component>",
24
Short: "Enables the debug log level for a component",
25
Args: cobra.ExactArgs(1),
26
Aliases: []string{"logs"},
27
Run: func(cmd *cobra.Command, args []string) {
28
comp := args[0]
29
if comp == "supervisor" {
30
log.Fatal("cannot enable supervisor debug logging yet - please edit the default workspace template and add an env var SUPERVISOR_DEBUG=true")
31
}
32
33
err := func() error {
34
cfg, namespace, err := getKubeconfig()
35
if err != nil {
36
return err
37
}
38
clientSet, err := kubernetes.NewForConfig(cfg)
39
if err != nil {
40
return err
41
}
42
43
pods, err := util.FindPodsForComponent(clientSet, namespace, comp)
44
if err != nil {
45
return err
46
}
47
48
var failed bool
49
for _, podName := range pods {
50
err := enableLogDebug(podName, cfg, namespace)
51
if err != nil {
52
log.WithError(err).Error("cannot enable debug logging")
53
failed = true
54
continue
55
}
56
57
fmt.Println(podName)
58
}
59
60
if failed {
61
os.Exit(1)
62
}
63
64
return nil
65
}()
66
67
if err != nil {
68
log.Fatal(err)
69
}
70
},
71
}
72
73
func enableLogDebug(podName string, cfg *rest.Config, namespace string) error {
74
freePort, err := GetFreePort()
75
if err != nil {
76
return err
77
}
78
79
ctx, cancel := context.WithCancel(context.Background())
80
defer cancel()
81
82
port := fmt.Sprintf("%d:6060", freePort)
83
readychan, errchan := util.ForwardPort(ctx, cfg, namespace, podName, port)
84
select {
85
case <-readychan:
86
case err := <-errchan:
87
return err
88
case <-ctx.Done():
89
return ctx.Err()
90
}
91
92
_, err = http.Post(fmt.Sprintf("http://localhost:%d/debug/logging", freePort), "application/json", bytes.NewReader([]byte(`{"level":"debug"}`)))
93
return err
94
}
95
96
func init() {
97
debugCmd.AddCommand(debugLogCmd)
98
}
99
100