-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
125 lines (104 loc) · 3.59 KB
/
main.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/spf13/pflag"
"golang.org/x/crypto/ssh/terminal"
"k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
)
var (
containerName = pflag.StringP("container", "c", "", "Container in which to execute the command. Defaults to only container if there is only one container in the pod.")
namespace = pflag.StringP("namespace", "n", "", "Namespace where pod is deployed. Defaults to default.")
command = pflag.StringSliceP("command", "e", []string{"sh"}, "The remote command to execute. Defaults to sh.")
help = pflag.BoolP("help", "h", false, "Prints help for application.")
)
func main() {
validUsageAndExitOnFailure()
kubeconfig := getConfig(os.Getenv("KUBECONFIG"))
k8sCliCfg, err := kubeconfig.ClientConfig()
fatalOnErr(err, "while getting client cfg")
k8sCoreCli, err := corev1.NewForConfig(k8sCliCfg)
fatalOnErr(err, "while creating core client")
podName := pflag.Arg(0)
ns, err := determineNamespace(kubeconfig)
fatalOnErr(err, "while getting default namespace")
req := k8sCoreCli.RESTClient().
Post().
Namespace(ns).
Resource("pods").
Name(podName).
SubResource("exec").
VersionedParams(&v1.PodExecOptions{
Container: *containerName,
Command: *command,
Stdin: true,
Stdout: true,
Stderr: true,
TTY: true,
}, scheme.ParameterCodec)
fmt.Printf("Exec to POD %s/%s with command %q\n", ns, podName, *command)
exec, err := remotecommand.NewSPDYExecutor(k8sCliCfg, http.MethodPost, req.URL())
fatalOnErr(err, "while creating SPDY executor")
// By default terminal starts in cooked mode (canonical).
// In this mode, keyboard input is preprocessed before being given to a program.
// In Raw mode the data is passed to the program without interpreting any of the special characters, by that
// we are turning off the ECHO feature because we already connecting the streams between our terminal and the remote shell process
// Stdin: os.Stdin -> Stdout: os.Stdout,
oldState, err := terminal.MakeRaw(0)
fatalOnErr(err, "while putting terminal into raw mode")
defer terminal.Restore(0, oldState)
err = exec.Stream(remotecommand.StreamOptions{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Tty: true,
})
fatalOnErr(err, "connect to process")
}
func determineNamespace(cfg clientcmd.ClientConfig) (string, error) {
if *namespace != "" {
return *namespace, nil
}
ns, _, err := cfg.Namespace()
return ns, err
}
func getConfig(explicitKubeconfig string) clientcmd.ClientConfig {
rules := clientcmd.NewDefaultClientConfigLoadingRules()
rules.ExplicitPath = explicitKubeconfig
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, &clientcmd.ConfigOverrides{})
}
func fatalOnErr(err error, ctx string) {
if err != nil {
log.Fatalf("%s: %v", ctx, err)
}
}
func validUsageAndExitOnFailure() {
pflag.Parse()
if *help {
printHelpAndExit()
}
if pflag.NArg() == 0 || pflag.NArg() > 1 {
printArgErrMsgAndExit()
}
}
func printHelpAndExit() {
fmt.Println("Execute a command in a container.")
fmt.Printf("Usage: \n \t '%s POD_NAME [-c CONTAINER] [-n NAMESPACE] [-e COMMAND]'\n", os.Args[0])
fmt.Println("Options:")
pflag.PrintDefaults()
os.Exit(0)
}
func printArgErrMsgAndExit() {
fmt.Printf("Expected '%s POD_NAME [-c CONTAINER] [-n NAMESPACE] [-e COMMAND]'\n", os.Args[0])
fmt.Printf("POD is a required argument for the %s command\n", os.Args[0])
fmt.Println()
fmt.Println("Options:")
pflag.PrintDefaults()
os.Exit(1)
}