Skip to content

Commit

Permalink
chore(refactor): streamline logic for HTTPRoute configuration generation
Browse files Browse the repository at this point in the history
  • Loading branch information
programmer04 committed Nov 28, 2024
1 parent 1745881 commit aa120a2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
24 changes: 20 additions & 4 deletions internal/admission/validation/gateway/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import (
"strings"

"github.com/kong/go-kong/kong"
"github.com/samber/lo"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kong/kubernetes-ingress-controller/v3/internal/admission/validation"
gatewaycontroller "github.com/kong/kubernetes-ingress-controller/v3/internal/controllers/gateway"
"github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/kongstate"
"github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/translator"
"github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/translator/subtranslator"
"github.com/kong/kubernetes-ingress-controller/v3/internal/gatewayapi"
"github.com/kong/kubernetes-ingress-controller/v3/internal/util"
)

type routeValidator interface {
Expand Down Expand Up @@ -196,11 +199,24 @@ func validateWithKongGateway(
Matches: rule.Matches,
Filters: rule.Filters,
}
routes, err := translator.GenerateKongRouteFromTranslation(
httproute, translation, translatorFeatures.ExpressionRoutes,
var (
routes []kongstate.Route
translationErr error
)
if err != nil {
errMsgs = append(errMsgs, err.Error())
if translatorFeatures.ExpressionRoutes {
routes, translationErr = subtranslator.GenerateKongExpressionRoutesFromTranslation(
translation,
util.FromK8sObject(httproute),
lo.Map(httproute.Spec.Hostnames, func(h gatewayapi.Hostname, _ int) string { return string(h) }),
nil, // Tags can be omitted for validation.
)
} else {
routes, translationErr = translator.GenerateKongRouteFromTranslation(
httproute, translation,
)
}
if translationErr != nil {
errMsgs = append(errMsgs, translationErr.Error())
continue
}
for _, r := range routes {
Expand Down
5 changes: 3 additions & 2 deletions internal/dataplane/translator/subtranslator/httproute_atc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (
"github.com/kong/kubernetes-ingress-controller/v3/internal/util"
)

// GenerateKongExpressionRoutesFromHTTPRouteMatches generates Kong routes from HTTPRouteRule
// GenerateKongExpressionRoutesFromTranslation generates Kong routes from HTTPRouteRule
// pointing to a specific backend.
func GenerateKongExpressionRoutesFromHTTPRouteMatches(
// NOTICE: currently it's used only for validation in internal/admission/validation/gateway/httproute.go file.
func GenerateKongExpressionRoutesFromTranslation(
translation KongRouteTranslation,
ingressObjectInfo util.K8sObjectInfo,
hostnames []string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func TestGenerateKongExpressionRoutesFromHTTPRouteMatches(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
routes, err := GenerateKongExpressionRoutesFromHTTPRouteMatches(
routes, err := GenerateKongExpressionRoutesFromTranslation(
KongRouteTranslation{
Name: tc.routeName,
Matches: tc.matches,
Expand Down
23 changes: 4 additions & 19 deletions internal/dataplane/translator/translate_httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (t *Translator) ingressRulesFromHTTPRoute(result *ingressRules, httproute *

// generate the routes for the service and attach them to the service
for _, kongRouteTranslation := range kongServiceTranslation.KongRoutes {
routes, err := GenerateKongRouteFromTranslation(httproute, kongRouteTranslation, t.featureFlags.ExpressionRoutes)
routes, err := GenerateKongRouteFromTranslation(httproute, kongRouteTranslation)
if err != nil {
return err
}
Expand Down Expand Up @@ -165,6 +165,7 @@ func validateHTTPRoute(httproute *gatewayapi.HTTPRoute, featureFlags FeatureFlag
// Because we need to assign different priorities based on the hostname and match in the specification of HTTPRoutes,
// We need to split the HTTPRoutes into ones with only one hostname and one match, then assign priority to them
// and finally translate the split HTTPRoutes into Kong services and routes with assigned priorities.
// XXX:aa.
func (t *Translator) ingressRulesFromHTTPRoutesUsingExpressionRoutes(httpRoutes []*gatewayapi.HTTPRoute, result *ingressRules) {
// first, split HTTPRoutes by hostnames and matches.
splitHTTPRouteMatches := []subtranslator.SplitHTTPRouteMatch{}
Expand Down Expand Up @@ -229,37 +230,21 @@ func getHTTPRouteHostnamesAsSliceOfStringPointers(httproute *gatewayapi.HTTPRout
}

// GenerateKongRouteFromTranslation generates Kong routes from HTTPRoute
// pointing to a specific backend. It is used for both traditional and expression based routes.
// pointing to a specific backend. It is used for traditional routes.
func GenerateKongRouteFromTranslation(
httproute *gatewayapi.HTTPRoute,
translation subtranslator.KongRouteTranslation,
expressionRoutes bool,
) ([]kongstate.Route, error) {
// gather the k8s object information and hostnames from the httproute
objectInfo := util.FromK8sObject(httproute)
tags := util.GenerateTagsForObject(httproute)

// translate to expression based routes when expressionRoutes is enabled.
if expressionRoutes {
// get the hostnames from the HTTPRoute
hostnames := getHTTPRouteHostnamesAsSliceOfStrings(httproute)
return subtranslator.GenerateKongExpressionRoutesFromHTTPRouteMatches(
translation,
objectInfo,
hostnames,
tags,
)
}

// get the hostnames from the HTTPRoute
hostnames := getHTTPRouteHostnamesAsSliceOfStringPointers(httproute)

return generateKongRoutesFromHTTPRouteMatches(
translation.Name,
translation.Matches,
translation.Filters,
objectInfo,
hostnames,
getHTTPRouteHostnamesAsSliceOfStringPointers(httproute),
tags,
)
}
Expand Down

0 comments on commit aa120a2

Please sign in to comment.