refactor: dedup the route context code using reflection#1502
refactor: dedup the route context code using reflection#1502zirain merged 9 commits intoenvoyproxy:mainfrom
Conversation
Signed-off-by: Shawnh2 <shawnhxh@outlook.com>
Signed-off-by: Shawnh2 <shawnhxh@outlook.com>
Codecov Report
@@ Coverage Diff @@
## main #1502 +/- ##
========================================
Coverage 61.70% 61.71%
========================================
Files 81 81
Lines 12131 11967 -164
========================================
- Hits 7485 7385 -100
+ Misses 4179 4127 -52
+ Partials 467 455 -12
|
internal/gatewayapi/contexts.go
Outdated
| // corresponding to the const defined in translator, like KindHTTPRoute, | ||
| // KindGRPCRoute, KindTLSRoute, KindTCPRoute, KindUDPRoute | ||
| func GetRouteType(route RouteContext) v1beta1.Kind { | ||
| rv := reflect.ValueOf(route) |
There was a problem hiding this comment.
do you think reflect is the right package to use here or should we use https://pkg.go.dev/k8s.io/apimachinery/pkg/util/validation/field which is used heavily in upstream gateway api, here an
example
There was a problem hiding this comment.
thanks for pointing it out, but i think https://pkg.go.dev/k8s.io/apimachinery/pkg/util/validation/field is not suitable for our case.
despite the fact that this package provides several powerful methods, but its intention is for validations only. in our case, we need more than that, we need to write some values for some fields in one struct as well, but this package does not have that kind of ability.
i thought about whether we could replace current validations with the one provided in that package, and turns out we may not. for example, the strings.HasPrefix(*path.Value, "/") use the variable path directly, which has explicit type *gatewayv1b1.HTTPPathMatch. however, in our case, the only variable we can manipulate with is route, which is a interface RouteContext. so we need the ability provided in package reflect, to play with the real type behinds whatever it represents.
so basically, i think relfect is inevitable.
|
thanks for picking this refactor up @shawnh2 ! |
| for i := 0; i < specParentRefs.Len(); i++ { | ||
| p := specParentRefs.Index(i).Interface().(v1beta1.ParentReference) | ||
| up := p | ||
| if !isHTTPRoute { |
There was a problem hiding this comment.
curious why this custom HTTPRoute logic is needed
There was a problem hiding this comment.
becasue there are some differences between the implementation of HTTPRoute and other Routes in GetRouteParentContext method.
for example, HTTPRoute is like:
gateway/internal/gatewayapi/contexts.go
Lines 220 to 226 in 7f8a491
as for other Route:
gateway/internal/gatewayapi/contexts.go
Lines 471 to 479 in 7f8a491
so add a if logic to deal with HTTPRoute
internal/gatewayapi/contexts.go
Outdated
| func GetRouteType(route RouteContext) v1beta1.Kind { | ||
| rv := reflect.ValueOf(route) | ||
| rt := rv.Type().String() | ||
| return v1beta1.Kind(strings.TrimSuffix(rt, "Context")[strings.LastIndex(rt, ".")+1:]) |
There was a problem hiding this comment.
this looks a little intimidating to me, can we use FieldByName here ?
There was a problem hiding this comment.
sure we can use FieldByName, in that case, we need to add one more field, like type string in struct HTTP/GRPC/.../Context, indicating the type of this struct, and all the value of field type comes from:
gateway/internal/gatewayapi/translator.go
Lines 17 to 22 in 4dd4f87
@arkodg see if this is ok?
There was a problem hiding this comment.
or you could also use GetKind() ?
see it being used in other places in the codebase
gateway/internal/gatewayapi/filters.go
Line 769 in 4dd4f87
There was a problem hiding this comment.
thanks for the heads up!
now we can use FieldByName("Kind"), BTW, I add some TypeMeta for xRoute resources in translate func, nor it will keep getting "".
Signed-off-by: Shawnh2 <shawnhxh@outlook.com>
Signed-off-by: Shawnh2 <shawnhxh@outlook.com>
|
thanks @shawnh2 for addressing the comments, adding a minor comment, else LGTM ! |
Signed-off-by: Shawnh2 <shawnhxh@outlook.com>
What type of PR is this?
What this PR does / why we need it:
Which issue(s) this PR fixes:
Fixes #1252