-
Notifications
You must be signed in to change notification settings - Fork 2
/
exec-pods.go
102 lines (81 loc) · 2.05 KB
/
exec-pods.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
package main
import (
"encoding/json"
"fmt"
"net/http"
v1 "k8s.io/api/core/v1"
)
type Connection struct {
From string
FromNamespace string
To string
ToNamespace string
}
var result bool = false
type ExecCommand struct {
PodName string
PodNamespace string
Command string
}
type ExecResult struct {
CommandOutput string
Error bool
}
func podExecHandler(w http.ResponseWriter, r *http.Request) {
var exec ExecCommand
err := json.NewDecoder(r.Body).Decode(&exec)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
fmt.Println(err.Error())
}
clientset := getClientSet()
pods := getPods(clientset)
// Search for pod and service to test connection on
var pod v1.Pod
for _, p := range pods {
if exec.PodName == p.Name && exec.PodNamespace == p.Namespace {
pod = p
}
}
stdout, stderr, _ := execIntoPod(clientset, &pod, exec.Command, nil)
var output ExecResult
if stdout != "" {
output.CommandOutput = string(stdout)
fmt.Println(output.CommandOutput)
output.Error = false
} else {
output.CommandOutput = string(stderr)
output.Error = true
}
formattedOutput, _ := json.Marshal(output)
fmt.Println(string(formattedOutput))
json.NewEncoder(w).Encode(string(formattedOutput))
}
func TestConnection(w http.ResponseWriter, r *http.Request) {
var conn Connection
err := json.NewDecoder(r.Body).Decode(&conn)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
fmt.Println(err.Error())
// return
}
clientset := getClientSet()
pods := getPods(clientset)
svcs := getServices(clientset)
// Search for pod and service to test connection on
var podFrom v1.Pod
var serviceTo v1.Service
for _, pod := range pods {
if conn.From == pod.Name && conn.FromNamespace == pod.Namespace {
podFrom = pod
}
}
for _, svc := range svcs {
if conn.To == svc.Name && conn.ToNamespace == svc.Namespace {
serviceTo = svc
}
}
result = testConnectionPodToService(podFrom, serviceTo)
connResult, _ := json.Marshal(result)
json.NewEncoder(w).Encode(string(connResult))
}