blob: e0bde55059880b6bb36dd3bfa285dae1867cb2f7 [file] [log] [blame]
Lorenz Brun878f5f92020-05-12 16:15:39 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package main
18
19import (
20 "context"
21 "flag"
22 "fmt"
23 "io/ioutil"
24 "math/rand"
25 "os"
26 "strings"
27 "time"
28
29 "github.com/spf13/pflag"
30 "google.golang.org/grpc"
31 cliflag "k8s.io/component-base/cli/flag"
32 "k8s.io/kubectl/pkg/cmd/plugin"
33 "k8s.io/kubectl/pkg/util/logs"
34 "k8s.io/kubernetes/pkg/kubectl/cmd"
35
Serge Bazanskiefdb6e92020-07-13 17:19:27 +020036 apb "git.monogon.dev/source/nexantic.git/core/proto/api"
Lorenz Brun878f5f92020-05-12 16:15:39 +020037)
38
39func main() {
Serge Bazanskiefdb6e92020-07-13 17:19:27 +020040 ctx := context.Background()
Lorenz Brun878f5f92020-05-12 16:15:39 +020041 // Hardcode localhost since this should never be used to interface with a production node because of missing
42 // encryption & authentication
43 grpcClient, err := grpc.Dial("localhost:7837", grpc.WithInsecure())
44 if err != nil {
45 fmt.Printf("Failed to dial debug service (is it running): %v\n", err)
46 }
Serge Bazanskiefdb6e92020-07-13 17:19:27 +020047 debugClient := apb.NewNodeDebugServiceClient(grpcClient)
Lorenz Brun878f5f92020-05-12 16:15:39 +020048 if len(os.Args) < 2 {
49 fmt.Println("Please specify a subcommand")
50 os.Exit(1)
51 }
52
53 logsCmd := flag.NewFlagSet("logs", flag.ExitOnError)
54 logsTailN := logsCmd.Uint("tail", 0, "Get last n lines (0 = whole buffer)")
55 logsCmd.Usage = func() {
56 fmt.Fprintf(os.Stderr, "Usage: %s %s [options] component_path\n", os.Args[0], os.Args[1])
57 flag.PrintDefaults()
58
59 fmt.Fprintf(os.Stderr, "Example:\n %s %s --tail 5 kube.apiserver\n", os.Args[0], os.Args[1])
60 }
61 conditionCmd := flag.NewFlagSet("condition", flag.ExitOnError)
62 conditionCmd.Usage = func() {
63 fmt.Fprintf(os.Stderr, "Usage: %s %s [options] component_path\n", os.Args[0], os.Args[1])
64 flag.PrintDefaults()
65
66 fmt.Fprintf(os.Stderr, "Example:\n %s %s IPAssigned\n", os.Args[0], os.Args[1])
67 }
68 switch os.Args[1] {
69 case "logs":
70 logsCmd.Parse(os.Args[2:])
71 componentPath := strings.Split(logsCmd.Arg(0), ".")
Serge Bazanskiefdb6e92020-07-13 17:19:27 +020072 res, err := debugClient.GetComponentLogs(ctx, &apb.GetComponentLogsRequest{ComponentPath: componentPath, TailLines: uint32(*logsTailN)})
Lorenz Brun878f5f92020-05-12 16:15:39 +020073 if err != nil {
74 fmt.Fprintf(os.Stderr, "Failed to get logs: %v\n", err)
75 os.Exit(1)
76 }
77 for _, line := range res.Line {
78 fmt.Println(line)
79 }
80 return
Lorenz Brun878f5f92020-05-12 16:15:39 +020081 case "kubectl":
82 // Always get a kubeconfig with cluster-admin (group system:masters), kubectl itself can impersonate
83 kubeconfigFile, err := ioutil.TempFile("", "dbg_kubeconfig")
84 if err != nil {
85 fmt.Fprintf(os.Stderr, "Failed to create kubeconfig temp file: %v\n", err)
86 os.Exit(1)
87 }
88 defer kubeconfigFile.Close()
89 defer os.Remove(kubeconfigFile.Name())
90
Serge Bazanskiefdb6e92020-07-13 17:19:27 +020091 res, err := debugClient.GetDebugKubeconfig(ctx, &apb.GetDebugKubeconfigRequest{Id: "debug-user", Groups: []string{"system:masters"}})
Lorenz Brun878f5f92020-05-12 16:15:39 +020092 if err != nil {
93 fmt.Fprintf(os.Stderr, "Failed to get kubeconfig: %v\n", err)
94 os.Exit(1)
95 }
96 if _, err := kubeconfigFile.WriteString(res.DebugKubeconfig); err != nil {
97 fmt.Fprintf(os.Stderr, "Failed to write kubeconfig: %v\n", err)
98 os.Exit(1)
99 }
100
101 // This magic sets up everything as if this was just the kubectl binary. It sets the KUBECONFIG environment
102 // variable so that it knows where the Kubeconfig is located and forcibly overwrites the arguments so that
103 // the "wrapper" arguments are not visible to its flags parser. The base code is straight from
104 // https://github.com/kubernetes/kubernetes/blob/master/cmd/kubectl/kubectl.go
105 os.Setenv("KUBECONFIG", kubeconfigFile.Name())
106 rand.Seed(time.Now().UnixNano())
107 pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
108 pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
109 logs.InitLogs()
110 defer logs.FlushLogs()
111 command := cmd.NewDefaultKubectlCommandWithArgs(cmd.NewDefaultPluginHandler(plugin.ValidPluginFilenamePrefixes), os.Args[2:], os.Stdin, os.Stdout, os.Stderr)
112 command.SetArgs(os.Args[2:])
113 if err := command.Execute(); err != nil {
114 os.Exit(1)
115 }
116 }
117}