Skip to content

Commit

Permalink
route selectors moved back to v1beta2
Browse files Browse the repository at this point in the history
Signed-off-by: Guilherme Cassolato <[email protected]>
  • Loading branch information
guicassolato committed Oct 2, 2024
1 parent bfe0026 commit b5bd72f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 111 deletions.
68 changes: 65 additions & 3 deletions api/v1beta2/route_selectors.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,73 @@
package v1beta2

import (
kuadrantv1beta3 "github.com/kuadrant/kuadrant-operator/api/v1beta3"
"github.com/elliotchance/orderedmap/v2"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"

"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant"
"github.com/kuadrant/kuadrant-operator/pkg/library/utils"
)

// +kubebuilder:object:generate=false
type RouteSelector = kuadrantv1beta3.RouteSelector
// RouteSelector defines semantics for matching an HTTP request based on conditions
// https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.HTTPRouteSpec
type RouteSelector struct {
// Hostnames defines a set of hostname that should match against the HTTP Host header to select a HTTPRoute to process the request
// https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.HTTPRouteSpec
// +optional
Hostnames []gatewayapiv1.Hostname `json:"hostnames,omitempty"`

// Matches define conditions used for matching the rule against incoming HTTP requests.
// https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.HTTPRouteSpec
// +optional
// +kubebuilder:validation:MaxItems=8
Matches []gatewayapiv1.HTTPRouteMatch `json:"matches,omitempty"`
}

// SelectRules returns, from a HTTPRoute, all HTTPRouteRules that either specify no HTTRouteMatches or that contain at
// least one HTTRouteMatch whose statements expressly include (partially or totally) the statements of at least one of
// the matches of the selector. If the selector does not specify any matches, then all HTTPRouteRules are selected.
//
// Additionally, if the selector specifies a non-empty list of hostnames, a non-empty intersection between the literal
// hostnames of the selector and set of hostnames specified in the HTTPRoute must exist. Otherwise, the function
// returns nil.
func (s *RouteSelector) SelectRules(route *gatewayapiv1.HTTPRoute) (rules []gatewayapiv1.HTTPRouteRule) {
rulesIndices := orderedmap.NewOrderedMap[int, gatewayapiv1.HTTPRouteRule]()
if len(s.Hostnames) > 0 && !utils.Intersect(s.Hostnames, route.Spec.Hostnames) {
return nil

Check warning on line 36 in api/v1beta2/route_selectors.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta2/route_selectors.go#L33-L36

Added lines #L33 - L36 were not covered by tests
}
if len(s.Matches) == 0 {
return route.Spec.Rules

Check warning on line 39 in api/v1beta2/route_selectors.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta2/route_selectors.go#L38-L39

Added lines #L38 - L39 were not covered by tests
}
for idx := range s.Matches {
routeSelectorMatch := s.Matches[idx]
for idx, rule := range route.Spec.Rules {
rs := kuadrant.HTTPRouteRuleSelector{HTTPRouteMatch: &routeSelectorMatch}
if rs.Selects(rule) {
rulesIndices.Set(idx, rule)

Check warning on line 46 in api/v1beta2/route_selectors.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta2/route_selectors.go#L41-L46

Added lines #L41 - L46 were not covered by tests
}
}
}
for el := rulesIndices.Front(); el != nil; el = el.Next() {
rules = append(rules, el.Value)

Check warning on line 51 in api/v1beta2/route_selectors.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta2/route_selectors.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}
return

Check warning on line 53 in api/v1beta2/route_selectors.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta2/route_selectors.go#L53

Added line #L53 was not covered by tests
}

// HostnamesForConditions allows avoiding building conditions for hostnames that are excluded by the selector
// or when the hostname is irrelevant (i.e. matches all hostnames)
func (s *RouteSelector) HostnamesForConditions(route *gatewayapiv1.HTTPRoute) []gatewayapiv1.Hostname {
hostnames := route.Spec.Hostnames

Check warning on line 59 in api/v1beta2/route_selectors.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta2/route_selectors.go#L58-L59

Added lines #L58 - L59 were not covered by tests

if len(s.Hostnames) > 0 {
hostnames = utils.Intersection(s.Hostnames, hostnames)

Check warning on line 62 in api/v1beta2/route_selectors.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta2/route_selectors.go#L61-L62

Added lines #L61 - L62 were not covered by tests
}

if utils.SameElements(hostnames, route.Spec.Hostnames) {
return []gatewayapiv1.Hostname{"*"}

Check warning on line 66 in api/v1beta2/route_selectors.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta2/route_selectors.go#L65-L66

Added lines #L65 - L66 were not covered by tests
}

return hostnames

Check warning on line 69 in api/v1beta2/route_selectors.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta2/route_selectors.go#L69

Added line #L69 was not covered by tests
}

// +kubebuilder:object:generate=false
type RouteSelectorsGetter interface {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build unit

package v1beta3
package v1beta2

import (
"fmt"
Expand Down
33 changes: 30 additions & 3 deletions api/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 0 additions & 75 deletions api/v1beta3/route_selectors.go

This file was deleted.

28 changes: 0 additions & 28 deletions api/v1beta3/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/rlptools/wasm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"

kuadrantv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2"
kuadrantv1beta3 "github.com/kuadrant/kuadrant-operator/api/v1beta3"
"github.com/kuadrant/kuadrant-operator/pkg/common"
kuadrantgatewayapi "github.com/kuadrant/kuadrant-operator/pkg/library/gatewayapi"
Expand Down Expand Up @@ -117,7 +118,7 @@ func conditionsFromLimit(limit *kuadrantv1beta3.Limit, route *gatewayapiv1.HTTPR
routeConditions := make([]Condition, 0)

// build conditions from all rules if no route selectors are defined
hostnamesForConditions := (&kuadrantv1beta3.RouteSelector{}).HostnamesForConditions(route)
hostnamesForConditions := (&kuadrantv1beta2.RouteSelector{}).HostnamesForConditions(route)
for _, rule := range route.Spec.Rules {
routeConditions = append(routeConditions, conditionsFromRule(rule, hostnamesForConditions)...)

Check warning on line 123 in pkg/rlptools/wasm/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/rlptools/wasm/utils.go#L121-L123

Added lines #L121 - L123 were not covered by tests
}
Expand Down

0 comments on commit b5bd72f

Please sign in to comment.