-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
45 lines (37 loc) · 1.35 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
package main
import (
"os/exec"
"fmt"
"log"
"net/http"
)
var (
script_path = "./create_sa_user.sh"
kubecfg_path = "/tmp"
default_namespace = "test"
)
func createNamespace(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
namespace := r.Form["namespace"][0]
kubecfg_file := fmt.Sprintf("%s/%s", kubecfg_path, "k8s-config-" + namespace)
cmd := []string{"create", namespace, kubecfg_path}
out, err := exec.Command(script_path, cmd...).Output()
if err != nil {
fmt.Printf("Failed to execute command: %v, output: %s, Error: %v", cmd, out, err)
fmt.Fprintf(w, fmt.Sprintf("Failed to create namespace: %s", namespace))
return
}
fmt.Printf("Output of command: %v, output: %s", cmd, out)
w.Header().Set("Content-Disposition", "attachment; filename=config")
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
http.ServeFile(w, r, kubecfg_file)
}
func deleteNamespace(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, fmt.Sprint("I am sorry, but I don't know how to delete namespaces yet. Would you teach me that and submit PR at https://github.com/prgcont/workshop-namespaces please?"))
}
func main() {
http.Handle("/", http.FileServer(http.Dir("./static")))
http.HandleFunc("/create", createNamespace) // set router
http.HandleFunc("/delete", deleteNamespace) // set router
log.Fatal(http.ListenAndServe(":9090", nil))
}