Skip to content

Commit 8237290

Browse files
authored
Enable CORS for async, batch, and task APIs (#2082)
1 parent 5aefd89 commit 8237290

File tree

7 files changed

+46
-2
lines changed

7 files changed

+46
-2
lines changed

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ operator-update:
109109
@kubectl scale --namespace=default deployments/operator --replicas=1
110110
@operator_pod=$$(kubectl get pods -l workloadID=operator --namespace=default -o jsonpath='{.items[0].metadata.name}') && kubectl wait --for=condition=ready pod $$operator_pod --namespace=default
111111

112+
# restart all in-cluster async-gateways
113+
async-gateway-restart:
114+
@$(MAKE) kubectl
115+
@kubectl delete pods -l cortex.dev/async=gateway --namespace=default
116+
117+
# build and update all in-cluster async-gateways
118+
async-gateway-update:
119+
@$(MAKE) kubectl
120+
@./dev/registry.sh update-single async-gateway
121+
@kubectl delete pods -l cortex.dev/async=gateway --namespace=default
122+
112123
# Docker images
113124

114125
images-all:

async-gateway/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.15
44

55
require (
66
github.com/aws/aws-sdk-go v1.37.23
7+
github.com/gorilla/handlers v1.5.1
78
github.com/gorilla/mux v1.8.0
89
go.uber.org/zap v1.16.0
910
)

async-gateway/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ github.com/aws/aws-sdk-go v1.37.23/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2z
55
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
77
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
9+
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
810
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
11+
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
12+
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
913
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
1014
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
1115
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=

async-gateway/main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/aws/aws-sdk-go/aws"
2626
"github.com/aws/aws-sdk-go/aws/session"
27+
"github.com/gorilla/handlers"
2728
"github.com/gorilla/mux"
2829
"go.uber.org/zap"
2930
"go.uber.org/zap/zapcore"
@@ -127,8 +128,18 @@ func main() {
127128
)
128129
router.HandleFunc("/{id}", ep.GetWorkload).Methods("GET")
129130

131+
// inspired by our nginx config
132+
corsOptions := []handlers.CORSOption{
133+
handlers.AllowedOrigins([]string{"*"}),
134+
// custom headers are not supported currently, since "*" is not supported in AllowedHeaders(); here are some common ones:
135+
handlers.AllowedHeaders([]string{"Content-Type", "X-Requested-With", "User-Agent", "Accept", "Accept-Language", "Content-Language", "Origin"}),
136+
handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"}),
137+
handlers.ExposedHeaders([]string{"Content-Length", "Content-Range"}),
138+
handlers.AllowCredentials(),
139+
}
140+
130141
log.Info("Running on port " + *port)
131-
if err = http.ListenAndServe(":"+*port, router); err != nil {
142+
if err = http.ListenAndServe(":"+*port, handlers.CORS(corsOptions...)(router)); err != nil {
132143
log.Fatal("failed to start server", zap.Error(err))
133144
}
134145
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ require (
2626
github.com/gobwas/glob v0.2.3
2727
github.com/google/uuid v1.1.2
2828
github.com/googleapis/gnostic v0.2.0 // indirect
29+
github.com/gorilla/handlers v1.5.1
2930
github.com/gorilla/mux v1.8.0
3031
github.com/gorilla/websocket v1.4.2
3132
github.com/imdario/mergo v0.3.6 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
149149
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
150150
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
151151
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
152+
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
153+
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
152154
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
153155
github.com/gavv/httpexpect v2.0.0+incompatible/go.mod h1:x+9tiU1YnrOvnB725RkpoLv1M62hOWzwo5OXotisrKc=
154156
github.com/getsentry/sentry-go v0.8.0 h1:F52cjBVLuiTfdW6p4JFuxlt3pOjKfWYT/aka7cdJ7v0=
@@ -267,6 +269,8 @@ github.com/googleapis/gnostic v0.2.0 h1:l6N3VoaVzTncYYW+9yOz2LJJammFZGBO13sqgEhp
267269
github.com/googleapis/gnostic v0.2.0/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
268270
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
269271
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
272+
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
273+
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
270274
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
271275
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
272276
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=

pkg/operator/main.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/cortexlabs/cortex/pkg/operator/resources/job/taskapi"
3434
"github.com/cortexlabs/cortex/pkg/operator/resources/realtimeapi"
3535
"github.com/cortexlabs/cortex/pkg/types/userconfig"
36+
"github.com/gorilla/handlers"
3637
"github.com/gorilla/mux"
3738
"github.com/prometheus/client_golang/prometheus/promhttp"
3839
)
@@ -127,5 +128,16 @@ func main() {
127128
routerWithAuth.HandleFunc("/logs/{apiName}", endpoints.ReadLogs)
128129

129130
operatorLogger.Info("Running on port " + _operatorPortStr)
130-
operatorLogger.Fatal(http.ListenAndServe(":"+_operatorPortStr, router))
131+
132+
// inspired by our nginx config
133+
corsOptions := []handlers.CORSOption{
134+
handlers.AllowedOrigins([]string{"*"}),
135+
// custom headers are not supported currently, since "*" is not supported in AllowedHeaders(); here are some common ones:
136+
handlers.AllowedHeaders([]string{"Content-Type", "X-Requested-With", "User-Agent", "Accept", "Accept-Language", "Content-Language", "Origin"}),
137+
handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"}),
138+
handlers.ExposedHeaders([]string{"Content-Length", "Content-Range"}),
139+
handlers.AllowCredentials(),
140+
}
141+
142+
operatorLogger.Fatal(http.ListenAndServe(":"+_operatorPortStr, handlers.CORS(corsOptions...)(router)))
131143
}

0 commit comments

Comments
 (0)