Skip to content

Commit

Permalink
remove routeSelectors from v1beta3
Browse files Browse the repository at this point in the history
Signed-off-by: Eguzki Astiz Lezaun <[email protected]>
  • Loading branch information
eguzki committed Oct 4, 2024
1 parent ca6f6a0 commit 57d555b
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 2,277 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
}
if len(s.Matches) == 0 {
return route.Spec.Rules
}
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)
}
}
}
for el := rulesIndices.Front(); el != nil; el = el.Next() {
rules = append(rules, el.Value)
}
return
}

// 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

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

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

return hostnames
}

// +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
4 changes: 2 additions & 2 deletions api/v1beta2/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

var (
AuthPoliciesResource = GroupVersion.WithResource("authpolicies")
AuthPolicyGroupKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "AuthPolicy"}
AuthPoliciesResource = GroupVersion.WithResource("authpolicies")
AuthPolicyGroupKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "AuthPolicy"}
)

var _ machinery.Policy = &AuthPolicy{}
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.

20 changes: 1 addition & 19 deletions api/v1beta3/ratelimitpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type Rate struct {
Unit TimeUnit `json:"unit"`
}

// RouteSelector defines semantics for matching an HTTP request based on conditions
// WhenCondition 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 WhenCondition struct {
// Selector defines one item from the well known selectors
Expand All @@ -104,11 +104,6 @@ type WhenCondition struct {

// Limit represents a complete rate limit configuration
type Limit struct {
// RouteSelectors defines semantics for matching an HTTP request based on conditions
// +optional
// +kubebuilder:validation:MaxItems=15
RouteSelectors []RouteSelector `json:"routeSelectors,omitempty"`

// When holds the list of conditions for the policy to be enforced.
// Called also "soft" conditions as route selectors must also match
// +optional
Expand All @@ -132,7 +127,6 @@ func (l Limit) CountersAsStringList() []string {
}

// RateLimitPolicySpec defines the desired state of RateLimitPolicy
// +kubebuilder:validation:XValidation:rule="self.targetRef.kind != 'Gateway' || !has(self.limits) || !self.limits.exists(x, has(self.limits[x].routeSelectors))",message="route selectors not supported when targeting a Gateway"
// +kubebuilder:validation:XValidation:rule="!(has(self.defaults) && has(self.limits))",message="Implicit and explicit defaults are mutually exclusive"
// +kubebuilder:validation:XValidation:rule="!(has(self.defaults) && has(self.overrides))",message="Overrides and explicit defaults are mutually exclusive"
// +kubebuilder:validation:XValidation:rule="!(has(self.overrides) && has(self.limits))",message="Overrides and implicit defaults are mutually exclusive"
Expand Down Expand Up @@ -266,18 +260,6 @@ func (r *RateLimitPolicy) GetWrappedNamespace() gatewayapiv1.Namespace {

func (r *RateLimitPolicy) GetRulesHostnames() (ruleHosts []string) {
ruleHosts = make([]string, 0)
for _, limit := range r.Spec.CommonSpec().Limits {
for _, routeSelector := range limit.RouteSelectors {
convertHostnamesToString := func(gwHostnames []gatewayapiv1.Hostname) []string {
hostnames := make([]string, 0, len(gwHostnames))
for _, gwHostName := range gwHostnames {
hostnames = append(hostnames, string(gwHostName))
}
return hostnames
}
ruleHosts = append(ruleHosts, convertHostnamesToString(routeSelector.Hostnames)...)
}
}
return
}

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

This file was deleted.

2 changes: 1 addition & 1 deletion api/v1beta3/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var (
RateLimitPoliciesResource = GroupVersion.WithResource("ratelimitpolicies")
RateLimitPolicyKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "RateLimitPolicy"}
RateLimitPolicyGroupKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "RateLimitPolicy"}
)

var _ machinery.Policy = &RateLimitPolicy{}
Expand Down
35 changes: 0 additions & 35 deletions api/v1beta3/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ metadata:
capabilities: Basic Install
categories: Integration & Delivery
containerImage: quay.io/kuadrant/kuadrant-operator:latest
createdAt: "2024-10-04T07:47:31Z"
createdAt: "2024-10-04T10:37:57Z"
description: A Kubernetes Operator to manage the lifecycle of the Kuadrant system
operators.operatorframework.io/builder: operator-sdk-v1.32.0
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
Expand Down
Loading

0 comments on commit 57d555b

Please sign in to comment.