-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (100 loc) · 2.78 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
// golang proxy server
package main
import (
"context"
"fmt"
"log"
"net/http"
"net/http/httputil"
"os"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
escv1alpha1 "github.com/koba1t/ESC/api/v1alpha1"
//https://pkg.go.dev/github.com/cenkalti/backoff/v4?tab=doc
)
var (
scheme = runtime.NewScheme()
)
func init() {
// init scheme
_ = escv1alpha1.AddToScheme(scheme)
}
func main() {
usernameHeader := os.Getenv("USERNAME_HEADER")
if usernameHeader == "" {
// oauth2-proxy set username for X-Auth-Request-User
// X-Auth-Request-User: koba1t
usernameHeader = "X-Auth-Request-User"
}
localClusterDomain := os.Getenv("LOCAL_CLUSTER_DOMAIN")
if localClusterDomain == "" {
localClusterDomain = "cluster.local"
}
namespaceName := os.Getenv("TARGET_NAMESPACE_NAME")
if namespaceName == "" {
// If not set value, using default namespace.
namespaceName = "default"
}
escTemplateName := os.Getenv("ESC_TEMPLATE_NAME")
if escTemplateName == "" {
log.Fatal("template name is not set")
}
// create k8s client
ctx := context.Background()
cl, err := client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme})
if err != nil {
fmt.Println("failed to create client")
os.Exit(1)
}
userland := &escv1alpha1.Userland{}
nn := client.ObjectKey{
Namespace: "default",
Name: "name",
}
_ = cl.Get(ctx, nn, userland)
// Reverse proxy director
director := func(req *http.Request) {
username := req.Header.Get(usernameHeader)
if username == "" {
fmt.Printf("Username is not set at %s\n", usernameHeader)
return
}
req.URL.Scheme = "http"
req.URL.Host = escTemplateName + "-" + username + "-svc." + namespaceName + ".svc." + localClusterDomain
fmt.Printf("ReverseProxy for %s\n", req.URL.Host)
}
errorHandle := func(rw http.ResponseWriter, req *http.Request, err error) {
fmt.Printf("[ErrorHandle] http: proxy error: %v\n", err)
username := req.Header.Get(usernameHeader)
// create userland resource
escuser := &escv1alpha1.Userland{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespaceName,
Name: username,
},
Spec: escv1alpha1.UserlandSpec{
TemplateName: escTemplateName,
},
}
e := cl.Create(context.Background(), escuser)
if e != nil {
fmt.Printf("Userland create error: %v\n", e)
}
rw.WriteHeader(http.StatusOK)
rw.Write([]byte("<html><head><meta http-equiv=\"Refresh\" content=\"10\">Creating container.</head><body>"))
//https://golang.org/pkg/net/http/
}
rp := &httputil.ReverseProxy{
Director: director,
ErrorHandler: errorHandle,
}
srv := &http.Server{
Addr: ":8080",
Handler: rp,
}
if err := srv.ListenAndServe(); err != nil {
log.Fatal(err.Error())
}
}