-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
address comments and move GenerateKongRoutesFromGRPCRouteRule to tran…
…slators
- Loading branch information
1 parent
ff058a0
commit 3696668
Showing
5 changed files
with
334 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package translators | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kong/go-kong/kong" | ||
"github.com/samber/lo" | ||
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" | ||
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" | ||
|
||
"github.com/kong/kubernetes-ingress-controller/v2/internal/dataplane/kongstate" | ||
"github.com/kong/kubernetes-ingress-controller/v2/internal/util" | ||
) | ||
|
||
func getGRPCMatchDefaults() ( | ||
map[gatewayv1alpha2.GRPCMethodMatchType]string, | ||
map[gatewayv1alpha2.GRPCMethodMatchType]string, | ||
) { | ||
// Kong routes derived from a GRPCRoute use a path composed of the match's gRPC service and method | ||
// If either the service or method is omitted, there is a default regex determined by the match type | ||
// https://gateway-api.sigs.k8s.io/geps/gep-1016/#matcher-types describes the defaults | ||
|
||
// default path components for the GRPC service | ||
return map[gatewayv1alpha2.GRPCMethodMatchType]string{ | ||
gatewayv1alpha2.GRPCMethodMatchType(""): ".+", | ||
gatewayv1alpha2.GRPCMethodMatchExact: ".+", | ||
gatewayv1alpha2.GRPCMethodMatchRegularExpression: ".+", | ||
}, | ||
// default path components for the GRPC method | ||
map[gatewayv1alpha2.GRPCMethodMatchType]string{ | ||
gatewayv1alpha2.GRPCMethodMatchType(""): "", | ||
gatewayv1alpha2.GRPCMethodMatchExact: "", | ||
gatewayv1alpha2.GRPCMethodMatchRegularExpression: ".+", | ||
} | ||
} | ||
|
||
func GenerateKongRoutesFromGRPCRouteRule(grpcroute *gatewayv1alpha2.GRPCRoute, ruleNumber int) []kongstate.Route { | ||
if ruleNumber >= len(grpcroute.Spec.Rules) { | ||
return nil | ||
} | ||
rule := grpcroute.Spec.Rules[ruleNumber] | ||
|
||
routes := make([]kongstate.Route, 0, len(rule.Matches)) | ||
// gather the k8s object information and hostnames from the grpcroute | ||
ingressObjectInfo := util.FromK8sObject(grpcroute) | ||
|
||
for matchNumber, match := range rule.Matches { | ||
routeName := fmt.Sprintf( | ||
"grpcroute.%s.%s.%d.%d", | ||
grpcroute.Namespace, | ||
grpcroute.Name, | ||
ruleNumber, | ||
matchNumber, | ||
) | ||
|
||
r := kongstate.Route{ | ||
Ingress: ingressObjectInfo, | ||
Route: kong.Route{ | ||
Name: kong.String(routeName), | ||
Protocols: kong.StringSlice("grpc", "grpcs"), | ||
}, | ||
} | ||
|
||
if match.Method != nil { | ||
serviceMap, methodMap := getGRPCMatchDefaults() | ||
var method, service string | ||
matchMethod := match.Method.Method | ||
matchService := match.Method.Service | ||
var matchType gatewayv1alpha2.GRPCMethodMatchType | ||
if match.Method.Type == nil { | ||
matchType = gatewayv1alpha2.GRPCMethodMatchExact | ||
} else { | ||
matchType = *match.Method.Type | ||
} | ||
if matchMethod == nil { | ||
method = methodMap[matchType] | ||
} else { | ||
method = *matchMethod | ||
} | ||
if matchService == nil { | ||
service = serviceMap[matchType] | ||
} else { | ||
service = *matchService | ||
} | ||
r.Paths = append(r.Paths, kong.String(fmt.Sprintf("~/%s/%s", service, method))) | ||
} | ||
|
||
if len(grpcroute.Spec.Hostnames) > 0 { | ||
r.Hosts = getGRPCRouteHostnamesAsSliceOfStringPointers(grpcroute) | ||
} | ||
|
||
r.Headers = map[string][]string{} | ||
for _, hmatch := range match.Headers { | ||
name := string(hmatch.Name) | ||
r.Headers[name] = append(r.Headers[name], hmatch.Value) | ||
} | ||
|
||
routes = append(routes, r) | ||
} | ||
|
||
return routes | ||
} | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Translate GRPCRoute - Utils | ||
// ----------------------------------------------------------------------------- | ||
|
||
// getGRPCRouteHostnamesAsSliceOfStringPointers translates the hostnames defined | ||
// in an GRPCRoute specification into a []*string slice, which is the type required | ||
// by kong.Route{}. | ||
func getGRPCRouteHostnamesAsSliceOfStringPointers(grpcroute *gatewayv1alpha2.GRPCRoute) []*string { | ||
return lo.Map(grpcroute.Spec.Hostnames, func(h gatewayv1beta1.Hostname, _ int) *string { | ||
return lo.ToPtr(string(h)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.