-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathkube-debug.go
129 lines (108 loc) · 5.9 KB
/
kube-debug.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"flag"
"os"
"os/user"
// "bufio"
"strconv"
// "strings"
"path/filepath"
"kube-debug/lib"
)
func main() {
//Set variable
const VERSION string = "v0.1.0"
var pod string
var namespace string
var kubeconfig string
var container string
var node string
var hostIP string
var debugPort int = 0
var hostUsername string
var hostHomedir string
var currentdir string
//Setting the flag of command parameters
versionFlag := flag.Bool("version",false,"View software version information.")
helpFlag := flag.Bool("help",false,"View usage help information.")
initFlag := flag.Bool("init",false,"Initialize the kube-debug environment.")
debugportFlag := flag.Int("debugport",debugPort,"Custom debug container in the local debugging listening port (the default is TCP 3080 port).")
localhostFlag := flag.Bool("localhost",false,"Debug the local host.")
clearFlag := flag.Bool("clear",false,"Clean up the local host debugging environment.")
flag.StringVar(&node,"node","","Set the kubernetes node IP to query.")
flag.StringVar(&container,"container","","Set the target container ID or container name to be debugged.")
flag.StringVar(&pod,"pod","","Set the kubernetes pod name to query.")
flag.StringVar(&namespace,"namespace","","Set the namespace of kubernetes pod to be queried.")
flag.StringVar(&kubeconfig,"kubeconfig","","Set the kubeconfig file path of kubernetes cluster to be queried.")
flag.Parse()
ip, err := kdlib.ExternalIP()
kdlib.CheckErr(err)
hostIP = ip.String()
if *debugportFlag != debugPort {
debugPort = *debugportFlag
}
u, err := user.Current()
hostUsername = u.Username
hostHomedir = u.HomeDir
path, err := os.Executable()
kdlib.CheckErr(err)
currentdir = filepath.Dir(path)
switch {
case *initFlag :
kdlib.InitDebugEnv(currentdir, debugPort)
case container != "" :
kdContainerName := "kube-debug-container-"+container
kdlib.CheckSoft("iptables")
kdlib.CheckSoft("docker")
kdlib.CheckDebugPort(debugPort)
kdlib.CheckPortExist(debugPort)
kdlib.RunLocalContainer(container, kdContainerName, hostIP, debugPort, VERSION)
fmt.Println("\nNotice: You can now enter the debugging interface in the following two ways:\n (1) Using a web browser to access http://Localhost's_IP:"+strconv.Itoa(debugPort)+" Debug! (Recommended URL: http://"+hostIP+":"+strconv.Itoa(debugPort)+")\n (2) Use the command to debug directly on the local host: docker exec -it "+kdContainerName+" /bin/bash \n")
case pod != "" :
kdContainerName := "kube-debug-pod-"+pod
kdlib.CheckSoft("ssh-copy-id")
kdlib.CheckSoft("scp")
kdlib.CheckSoft("ssh")
kdlib.CheckDebugPort(debugPort)
kdlib.CheckParamString("namespace",namespace)
kdlib.CheckParamString("kubeconfig",kubeconfig)
kdlib.CheckFileExist(kubeconfig)
nodeIP,podIP,containerID := kdlib.GetPod(pod, namespace, kubeconfig)
kdlib.CheckIP(nodeIP)
kdlib.CheckIP(podIP)
fmt.Println("Preparing kube-debug environment, Please wait... \n")
sudoStr,sshStr := kdlib.CheckSshLoginSudo(nodeIP, hostUsername, hostHomedir)
kdlib.GenerateRemoteCheck(nodeIP, hostUsername, currentdir, debugPort)
kdlib.RunRemoteContainer(hostUsername,nodeIP,podIP,containerID,kdContainerName,VERSION,debugPort,sudoStr,sshStr)
fmt.Println("\nNotice: You can now enter the debugging interface in the following two ways:\n (1) Using a web browser to access http://k8s-node's_IP:"+strconv.Itoa(debugPort)+" Debug! (Recommended URL: http://"+nodeIP+":"+strconv.Itoa(debugPort)+")\n (2) Login to the target k8s-node host ("+nodeIP+"), debugging with commands: docker exec -it "+kdContainerName+" /bin/bash \n")
case node != "" :
kdContainerName := "kube-debug-node-"+node
kdlib.CheckDebugPort(debugPort)
kdlib.CheckIP(node)
kdlib.CheckSoft("ssh-copy-id")
kdlib.CheckSoft("scp")
kdlib.CheckSoft("ssh")
fmt.Println("Preparing kube-debug environment, Please wait... \n")
sudoStr,sshStr := kdlib.CheckSshLoginSudo(node, hostUsername, hostHomedir)
kdlib.GenerateRemoteCheck(node, hostUsername, currentdir, debugPort)
kdlib.RunRemoteContainer(hostUsername,node,"","",kdContainerName,VERSION,debugPort,sudoStr,sshStr)
fmt.Println("\nNotice: You can now enter the debugging interface in the following two ways:\n (1) Using a web browser to access http://k8s-node's_IP:"+strconv.Itoa(debugPort)+" Debug! (Recommended URL: http://"+node+":"+strconv.Itoa(debugPort)+")\n (2) Login to the target k8s-node host ("+node+"), debugging with commands: docker exec -it "+kdContainerName+" /bin/bash \n")
case *localhostFlag :
if debugPort == 0 {
debugPort = 3080
}
kdlib.CheckSoft("docker")
kdlib.RunLocalContainer("", "", "", debugPort, VERSION)
fmt.Println("\nNotice: You can now enter the debugging interface in the following two ways:\n (1) Using a web browser to access http://Localhost's_IP:"+strconv.Itoa(debugPort)+" Debug! (Recommended URL: http://"+hostIP+":"+strconv.Itoa(debugPort)+")\n (2) Use the command to debug directly on the local host: docker exec -it kube-debug-localhost /bin/bash \n")
case *clearFlag :
kdlib.ClearDebuggEnv(hostUsername)
case *versionFlag :
kdlib.ShowVersion()
case *helpFlag :
kdlib.ShowHelp()
default:
fmt.Println("Notice: the command parameter you entered is wrong. Please refer to the help document below and try again after checking! \n")
kdlib.ShowHelp()
}
}