Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame^] | 1 | // 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 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 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 | |
| 36 | apipb "git.monogon.dev/source/nexantic.git/core/generated/api" |
| 37 | ) |
| 38 | |
| 39 | func main() { |
| 40 | // Hardcode localhost since this should never be used to interface with a production node because of missing |
| 41 | // encryption & authentication |
| 42 | grpcClient, err := grpc.Dial("localhost:7837", grpc.WithInsecure()) |
| 43 | if err != nil { |
| 44 | fmt.Printf("Failed to dial debug service (is it running): %v\n", err) |
| 45 | } |
| 46 | debugClient := apipb.NewNodeDebugServiceClient(grpcClient) |
| 47 | if len(os.Args) < 2 { |
| 48 | fmt.Println("Please specify a subcommand") |
| 49 | os.Exit(1) |
| 50 | } |
| 51 | |
| 52 | logsCmd := flag.NewFlagSet("logs", flag.ExitOnError) |
| 53 | logsTailN := logsCmd.Uint("tail", 0, "Get last n lines (0 = whole buffer)") |
| 54 | logsCmd.Usage = func() { |
| 55 | fmt.Fprintf(os.Stderr, "Usage: %s %s [options] component_path\n", os.Args[0], os.Args[1]) |
| 56 | flag.PrintDefaults() |
| 57 | |
| 58 | fmt.Fprintf(os.Stderr, "Example:\n %s %s --tail 5 kube.apiserver\n", os.Args[0], os.Args[1]) |
| 59 | } |
| 60 | conditionCmd := flag.NewFlagSet("condition", flag.ExitOnError) |
| 61 | conditionCmd.Usage = func() { |
| 62 | fmt.Fprintf(os.Stderr, "Usage: %s %s [options] component_path\n", os.Args[0], os.Args[1]) |
| 63 | flag.PrintDefaults() |
| 64 | |
| 65 | fmt.Fprintf(os.Stderr, "Example:\n %s %s IPAssigned\n", os.Args[0], os.Args[1]) |
| 66 | } |
| 67 | switch os.Args[1] { |
| 68 | case "logs": |
| 69 | logsCmd.Parse(os.Args[2:]) |
| 70 | componentPath := strings.Split(logsCmd.Arg(0), ".") |
| 71 | res, err := debugClient.GetComponentLogs(context.Background(), &apipb.GetComponentLogsRequest{ComponentPath: componentPath, TailLines: uint32(*logsTailN)}) |
| 72 | if err != nil { |
| 73 | fmt.Fprintf(os.Stderr, "Failed to get logs: %v\n", err) |
| 74 | os.Exit(1) |
| 75 | } |
| 76 | for _, line := range res.Line { |
| 77 | fmt.Println(line) |
| 78 | } |
| 79 | return |
| 80 | case "condition": |
| 81 | conditionCmd.Parse(os.Args[2:]) |
| 82 | condition := conditionCmd.Arg(0) |
| 83 | res, err := debugClient.GetCondition(context.Background(), &apipb.GetConditionRequest{Name: condition}) |
| 84 | if err != nil { |
| 85 | fmt.Fprintf(os.Stderr, "Failed to get condition: %v\n", err) |
| 86 | os.Exit(1) |
| 87 | } |
| 88 | fmt.Println(res.Ok) |
| 89 | case "kubectl": |
| 90 | // Always get a kubeconfig with cluster-admin (group system:masters), kubectl itself can impersonate |
| 91 | kubeconfigFile, err := ioutil.TempFile("", "dbg_kubeconfig") |
| 92 | if err != nil { |
| 93 | fmt.Fprintf(os.Stderr, "Failed to create kubeconfig temp file: %v\n", err) |
| 94 | os.Exit(1) |
| 95 | } |
| 96 | defer kubeconfigFile.Close() |
| 97 | defer os.Remove(kubeconfigFile.Name()) |
| 98 | |
| 99 | res, err := debugClient.GetDebugKubeconfig(context.Background(), &apipb.GetDebugKubeconfigRequest{Id: "debug-user", Groups: []string{"system:masters"}}) |
| 100 | if err != nil { |
| 101 | fmt.Fprintf(os.Stderr, "Failed to get kubeconfig: %v\n", err) |
| 102 | os.Exit(1) |
| 103 | } |
| 104 | if _, err := kubeconfigFile.WriteString(res.DebugKubeconfig); err != nil { |
| 105 | fmt.Fprintf(os.Stderr, "Failed to write kubeconfig: %v\n", err) |
| 106 | os.Exit(1) |
| 107 | } |
| 108 | |
| 109 | // This magic sets up everything as if this was just the kubectl binary. It sets the KUBECONFIG environment |
| 110 | // variable so that it knows where the Kubeconfig is located and forcibly overwrites the arguments so that |
| 111 | // the "wrapper" arguments are not visible to its flags parser. The base code is straight from |
| 112 | // https://github.com/kubernetes/kubernetes/blob/master/cmd/kubectl/kubectl.go |
| 113 | os.Setenv("KUBECONFIG", kubeconfigFile.Name()) |
| 114 | rand.Seed(time.Now().UnixNano()) |
| 115 | pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc) |
| 116 | pflag.CommandLine.AddGoFlagSet(flag.CommandLine) |
| 117 | logs.InitLogs() |
| 118 | defer logs.FlushLogs() |
| 119 | command := cmd.NewDefaultKubectlCommandWithArgs(cmd.NewDefaultPluginHandler(plugin.ValidPluginFilenamePrefixes), os.Args[2:], os.Stdin, os.Stdout, os.Stderr) |
| 120 | command.SetArgs(os.Args[2:]) |
| 121 | if err := command.Execute(); err != nil { |
| 122 | os.Exit(1) |
| 123 | } |
| 124 | } |
| 125 | } |