-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
cmd_loglevel.go
95 lines (84 loc) · 2.87 KB
/
cmd_loglevel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package cli
import (
"context"
"fmt"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/durationpb"
"github.com/telepresenceio/telepresence/rpc/v2/connector"
"github.com/telepresenceio/telepresence/rpc/v2/daemon"
"github.com/telepresenceio/telepresence/rpc/v2/manager"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/cliutil"
"github.com/telepresenceio/telepresence/v2/pkg/client/errcat"
)
const defaultDuration = 30 * time.Minute
type logLevelSetter struct {
duration time.Duration
localOnly bool
remoteOnly bool
}
func logLevelArg(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errcat.User.New("accepts exactly one argument (the log level)")
}
lvl, err := logrus.ParseLevel(args[0])
if err != nil {
return errcat.User.New(err)
}
switch lvl {
case logrus.PanicLevel, logrus.FatalLevel:
return errcat.User.Newf("unsupported log level: %s", lvl)
}
return nil
}
func loglevelCommand() *cobra.Command {
lvs := logrus.AllLevels[2:] // Don't include `panic` and `fatal`
lvStrs := make([]string, len(lvs))
for i, lv := range lvs {
lvStrs[i] = lv.String()
}
lls := logLevelSetter{}
cmd := &cobra.Command{
Use: fmt.Sprintf("loglevel <%s>", strings.Join(lvStrs, ",")),
Args: logLevelArg,
Short: "Temporarily change the log-level of the traffic-manager, traffic-agent, and user and root daemons",
RunE: lls.setTempLogLevel,
ValidArgs: lvStrs,
}
flags := cmd.Flags()
flags.DurationVarP(&lls.duration, "duration", "d", defaultDuration, "The time that the log-level will be in effect (0s means indefinitely)")
flags.BoolVarP(&lls.localOnly, "local-only", "l", false, "Only affect the user and root daemons")
flags.BoolVarP(&lls.remoteOnly, "remote-only", "r", false, "Only affect the traffic-manager and traffic-agents")
return cmd
}
func (lls *logLevelSetter) setTempLogLevel(cmd *cobra.Command, args []string) error {
if lls.localOnly && lls.remoteOnly {
return errcat.User.New("the local-only and remote-only options are mutually exclusive")
}
return withConnector(cmd, true, func(ctx context.Context, connectorClient connector.ConnectorClient, _ *connector.ConnectInfo, _ daemon.DaemonClient) error {
rq := &manager.LogLevelRequest{LogLevel: args[0], Duration: durationpb.New(lls.duration)}
if !lls.remoteOnly {
_, err := connectorClient.SetLogLevel(ctx, rq)
if err != nil {
return err
}
err = cliutil.WithStartedDaemon(ctx, func(ctx context.Context, daemonClient daemon.DaemonClient) error {
_, err := daemonClient.SetLogLevel(ctx, rq)
return err
})
if err != nil {
return err
}
}
if !lls.localOnly {
err := cliutil.WithManager(ctx, func(ctx context.Context, managerClient manager.ManagerClient) error {
_, err := managerClient.SetLogLevel(ctx, rq)
return err
})
return err
}
return nil
})
}