blob: bfa787161a15371e6b998270c0e6704c4cab8371 [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
81 case "condition":
82 conditionCmd.Parse(os.Args[2:])
83 condition := conditionCmd.Arg(0)
Serge Bazanskiefdb6e92020-07-13 17:19:27 +020084 res, err := debugClient.GetCondition(ctx, &apb.GetConditionRequest{Name: condition})
Lorenz Brun878f5f92020-05-12 16:15:39 +020085 if err != nil {
86 fmt.Fprintf(os.Stderr, "Failed to get condition: %v\n", err)
87 os.Exit(1)
88 }
89 fmt.Println(res.Ok)
90 case "kubectl":
91 // Always get a kubeconfig with cluster-admin (group system:masters), kubectl itself can impersonate
92 kubeconfigFile, err := ioutil.TempFile("", "dbg_kubeconfig")
93 if err != nil {
94 fmt.Fprintf(os.Stderr, "Failed to create kubeconfig temp file: %v\n", err)
95 os.Exit(1)
96 }
97 defer kubeconfigFile.Close()
98 defer os.Remove(kubeconfigFile.Name())
99
Serge Bazanskiefdb6e92020-07-13 17:19:27 +0200100 res, err := debugClient.GetDebugKubeconfig(ctx, &apb.GetDebugKubeconfigRequest{Id: "debug-user", Groups: []string{"system:masters"}})
Lorenz Brun878f5f92020-05-12 16:15:39 +0200101 if err != nil {
102 fmt.Fprintf(os.Stderr, "Failed to get kubeconfig: %v\n", err)
103 os.Exit(1)
104 }
105 if _, err := kubeconfigFile.WriteString(res.DebugKubeconfig); err != nil {
106 fmt.Fprintf(os.Stderr, "Failed to write kubeconfig: %v\n", err)
107 os.Exit(1)
108 }
109
110 // This magic sets up everything as if this was just the kubectl binary. It sets the KUBECONFIG environment
111 // variable so that it knows where the Kubeconfig is located and forcibly overwrites the arguments so that
112 // the "wrapper" arguments are not visible to its flags parser. The base code is straight from
113 // https://github.com/kubernetes/kubernetes/blob/master/cmd/kubectl/kubectl.go
114 os.Setenv("KUBECONFIG", kubeconfigFile.Name())
115 rand.Seed(time.Now().UnixNano())
116 pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
117 pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
118 logs.InitLogs()
119 defer logs.FlushLogs()
120 command := cmd.NewDefaultKubectlCommandWithArgs(cmd.NewDefaultPluginHandler(plugin.ValidPluginFilenamePrefixes), os.Args[2:], os.Stdin, os.Stdout, os.Stderr)
121 command.SetArgs(os.Args[2:])
122 if err := command.Execute(); err != nil {
123 os.Exit(1)
124 }
125 }
126}