-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
86 lines (70 loc) · 2.39 KB
/
root.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
package cmd
import (
"flag"
"log"
"os"
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/cmd/debug"
"k8s.io/kubectl/pkg/cmd/run"
"k8s.io/cli-runtime/pkg/genericclioptions"
kcmdutil "k8s.io/kubectl/pkg/cmd/util"
)
const (
hostNetworkOverride = "{\"spec\": {\"hostNetwork\": true}}"
)
var (
hostNetwork bool
imageName string
imageTag string
rootCmd = &cobra.Command{
Use: "kubectl-netshoot",
Short: "kubectl plugin for spinning up netshoot container for network troubleshooting.",
Long: "kubectl plugin for spinning up netshoot container for network troubleshooting.",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
setFlagsForChildCmds(cmd)
},
}
)
func init() {
rootCmd.PersistentFlags().BoolVar(&hostNetwork,
"host-network", false, "(\"run\" command only) spin up netshoot on the node's network namespace")
rootCmd.PersistentFlags().StringVar(&imageName,
"image-name", "nicolaka/netshoot", "netshoot container image to use")
rootCmd.PersistentFlags().StringVar(&imageTag,
"image-tag", "latest", "netshoot container image tag to use")
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDiscoveryBurst(350).WithDiscoveryQPS(50.0)
matchVersionKubeConfigFlags := kcmdutil.NewMatchVersionFlags(kubeConfigFlags)
f := kcmdutil.NewFactory(matchVersionKubeConfigFlags)
ioStreams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
debugCmd := debug.NewCmdDebug(f, ioStreams)
debugCmd.SetHelpTemplate(debugHelp)
debugCmd.Short = debugShort
debugCmd.Flags().Set("stdin", "true")
debugCmd.Flags().Set("tty", "true")
rootCmd.AddCommand(debugCmd)
runCmd := run.NewCmdRun(f, ioStreams)
runCmd.SetHelpTemplate(runHelp)
runCmd.Short = runShort
runCmd.Flags().Set("stdin", "true")
runCmd.Flags().Set("tty", "true")
runCmd.Flags().Set("rm", "true")
rootCmd.AddCommand(runCmd)
kubeConfigFlags.AddFlags(rootCmd.PersistentFlags())
matchVersionKubeConfigFlags.AddFlags(rootCmd.PersistentFlags())
rootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
}
func setFlagsForChildCmds(cmd *cobra.Command) {
fullImageName := imageName + ":" + imageTag
if cmd.Name() == "run" || cmd.Name() == "debug" {
cmd.Flags().Set("image", fullImageName)
}
if cmd.Name() == "run" && hostNetwork {
cmd.Flags().Set("overrides", hostNetworkOverride)
}
}
func Execute() {
err := rootCmd.Execute()
if err != nil {
log.Fatalf("error: %v\n", err)
}
}