Skip to content

Commit b222dea

Browse files
committed
Pick Up New Core Libraries
Updates to new interfaces for core middleware. Ports identity middleware over to the new paradigm to maintain consistency. This primarily consists of making existing middleare aware of the route resolver so we aren't calling this expensive routing lookup more than once, and updating tests to be compatible with that.
1 parent 103ad8e commit b222dea

File tree

3 files changed

+44
-39
lines changed

3 files changed

+44
-39
lines changed

go.mod

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ go 1.24.3
44

55
require (
66
github.com/getkin/kin-openapi v0.132.0
7-
github.com/go-chi/chi/v5 v5.2.2
7+
github.com/go-chi/chi/v5 v5.2.4
88
github.com/oapi-codegen/runtime v1.1.1
99
github.com/onsi/ginkgo/v2 v2.22.0
1010
github.com/onsi/gomega v1.36.1
11+
github.com/pact-foundation/pact-go/v2 v2.0.7
1112
github.com/spf13/pflag v1.0.10
1213
github.com/spjmurray/go-util v0.1.3
1314
github.com/stretchr/testify v1.11.1
14-
github.com/unikorn-cloud/core v1.13.1-0.20260126035616-2b67e263f08d
15-
github.com/unikorn-cloud/identity v1.13.0
16-
github.com/unikorn-cloud/region v1.13.0
17-
go.opentelemetry.io/otel/sdk v1.35.0
15+
github.com/unikorn-cloud/core v1.13.1-0.20260127150215-6c9e7fe90056
16+
github.com/unikorn-cloud/identity v1.13.1-0.20260127151745-d413fc7b870f
17+
github.com/unikorn-cloud/region v1.13.1-0.20260127163608-82e1da69b700
1818
go.uber.org/mock v0.5.2
1919
k8s.io/api v0.33.1
2020
k8s.io/apimachinery v0.33.1
@@ -26,6 +26,7 @@ require (
2626
github.com/Masterminds/semver/v3 v3.3.1 // indirect
2727
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
2828
github.com/beorn7/perks v1.0.1 // indirect
29+
github.com/brunoga/deep v1.2.4 // indirect
2930
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
3031
github.com/cespare/xxhash/v2 v2.3.0 // indirect
3132
github.com/coreos/go-oidc/v3 v3.14.1 // indirect
@@ -52,11 +53,8 @@ require (
5253
github.com/google/go-cmp v0.7.0 // indirect
5354
github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db // indirect
5455
github.com/google/uuid v1.6.0 // indirect
55-
github.com/gorilla/mux v1.8.1 // indirect
5656
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 // indirect
57-
github.com/hashicorp/go-version v1.7.0 // indirect
5857
github.com/hashicorp/logutils v1.0.0 // indirect
59-
github.com/inconshreveable/mousetrap v1.1.0 // indirect
6058
github.com/josharian/intern v1.0.0 // indirect
6159
github.com/json-iterator/go v1.1.12 // indirect
6260
github.com/mailru/easyjson v0.9.0 // indirect
@@ -67,7 +65,6 @@ require (
6765
github.com/oapi-codegen/oapi-codegen/v2 v2.5.0 // indirect
6866
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 // indirect
6967
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect
70-
github.com/pact-foundation/pact-go/v2 v2.0.7 // indirect
7168
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
7269
github.com/perimeterx/marshmallow v1.1.5 // indirect
7370
github.com/pkg/errors v0.9.1 // indirect
@@ -82,7 +79,6 @@ require (
8279
github.com/speakeasy-api/openapi-overlay v0.10.2 // indirect
8380
github.com/spf13/afero v1.15.0 // indirect
8481
github.com/spf13/cast v1.10.0 // indirect
85-
github.com/spf13/cobra v1.8.1 // indirect
8682
github.com/spf13/viper v1.21.0 // indirect
8783
github.com/subosito/gotenv v1.6.0 // indirect
8884
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
@@ -92,6 +88,7 @@ require (
9288
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.35.0 // indirect
9389
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.35.0 // indirect
9490
go.opentelemetry.io/otel/metric v1.35.0 // indirect
91+
go.opentelemetry.io/otel/sdk v1.35.0 // indirect
9592
go.opentelemetry.io/otel/trace v1.35.0 // indirect
9693
go.opentelemetry.io/proto/otlp v1.6.0 // indirect
9794
go.uber.org/multierr v1.11.0 // indirect
@@ -127,3 +124,9 @@ require (
127124
)
128125

129126
tool github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen
127+
128+
replace (
129+
github.com/unikorn-cloud/core => ../core
130+
github.com/unikorn-cloud/identity => ../identity
131+
github.com/unikorn-cloud/region => ../region
132+
)

go.sum

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP
66
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
77
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
88
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
9+
github.com/brunoga/deep v1.2.4 h1:Aj9E9oUbE+ccbyh35VC/NHlzzjfIVU69BXu2mt2LmL8=
10+
github.com/brunoga/deep v1.2.4/go.mod h1:GDV6dnXqn80ezsLSZ5Wlv1PdKAWAO4L5PnKYtv2dgaI=
911
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
1012
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
1113
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
@@ -15,7 +17,6 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P
1517
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
1618
github.com/coreos/go-oidc/v3 v3.14.1 h1:9ePWwfdwC4QKRlCXsJGou56adA/owXczOzwKdOumLqk=
1719
github.com/coreos/go-oidc/v3 v3.14.1/go.mod h1:HaZ3szPaZ0e4r6ebqvsLWlk2Tn+aejfmrfah6hnSYEU=
18-
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
1920
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2021
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2122
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
@@ -41,8 +42,8 @@ github.com/fxamacker/cbor/v2 v2.8.0 h1:fFtUGXUzXPHTIUdne5+zzMPTfffl3RD5qYnkY40vt
4142
github.com/fxamacker/cbor/v2 v2.8.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ=
4243
github.com/getkin/kin-openapi v0.132.0 h1:3ISeLMsQzcb5v26yeJrBcdTCEQTag36ZjaGk7MIRUwk=
4344
github.com/getkin/kin-openapi v0.132.0/go.mod h1:3OlG51PCYNsPByuiMB0t4fjnNlIDnaEDsjiKUV8nL58=
44-
github.com/go-chi/chi/v5 v5.2.2 h1:CMwsvRVTbXVytCk1Wd72Zy1LAsAh9GxMmSNWLHCG618=
45-
github.com/go-chi/chi/v5 v5.2.2/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
45+
github.com/go-chi/chi/v5 v5.2.4 h1:WtFKPHwlywe8Srng8j2BhOD9312j9cGUxG1SP4V2cR4=
46+
github.com/go-chi/chi/v5 v5.2.4/go.mod h1:X7Gx4mteadT3eDOMTsXzmI4/rwUpOwBHLpAfupzFJP0=
4647
github.com/go-jose/go-jose/v4 v4.1.1 h1:JYhSgy4mXXzAdF3nUx3ygx347LRXJRrpgyU3adRmkAI=
4748
github.com/go-jose/go-jose/v4 v4.1.1/go.mod h1:BdsZGqgdO3b6tTc6LSE56wcDbMMLuPsw5d4ZD5f94kA=
4849
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
@@ -101,14 +102,10 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
101102
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
102103
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 h1:5ZPtiqj0JL5oKWmcsq4VMaAW5ukBEgSGXEN89zeH1Jo=
103104
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3/go.mod h1:ndYquD05frm2vACXE1nsccT4oJzjhw2arTS2cpUD1PI=
104-
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
105-
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
106105
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
107106
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
108107
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
109108
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
110-
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
111-
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
112109
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
113110
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
114111
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@@ -185,7 +182,6 @@ github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzM
185182
github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is=
186183
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
187184
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
188-
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
189185
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
190186
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
191187
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
@@ -200,9 +196,6 @@ github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I=
200196
github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg=
201197
github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
202198
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
203-
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
204-
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
205-
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
206199
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
207200
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
208201
github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
@@ -222,12 +215,6 @@ github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8
222215
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
223216
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
224217
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
225-
github.com/unikorn-cloud/core v1.13.1-0.20260126035616-2b67e263f08d h1:4Uop8p10mYel8qkTN8dHno2+qVGpEAEHSJxtamOzn58=
226-
github.com/unikorn-cloud/core v1.13.1-0.20260126035616-2b67e263f08d/go.mod h1:U+ip8sDrF6HODW8Izc2TZG6HGCI+eUvZkDnG2bvQW/0=
227-
github.com/unikorn-cloud/identity v1.13.0 h1:uPr2ANskKTKTUcLquIbDFYKoi5sjnePHnvY8RuPuM8Y=
228-
github.com/unikorn-cloud/identity v1.13.0/go.mod h1:tn0bPBdyQC5/RZqx6GoW58JBCRXOpzqXeqDso3hKauA=
229-
github.com/unikorn-cloud/region v1.13.0 h1:zO3Iwvgj5OAoAqWWJmJiVnFONNe/c2jnvv3BLo6c0UE=
230-
github.com/unikorn-cloud/region v1.13.0/go.mod h1:Xv4G6TpYSA80cVvTDynUeh5FROvT+Jnw2/Gw5UpjfbU=
231218
github.com/vmware-labs/yaml-jsonpath v0.3.2 h1:/5QKeCBGdsInyDCyVNLbXyilb61MXGi9NP674f9Hobk=
232219
github.com/vmware-labs/yaml-jsonpath v0.3.2/go.mod h1:U6whw1z03QyqgWdgXxvVnQ90zN1BWz5V+51Ewf8k+rQ=
233220
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=

pkg/server/server.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ import (
2525

2626
chi "github.com/go-chi/chi/v5"
2727
"github.com/spf13/pflag"
28-
"go.opentelemetry.io/otel/sdk/trace"
2928

3029
"github.com/unikorn-cloud/compute/pkg/constants"
3130
"github.com/unikorn-cloud/compute/pkg/openapi"
3231
"github.com/unikorn-cloud/compute/pkg/server/handler"
3332
coreclient "github.com/unikorn-cloud/core/pkg/client"
34-
coreapi "github.com/unikorn-cloud/core/pkg/openapi"
33+
"github.com/unikorn-cloud/core/pkg/openapi/helpers"
3534
"github.com/unikorn-cloud/core/pkg/options"
3635
"github.com/unikorn-cloud/core/pkg/server/middleware/cors"
36+
"github.com/unikorn-cloud/core/pkg/server/middleware/logging"
3737
"github.com/unikorn-cloud/core/pkg/server/middleware/opentelemetry"
38-
"github.com/unikorn-cloud/core/pkg/server/middleware/timeout"
38+
"github.com/unikorn-cloud/core/pkg/server/middleware/routeresolver"
3939
identityclient "github.com/unikorn-cloud/identity/pkg/client"
4040
"github.com/unikorn-cloud/identity/pkg/middleware/audit"
4141
openapimiddleware "github.com/unikorn-cloud/identity/pkg/middleware/openapi"
@@ -98,7 +98,7 @@ func (s *Server) SetupLogging() {
9898
// logs by default, and optionally ship the spans to an OTLP listener.
9999
// TODO: move config into an otel specific options struct.
100100
func (s *Server) SetupOpenTelemetry(ctx context.Context) error {
101-
return s.CoreOptions.SetupOpenTelemetry(ctx, trace.WithSpanProcessor(&opentelemetry.LoggingSpanProcessor{}))
101+
return s.CoreOptions.SetupOpenTelemetry(ctx)
102102
}
103103

104104
func (s *Server) GetServer(client client.Client) (*http.Server, error) {
@@ -123,18 +123,30 @@ func (s *Server) GetServer(client client.Client) (*http.Server, error) {
123123
}
124124
}()
125125

126-
schema, err := coreapi.NewSchema(openapi.GetSwagger)
126+
schema, err := helpers.NewSchema(openapi.GetSwagger)
127127
if err != nil {
128128
return nil, err
129129
}
130130

131-
corsMiddleware := cors.New(schema, &s.CORSOptions)
131+
router := chi.NewRouter()
132132

133133
// Middleware specified here is applied to all requests pre-routing.
134-
router := chi.NewRouter()
135-
router.Use(timeout.Middleware(s.ServerOptions.RequestTimeout))
136-
router.Use(opentelemetry.Middleware(constants.Application, constants.Version))
137-
router.Use(corsMiddleware.Middleware)
134+
// Ordering is important:
135+
// * OpenTelemetry middleware optionally transmits spans over OTLP, but also
136+
// establishes a trace ID that is used to correlate logs with user issues.
137+
// * Logging ensures at least all errors are captured by logging telemetry and we
138+
// can trigger alerts based on them.
139+
// * Route resolver provides routing and OpenAPI information to child middlewares.
140+
// * CORS emulates OPTIONS endpoints based on OpenAPI (requires route resolver).
141+
opentelemetry := opentelemetry.New(constants.Application, constants.Version)
142+
logging := logging.New()
143+
routeresolver := routeresolver.New(schema)
144+
cors := cors.New(&s.CORSOptions)
145+
146+
router.Use(opentelemetry.Middleware)
147+
router.Use(logging.Middleware)
148+
router.Use(routeresolver.Middleware)
149+
router.Use(cors.Middleware)
138150
router.NotFound(http.HandlerFunc(handler.NotFound))
139151
router.MethodNotAllowed(http.HandlerFunc(handler.MethodNotAllowed))
140152

@@ -143,14 +155,17 @@ func (s *Server) GetServer(client client.Client) (*http.Server, error) {
143155
return nil, err
144156
}
145157

158+
validator := openapimiddleware.NewValidator(&s.OpenAPIOptions, authorizer)
159+
audit := audit.New(constants.Application, constants.Version)
160+
146161
// Middleware specified here is applied to all requests post-routing.
147162
// NOTE: these are applied in reverse order!!
148163
chiServerOptions := openapi.ChiServerOptions{
149164
BaseRouter: router,
150165
ErrorHandlerFunc: handler.HandleError,
151166
Middlewares: []openapi.MiddlewareFunc{
152-
audit.Middleware(schema, constants.Application, constants.Version),
153-
openapimiddleware.Middleware(&s.OpenAPIOptions, authorizer, schema),
167+
audit.Middleware,
168+
validator.Middleware,
154169
},
155170
}
156171

0 commit comments

Comments
 (0)