core: plug logtree into NodeDebugService
This introduces a new Proto API for accessing debug logs. Currently this
is implemented to be used by the debug service. However, these proto
definitions will likely be reused for production cluster APIs.
The implementation mostly consists of adding the proto, implementing
to/from conversion methods, and altering the debug service to use the
new API.
We also move all of the debug service implementation into a separate file,
to slightly clean up main.go. This produces an unfortunately colorful
diff, but it's just moving code around.
Test Plan: Manually tested using the dbg tool. We currently don't properly test the debug service. I suppose we should do that for the production cluster APIs, and just keep on going for now.
X-Origin-Diff: phab/D649
GitOrigin-RevId: ac454681e4b72b2876e313b3aeababa179eb1fa3
diff --git a/core/cmd/dbg/main.go b/core/cmd/dbg/main.go
index f2d8fc0..176973f 100644
--- a/core/cmd/dbg/main.go
+++ b/core/cmd/dbg/main.go
@@ -20,12 +20,14 @@
"context"
"flag"
"fmt"
+ "io"
"io/ioutil"
"math/rand"
"os"
- "strings"
"time"
+ "git.monogon.dev/source/nexantic.git/core/pkg/logtree"
+
"github.com/spf13/pflag"
"google.golang.org/grpc"
cliflag "k8s.io/component-base/cli/flag"
@@ -51,12 +53,14 @@
}
logsCmd := flag.NewFlagSet("logs", flag.ExitOnError)
- logsTailN := logsCmd.Uint("tail", 0, "Get last n lines (0 = whole buffer)")
+ logsTailN := logsCmd.Int("tail", -1, "Get last n lines (-1 = whole buffer, 0 = disable)")
+ logsStream := logsCmd.Bool("follow", false, "Stream log entries live from the system")
+ logsRecursive := logsCmd.Bool("recursive", false, "Get entries from entire DN subtree")
logsCmd.Usage = func() {
- fmt.Fprintf(os.Stderr, "Usage: %s %s [options] component_path\n", os.Args[0], os.Args[1])
+ fmt.Fprintf(os.Stderr, "Usage: %s %s [options] dn\n", os.Args[0], os.Args[1])
flag.PrintDefaults()
- fmt.Fprintf(os.Stderr, "Example:\n %s %s --tail 5 kube.apiserver\n", os.Args[0], os.Args[1])
+ fmt.Fprintf(os.Stderr, "Example:\n %s %s --tail 5 --follow init\n", os.Args[0], os.Args[1])
}
goldenticketCmd := flag.NewFlagSet("goldenticket", flag.ExitOnError)
conditionCmd := flag.NewFlagSet("condition", flag.ExitOnError)
@@ -66,19 +70,60 @@
fmt.Fprintf(os.Stderr, "Example:\n %s %s IPAssigned\n", os.Args[0], os.Args[1])
}
+
switch os.Args[1] {
case "logs":
logsCmd.Parse(os.Args[2:])
- componentPath := strings.Split(logsCmd.Arg(0), ".")
- res, err := debugClient.GetComponentLogs(ctx, &apb.GetComponentLogsRequest{ComponentPath: componentPath, TailLines: uint32(*logsTailN)})
+ dn := logsCmd.Arg(0)
+ req := &apb.GetLogsRequest{
+ Dn: dn,
+ BacklogMode: apb.GetLogsRequest_BACKLOG_DISABLE,
+ StreamMode: apb.GetLogsRequest_STREAM_DISABLE,
+ Filters: nil,
+ }
+
+ switch *logsTailN {
+ case 0:
+ case -1:
+ req.BacklogMode = apb.GetLogsRequest_BACKLOG_ALL
+ default:
+ req.BacklogMode = apb.GetLogsRequest_BACKLOG_COUNT
+ req.BacklogCount = int64(*logsTailN)
+ }
+
+ if *logsStream {
+ req.StreamMode = apb.GetLogsRequest_STREAM_UNBUFFERED
+ }
+
+ if *logsRecursive {
+ req.Filters = append(req.Filters, &apb.LogFilter{
+ Filter: &apb.LogFilter_WithChildren_{WithChildren: &apb.LogFilter_WithChildren{}},
+ })
+ }
+
+ stream, err := debugClient.GetLogs(ctx, req)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get logs: %v\n", err)
os.Exit(1)
}
- for _, line := range res.Line {
- fmt.Println(line)
+ for {
+ res, err := stream.Recv()
+ if err != nil {
+ if err == io.EOF {
+ os.Exit(0)
+ }
+ fmt.Fprintf(os.Stderr, "Failed to stream logs: %v\n", err)
+ os.Exit(1)
+ }
+ for _, entry := range res.BacklogEntries {
+ entry, err := logtree.LogEntryFromProto(entry)
+ if err != nil {
+ fmt.Printf("error decoding entry: %v", err)
+ continue
+ }
+ fmt.Println(entry.String())
+ }
}
- return
case "goldenticket":
goldenticketCmd.Parse(os.Args[2:])
ip := goldenticketCmd.Arg(0)