This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.go
136 lines (106 loc) · 3.22 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"embed"
"io/fs"
"net/http"
"os"
"path/filepath"
"github.com/fluxcd/pkg/runtime/logger"
"github.com/fluxcd/webui/pkg/clustersserver"
"github.com/go-logr/logr"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"
)
func init() {
var durations = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "HTTP request durations",
Buckets: prometheus.DefBuckets,
}, []string{"service", "method", "status"})
prometheus.MustRegister(durations)
}
func initialContexts() (contexts []string, currentCtx string, err error) {
cfgLoadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
rules, err := cfgLoadingRules.Load()
if err != nil {
return contexts, currentCtx, err
}
for contextName := range rules.Contexts {
contexts = append(contexts, contextName)
}
return contexts, rules.CurrentContext, nil
}
func main() {
log := logger.NewLogger(logger.Options{LogLevel: "debug"})
mux := http.NewServeMux()
mux.Handle("/metrics/", promhttp.Handler())
mux.Handle("/health/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
kubeContexts, currentKubeContext, err := initialContexts()
if err != nil {
log.Error(err, "could not get k8s contexts")
os.Exit(1)
}
clusters := clustersserver.NewServer(kubeContexts, currentKubeContext)
mux.Handle("/api/clusters/", http.StripPrefix("/api/clusters", clusters))
assetFS := getAssets()
assetHandler := http.FileServer(http.FS(assetFS))
redirector := createRedirector(assetFS, log)
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
extension := filepath.Ext(req.URL.Path)
// We use the golang http.FileServer for static file requests.
// This will return a 404 on normal page requests, ie /kustomizations and /sources.
// Redirect all non-file requests to index.html, where the JS routing will take over.
if extension == "" {
redirector(w, req)
return
}
assetHandler.ServeHTTP(w, req)
}))
log.Info("Serving on port 9000")
if err := http.ListenAndServe(":9000", mux); err != nil {
log.Error(err, "server exited")
os.Exit(1)
}
}
//go:embed dist/*
var static embed.FS
func getAssets() fs.FS {
f, err := fs.Sub(static, "dist")
if err != nil {
panic(err)
}
return f
}
func createRedirector(fsys fs.FS, log logr.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
indexPage, err := fsys.Open("index.html")
if err != nil {
log.Error(err, "could not open index.html page")
w.WriteHeader(http.StatusInternalServerError)
return
}
stat, err := indexPage.Stat()
if err != nil {
log.Error(err, "could not get index.html stat")
w.WriteHeader(http.StatusInternalServerError)
return
}
bt := make([]byte, stat.Size())
_, err = indexPage.Read(bt)
if err != nil {
log.Error(err, "could not read index.html")
w.WriteHeader(http.StatusInternalServerError)
return
}
_, err = w.Write(bt)
if err != nil {
log.Error(err, "error writing index.html")
w.WriteHeader(http.StatusInternalServerError)
return
}
}
}