-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (53 loc) · 1.5 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
package main
import (
"bytes"
"context"
"flag"
"fmt"
"io"
"net/http"
log "github.com/sirupsen/logrus"
"github.com/dmitsh/k8sutils/pkg/kubeclient"
)
var (
client *kubeclient.Client
namespace, pod, container string
port int
)
func doUsage(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
io.WriteString(w, fmt.Sprintf("Usage: curl %v/exec\n", ctx.Value("serverAddr")))
}
func doExec(w http.ResponseWriter, r *http.Request) {
var stdout, stderr bytes.Buffer
err := client.ExecCmd(context.Background(), namespace, pod, container,
[]string{"cat", "/etc/test/result.json"}, &stdout, &stderr)
log.Infof("STDOUT: %s", stdout.String())
log.Infof("STDERR: %s", stderr.String())
if err == nil {
w.Header().Set("Content-Type", "application/json")
w.Write(stdout.Bytes())
w.WriteHeader(http.StatusOK)
} else {
log.Error(err.Error())
w.WriteHeader(http.StatusBadRequest)
}
}
func main() {
flag.StringVar(&namespace, "namespace", "default", "namespace")
flag.StringVar(&pod, "pod", "test", "pod name")
flag.StringVar(&container, "container", "test", "container name")
flag.IntVar(&port, "port", 8080, "server port")
flag.Parse()
var err error
client, err = kubeclient.New()
if err != nil {
log.Fatalf("failed to create Kubernetes client: %v", err)
}
mux := http.NewServeMux()
mux.HandleFunc("/", doUsage)
mux.HandleFunc("/exec", doExec)
if err = http.ListenAndServe(fmt.Sprintf(":%d", port), mux); err != nil {
log.Fatal(err.Error())
}
}