Skip to content

Commit bc61ad9

Browse files
[Feat][apiserver] Support CORS config (#3711)
1 parent 11c75ea commit bc61ad9

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

apiserver/cmd/main.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
1919
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
2020
"github.com/prometheus/client_golang/prometheus/promhttp"
21+
"github.com/rs/cors"
2122
"google.golang.org/grpc"
2223
"google.golang.org/grpc/credentials/insecure"
2324
"google.golang.org/grpc/reflection"
@@ -42,6 +43,7 @@ var (
4243
localSwaggerPath = flag.String("localSwaggerPath", "", "Specify the root directory for `*.swagger.json` the swagger files.")
4344
grpcTimeout = flag.Duration("grpc_timeout", util.GRPCServerDefaultTimeout, "gRPC server timeout duration")
4445
enableAPIServerV2 = flag.Bool("enable-api-server-v2", true, "Enable API server V2")
46+
corsAllowOrigin = flag.String("cors-allow-origin", "", "Set the Access-Control-Allow-Origin response header for the HTTP proxy.")
4547
healthy int32
4648
)
4749

@@ -165,8 +167,19 @@ func startHttpProxy() {
165167
topMux = http.NewServeMux()
166168
}
167169

168-
// Seems /apis (matches /apis/v1alpha1/clusters) works fine
169-
topMux.Handle("/", runtimeMux)
170+
if *corsAllowOrigin != "" {
171+
klog.Info("Enabling CORS with Access-Control-Allow-Origin:", *corsAllowOrigin)
172+
handler := cors.New(cors.Options{
173+
AllowedOrigins: []string{*corsAllowOrigin},
174+
}).Handler(runtimeMux)
175+
176+
topMux.Handle("/", handler)
177+
} else {
178+
klog.Info("Access-Control-Allow-Origin not set, CORS is disabled.")
179+
// Seems /apis (matches /apis/v1alpha1/clusters) works fine
180+
topMux.Handle("/", runtimeMux)
181+
}
182+
170183
topMux.Handle("/metrics", promhttp.Handler())
171184
topMux.HandleFunc("/swagger/", serveSwaggerFile)
172185
topMux.HandleFunc("/healthz", serveHealth)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
github.com/orcaman/concurrent-map/v2 v2.0.1
2323
github.com/pkg/errors v0.9.1
2424
github.com/prometheus/client_golang v1.22.0
25+
github.com/rs/cors v1.11.1
2526
github.com/rs/zerolog v1.34.0
2627
github.com/spf13/cobra v1.9.1
2728
github.com/spf13/pflag v1.0.6

go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

helm-chart/kuberay-apiserver/templates/deployment.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ spec:
3636
args:
3737
{{- $argList := list -}}
3838
{{- $argList = append $argList (printf "--enable-api-server-v2=%t" .Values.enableAPIServerV2) -}}
39+
{{- if .Values.cors }}
40+
{{- with .Values.cors.allowOrigin }}
41+
{{- $argList = append $argList (printf "--cors-allow-origin=%s" .) -}}
42+
{{- end }}
43+
{{- end }}
3944
{{- (printf "\n") -}}
4045
{{- $argList | toYaml | indent 10 }}
4146
ports:

helm-chart/kuberay-apiserver/values.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ image:
1111
tag: nightly
1212
pullPolicy: IfNotPresent
1313

14+
# Uncomment allowOrigin to enable CORS
15+
# It'll set the Access-Control-Allow-Origin response header for the HTTP proxy.
16+
cors:
17+
# allowOrigin: "*"
18+
1419
labels:
1520
# key1: value1
1621
# key2: value2

0 commit comments

Comments
 (0)