-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
144 lines (120 loc) · 4.32 KB
/
server.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
137
138
139
140
141
142
143
144
package router
import (
"fmt"
"net/http"
"os"
"github.com/equinor/radix-api/api/utils"
"github.com/equinor/radix-api/models"
_ "github.com/equinor/radix-api/swaggerui" // statik files
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rakyll/statik/fs"
"github.com/rs/cors"
"github.com/urfave/negroni"
)
const (
apiVersionRoute = "/api/v1"
admissionControllerRootPath = "/admissioncontrollers"
buildstatusControllerRootPath = "/buildstatus"
healthControllerPath = "/health/"
radixDNSZoneEnvironmentVariable = "RADIX_DNS_ZONE"
)
// Server Holds instance variables
type Server struct {
Middleware *negroni.Negroni
clusterName string
controllers []models.Controller
}
// NewServer Constructor function
func NewServer(clusterName string, kubeUtil utils.KubeUtil, controllers ...models.Controller) http.Handler {
router := mux.NewRouter().StrictSlash(true)
statikFS, err := fs.New()
if err != nil {
panic(err)
}
staticServer := http.FileServer(statikFS)
sh := http.StripPrefix("/swaggerui/", staticServer)
router.PathPrefix("/swaggerui/").Handler(sh)
initializeAPIServer(kubeUtil, router, controllers)
initializeHealthEndpoint(router)
serveMux := http.NewServeMux()
serveMux.Handle(healthControllerPath, negroni.New(
negroni.Wrap(router),
))
serveMux.Handle(fmt.Sprintf("%s/", admissionControllerRootPath), negroni.New(
negroni.Wrap(router),
))
serveMux.Handle("/api/", negroni.New(
negroni.Wrap(router),
))
// TODO: We should maybe have oauth to stop any non-radix user from beeing
// able to see the API
serveMux.Handle("/swaggerui/", negroni.New(
negroni.Wrap(router),
))
serveMux.Handle("/metrics", negroni.New(
negroni.Wrap(promhttp.Handler()),
))
rec := negroni.NewRecovery()
rec.PrintStack = false
n := negroni.New(
rec,
)
n.UseHandler(serveMux)
server := &Server{
n,
clusterName,
controllers,
}
return getCORSHandler(server)
}
func getCORSHandler(apiRouter *Server) http.Handler {
radixDNSZone := os.Getenv(radixDNSZoneEnvironmentVariable)
c := cors.New(cors.Options{
AllowedOrigins: []string{
"http://localhost:3000",
"http://localhost:3001",
"http://localhost:8000",
"http://localhost:8086", // For swaggerui testing
// TODO: We should consider:
// 1. "https://*.radix.equinor.com"
// 2. Keep cors rules in ingresses
fmt.Sprintf("https://console.%s", radixDNSZone),
getHostName("web", "radix-web-console-qa", apiRouter.clusterName, radixDNSZone),
getHostName("web", "radix-web-console-prod", apiRouter.clusterName, radixDNSZone),
getHostName("web", "radix-web-console-dev", apiRouter.clusterName, radixDNSZone),
// Due to active-cluster
getActiveClusterHostName("web", "radix-web-console-qa", radixDNSZone),
getActiveClusterHostName("web", "radix-web-console-prod", radixDNSZone),
getActiveClusterHostName("web", "radix-web-console-dev", radixDNSZone),
},
AllowCredentials: true,
MaxAge: 600,
AllowedHeaders: []string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"},
AllowedMethods: []string{"GET", "PUT", "POST", "OPTIONS", "DELETE", "PATCH"},
})
return c.Handler(apiRouter.Middleware)
}
func getActiveClusterHostName(componentName, namespace, radixDNSZone string) string {
return fmt.Sprintf("https://%s-%s.%s", componentName, namespace, radixDNSZone)
}
func getHostName(componentName, namespace, clustername, radixDNSZone string) string {
return fmt.Sprintf("https://%s-%s.%s.%s", componentName, namespace, clustername, radixDNSZone)
}
func initializeAPIServer(kubeUtil utils.KubeUtil, router *mux.Router, controllers []models.Controller) {
for _, controller := range controllers {
for _, route := range controller.GetRoutes() {
addHandlerRoute(kubeUtil, router, route)
}
}
}
func initializeHealthEndpoint(router *mux.Router) {
router.HandleFunc(healthControllerPath, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}).Methods("GET")
}
func addHandlerRoute(kubeUtil utils.KubeUtil, router *mux.Router, route models.Route) {
path := apiVersionRoute + route.Path
router.HandleFunc(path,
utils.NewRadixMiddleware(kubeUtil, path, route.Method, route.AllowUnauthenticatedUsers, route.KubeApiConfig.QPS, route.KubeApiConfig.Burst, route.HandlerFunc).Handle).Methods(route.Method)
}