-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (84 loc) · 2.11 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
package main
import (
"flag"
"log"
"net/http"
"path/filepath"
"github.com/bryant-rh/k8sutil/terminal"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
var (
client kubernetes.Interface
config *rest.Config
)
func handler(w http.ResponseWriter, r *http.Request) {
urlValues := r.URL.Query()
namespaces, ok := urlValues["namespace"]
if !ok || len(namespaces) < 1 {
w.WriteHeader(http.StatusBadRequest)
return
}
namespace := namespaces[0]
podnames, ok := urlValues["pod"]
if !ok || len(podnames) < 1 {
w.WriteHeader(http.StatusBadRequest)
return
}
podName := podnames[0]
containers, ok := urlValues["container"]
if !ok || len(containers) < 1 {
w.WriteHeader(http.StatusBadRequest)
return
}
containerName := containers[0]
commands, ok := urlValues["shell"]
if !ok || len(containers) < 1 {
w.WriteHeader(http.StatusBadRequest)
return
}
cmd := commands[0]
ts, err := terminal.NewTerminalSession(client, w, r, nil)
if err != nil {
log.Printf("unable to upgrade websocket, %v", err)
return
}
defer ts.Close()
if err := ts.Exec(config, namespace, podName, containerName, []string{cmd}, &terminal.ExecOptions{
Stdin: true,
Stdout: true,
Stderr: true,
TTY: true,
}); err != nil {
log.Printf("unable to execute stream in container, %v", err)
ts.Done()
return
}
}
func main() {
var (
kubeconfig *string
err error
)
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// use the current context in kubeconfig
config, err = clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
client, err = kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
http.HandleFunc("/exec", handler)
http.Handle("/", http.FileServer(http.Dir("./frontend")))
log.Println("Listen on :8080")
http.ListenAndServe(":8080", nil)
}