Skip to content

Commit

Permalink
refactor: api machinary (#396)
Browse files Browse the repository at this point in the history
* refactor: object utils

* refactor: slice utils

* refactor: referrer interface

* refactor: event mappers

* refactor: use referrer interface, gateway diff and gateway wrapper

* refactor: use gatewayapiv1.GatewayConditionProgrammed instead

* refactor: move direct ref annotation to Referrer interface

* test: event mappers

* refactor: ValidateHierarchicalRules tests

* test: increase fetcher coverage

* refactor: use gatewayapiv1.GroupName

* refactor: fix redundant package rename

* refactor: package import naming & use consts for reference annotation strings

* refactor: kuadrant package in library package
  • Loading branch information
KevFan authored Feb 28, 2024
1 parent 93d2265 commit e51ddaf
Show file tree
Hide file tree
Showing 84 changed files with 3,190 additions and 2,577 deletions.
16 changes: 14 additions & 2 deletions api/v1alpha1/dnspolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

kuadrantdnsv1alpha1 "github.com/kuadrant/dns-operator/api/v1alpha1"

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

type RoutingStrategy string
Expand All @@ -38,6 +38,9 @@ const (
DefaultWeight Weight = 120
DefaultGeo GeoCode = "default"
WildcardGeo GeoCode = "*"

DNSPolicyBackReferenceAnnotationName = "kuadrant.io/dnspolicies"
DNSPolicyDirectReferenceAnnotationName = "kuadrant.io/dnspolicy"
)

// DNSPolicySpec defines the desired state of DNSPolicy
Expand Down Expand Up @@ -126,7 +129,8 @@ type DNSPolicyStatus struct {
HealthCheck *HealthCheckStatus `json:"healthCheck,omitempty"`
}

var _ common.KuadrantPolicy = &DNSPolicy{}
var _ kuadrant.Policy = &DNSPolicy{}
var _ kuadrant.Referrer = &DNSPolicy{}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
Expand Down Expand Up @@ -159,6 +163,14 @@ func (p *DNSPolicy) GetTargetRef() gatewayapiv1alpha2.PolicyTargetReference {

func (p *DNSPolicy) Kind() string { return p.TypeMeta.Kind }

func (p *DNSPolicy) BackReferenceAnnotationName() string {
return DNSPolicyBackReferenceAnnotationName
}

func (p *DNSPolicy) DirectReferenceAnnotationName() string {
return DNSPolicyDirectReferenceAnnotationName
}

// Validate ensures the resource is valid. Compatible with the validating interface
// used by webhooks
func (p *DNSPolicy) Validate() error {
Expand Down
18 changes: 16 additions & 2 deletions api/v1alpha1/tlspolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ import (
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

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

const (
TLSPolicyBackReferenceAnnotationName = "kuadrant.io/tlspolicies"
TLSPolicyDirectReferenceAnnotationName = "kuadrant.io/tlspolicy"
)

// TLSPolicySpec defines the desired state of TLSPolicy
Expand Down Expand Up @@ -111,7 +116,8 @@ type TLSPolicyStatus struct {
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
}

var _ common.KuadrantPolicy = &TLSPolicy{}
var _ kuadrant.Policy = &TLSPolicy{}
var _ kuadrant.Referrer = &TLSPolicy{}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
Expand Down Expand Up @@ -144,6 +150,14 @@ func (p *TLSPolicy) GetTargetRef() gatewayapiv1alpha2.PolicyTargetReference {
return p.Spec.TargetRef
}

func (p *TLSPolicy) BackReferenceAnnotationName() string {
return TLSPolicyBackReferenceAnnotationName
}

func (p *TLSPolicy) DirectReferenceAnnotationName() string {
return TLSPolicyDirectReferenceAnnotationName
}

func (p *TLSPolicy) Validate() error {
if p.Spec.TargetRef.Group != (gatewayapiv1.GroupName) {
return fmt.Errorf("invalid targetRef.Group %s. The only supported group is %s", p.Spec.TargetRef.Group, gatewayapiv1.GroupName)
Expand Down
6 changes: 3 additions & 3 deletions api/v1beta1/kuadrant_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

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

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand Down Expand Up @@ -76,8 +76,8 @@ func (r *KuadrantStatus) Equals(other *KuadrantStatus, logger logr.Logger) bool
}

// Marshalling sorts by condition type
currentMarshaledJSON, _ := common.ConditionMarshal(r.Conditions)
otherMarshaledJSON, _ := common.ConditionMarshal(other.Conditions)
currentMarshaledJSON, _ := kuadrant.ConditionMarshal(r.Conditions)
otherMarshaledJSON, _ := kuadrant.ConditionMarshal(other.Conditions)
if string(currentMarshaledJSON) != string(otherMarshaledJSON) {
diff := cmp.Diff(string(currentMarshaledJSON), string(otherMarshaledJSON))
logger.V(1).Info("Conditions not equal", "difference", diff)
Expand Down
31 changes: 23 additions & 8 deletions api/v1beta2/authpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import (

"github.com/go-logr/logr"
"github.com/google/go-cmp/cmp"
authorinoapi "github.com/kuadrant/authorino/api/v1beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

authorinoapi "github.com/kuadrant/authorino/api/v1beta2"
"github.com/kuadrant/kuadrant-operator/pkg/common"
"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant"
"github.com/kuadrant/kuadrant-operator/pkg/library/utils"
)

const (
AuthPolicyBackReferenceAnnotationName = "kuadrant.io/authpolicies"
AuthPolicyDirectReferenceAnnotationName = "kuadrant.io/authpolicy"
)

type AuthSchemeSpec struct {
Expand Down Expand Up @@ -186,8 +192,8 @@ func (s *AuthPolicyStatus) Equals(other *AuthPolicyStatus, logger logr.Logger) b
}

// Marshalling sorts by condition type
currentMarshaledJSON, _ := common.ConditionMarshal(s.Conditions)
otherMarshaledJSON, _ := common.ConditionMarshal(other.Conditions)
currentMarshaledJSON, _ := kuadrant.ConditionMarshal(s.Conditions)
otherMarshaledJSON, _ := kuadrant.ConditionMarshal(other.Conditions)
if string(currentMarshaledJSON) != string(otherMarshaledJSON) {
diff := cmp.Diff(string(currentMarshaledJSON), string(otherMarshaledJSON))
logger.V(1).Info("Conditions not equal", "difference", diff)
Expand All @@ -197,7 +203,8 @@ func (s *AuthPolicyStatus) Equals(other *AuthPolicyStatus, logger logr.Logger) b
return true
}

var _ common.KuadrantPolicy = &AuthPolicy{}
var _ kuadrant.Policy = &AuthPolicy{}
var _ kuadrant.Referrer = &AuthPolicy{}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
Expand Down Expand Up @@ -251,7 +258,7 @@ func (ap *AuthPolicy) GetRulesHostnames() (ruleHosts []string) {

appendRuleHosts := func(obj RouteSelectorsGetter) {
for _, routeSelector := range obj.GetRouteSelectors() {
ruleHosts = append(ruleHosts, common.HostnamesToStrings(routeSelector.Hostnames)...)
ruleHosts = append(ruleHosts, utils.HostnamesToStrings(routeSelector.Hostnames)...)
}
}

Expand Down Expand Up @@ -284,6 +291,14 @@ func (ap *AuthPolicy) Kind() string {
return ap.TypeMeta.Kind
}

func (ap *AuthPolicy) BackReferenceAnnotationName() string {
return AuthPolicyBackReferenceAnnotationName
}

func (ap *AuthPolicy) DirectReferenceAnnotationName() string {
return AuthPolicyDirectReferenceAnnotationName
}

//+kubebuilder:object:root=true

// AuthPolicyList contains a list of AuthPolicy
Expand All @@ -293,8 +308,8 @@ type AuthPolicyList struct {
Items []AuthPolicy `json:"items"`
}

func (l *AuthPolicyList) GetItems() []common.KuadrantPolicy {
return common.Map(l.Items, func(item AuthPolicy) common.KuadrantPolicy {
func (l *AuthPolicyList) GetItems() []kuadrant.Policy {
return utils.Map(l.Items, func(item AuthPolicy) kuadrant.Policy {
return &item
})
}
Expand Down
14 changes: 7 additions & 7 deletions api/v1beta2/authpolicy_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"reflect"
"testing"

authorinoapi "github.com/kuadrant/authorino/api/v1beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

authorinoapi "github.com/kuadrant/authorino/api/v1beta2"
"github.com/kuadrant/kuadrant-operator/pkg/common"
"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant"
)

func TestCommonAuthRuleSpecGetRouteSelectors(t *testing.T) {
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestAuthPolicyTargetKey(t *testing.T) {
},
Spec: AuthPolicySpec{
TargetRef: gatewayapiv1alpha2.PolicyTargetReference{
Group: "gateway.networking.k8s.io",
Group: gatewayapiv1.GroupName,
Kind: "HTTPRoute",
Name: "my-route",
},
Expand Down Expand Up @@ -86,9 +86,9 @@ func TestAuthPolicyListGetItems(t *testing.T) {
if len(result) != 1 {
t.Errorf("Expected 1 item, got %d", len(result))
}
_, ok := result[0].(common.KuadrantPolicy)
_, ok := result[0].(kuadrant.Policy)
if !ok {
t.Errorf("Expected item to be a KuadrantPolicy")
t.Errorf("Expected item to be a Policy")
}
}

Expand All @@ -100,7 +100,7 @@ func TestAuthPolicyGetRulesHostnames(t *testing.T) {
},
Spec: AuthPolicySpec{
TargetRef: gatewayapiv1alpha2.PolicyTargetReference{
Group: "gateway.networking.k8s.io",
Group: gatewayapiv1.GroupName,
Kind: "HTTPRoute",
Name: "my-route",
},
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestAuthPolicyValidate(t *testing.T) {
},
Spec: AuthPolicySpec{
TargetRef: gatewayapiv1alpha2.PolicyTargetReference{
Group: "gateway.networking.k8s.io",
Group: gatewayapiv1.GroupName,
Kind: "HTTPRoute",
Name: "my-route",
Namespace: ptr.To(gatewayapiv1.Namespace("other-namespace")),
Expand Down
28 changes: 21 additions & 7 deletions api/v1beta2/ratelimitpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (

"github.com/go-logr/logr"
"github.com/google/go-cmp/cmp"
"github.com/kuadrant/kuadrant-operator/pkg/common"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

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

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand All @@ -51,6 +53,9 @@ const (
IncludeOperator WhenConditionOperator = "incl"
ExcludeOperator WhenConditionOperator = "excl"
MatchesOperator WhenConditionOperator = "matches"

RateLimitPolicyBackReferenceAnnotationName = "kuadrant.io/ratelimitpolicies"
RateLimitPolicyDirectReferenceAnnotationName = "kuadrant.io/ratelimitpolicy"
)

// +kubebuilder:validation:Enum:=second;minute;hour;day
Expand Down Expand Up @@ -110,7 +115,7 @@ func (l Limit) CountersAsStringList() []string {
if len(l.Counters) == 0 {
return nil
}
return common.Map(l.Counters, func(counter ContextSelector) string { return string(counter) })
return utils.Map(l.Counters, func(counter ContextSelector) string { return string(counter) })
}

// RateLimitPolicySpec defines the desired state of RateLimitPolicy
Expand Down Expand Up @@ -150,8 +155,8 @@ func (s *RateLimitPolicyStatus) Equals(other *RateLimitPolicyStatus, logger logr
}

// Marshalling sorts by condition type
currentMarshaledJSON, _ := common.ConditionMarshal(s.Conditions)
otherMarshaledJSON, _ := common.ConditionMarshal(other.Conditions)
currentMarshaledJSON, _ := kuadrant.ConditionMarshal(s.Conditions)
otherMarshaledJSON, _ := kuadrant.ConditionMarshal(other.Conditions)
if string(currentMarshaledJSON) != string(otherMarshaledJSON) {
if logger.V(1).Enabled() {
diff := cmp.Diff(string(currentMarshaledJSON), string(otherMarshaledJSON))
Expand All @@ -163,7 +168,8 @@ func (s *RateLimitPolicyStatus) Equals(other *RateLimitPolicyStatus, logger logr
return true
}

var _ common.KuadrantPolicy = &RateLimitPolicy{}
var _ kuadrant.Policy = &RateLimitPolicy{}
var _ kuadrant.Referrer = &RateLimitPolicy{}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
Expand Down Expand Up @@ -212,8 +218,8 @@ type RateLimitPolicyList struct {
Items []RateLimitPolicy `json:"items"`
}

func (l *RateLimitPolicyList) GetItems() []common.KuadrantPolicy {
return common.Map(l.Items, func(item RateLimitPolicy) common.KuadrantPolicy {
func (l *RateLimitPolicyList) GetItems() []kuadrant.Policy {
return utils.Map(l.Items, func(item RateLimitPolicy) kuadrant.Policy {
return &item
})
}
Expand Down Expand Up @@ -247,6 +253,14 @@ func (r *RateLimitPolicy) Kind() string {
return r.TypeMeta.Kind
}

func (r *RateLimitPolicy) BackReferenceAnnotationName() string {
return RateLimitPolicyBackReferenceAnnotationName
}

func (r *RateLimitPolicy) DirectReferenceAnnotationName() string {
return RateLimitPolicyDirectReferenceAnnotationName
}

func init() {
SchemeBuilder.Register(&RateLimitPolicy{}, &RateLimitPolicyList{})
}
8 changes: 4 additions & 4 deletions api/v1beta2/ratelimitpolicy_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

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

func testBuildBasicRLP(name string, kind gatewayapiv1.Kind) *RateLimitPolicy {
Expand All @@ -25,7 +25,7 @@ func testBuildBasicRLP(name string, kind gatewayapiv1.Kind) *RateLimitPolicy {
},
Spec: RateLimitPolicySpec{
TargetRef: gatewayapiv1alpha2.PolicyTargetReference{
Group: "gateway.networking.k8s.io",
Group: gatewayapiv1.GroupName,
Kind: kind,
Name: "some-name",
},
Expand Down Expand Up @@ -70,8 +70,8 @@ func TestRateLimitPolicyListGetItems(t *testing.T) {
if len(result) != 1 {
t.Errorf("Expected 1 item, got %d", len(result))
}
_, ok := result[0].(common.KuadrantPolicy)
_, ok := result[0].(kuadrant.Policy)
if !ok {
t.Errorf("Expected item to be a KuadrantPolicy")
t.Errorf("Expected item to be a Policy")
}
}
14 changes: 7 additions & 7 deletions api/v1beta2/route_selectors.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package v1beta2

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

orderedmap "github.com/elliotchance/orderedmap/v2"

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

// RouteSelector defines semantics for matching an HTTP request based on conditions
Expand All @@ -32,7 +32,7 @@ type RouteSelector struct {
// returns nil.
func (s *RouteSelector) SelectRules(route *gatewayapiv1.HTTPRoute) (rules []gatewayapiv1.HTTPRouteRule) {
rulesIndices := orderedmap.NewOrderedMap[int, gatewayapiv1.HTTPRouteRule]()
if len(s.Hostnames) > 0 && !common.Intersect(s.Hostnames, route.Spec.Hostnames) {
if len(s.Hostnames) > 0 && !utils.Intersect(s.Hostnames, route.Spec.Hostnames) {
return nil
}
if len(s.Matches) == 0 {
Expand All @@ -41,7 +41,7 @@ func (s *RouteSelector) SelectRules(route *gatewayapiv1.HTTPRoute) (rules []gate
for idx := range s.Matches {
routeSelectorMatch := s.Matches[idx]
for idx, rule := range route.Spec.Rules {
rs := common.HTTPRouteRuleSelector{HTTPRouteMatch: &routeSelectorMatch}
rs := kuadrant.HTTPRouteRuleSelector{HTTPRouteMatch: &routeSelectorMatch}
if rs.Selects(rule) {
rulesIndices.Set(idx, rule)
}
Expand All @@ -59,10 +59,10 @@ func (s *RouteSelector) HostnamesForConditions(route *gatewayapiv1.HTTPRoute) []
hostnames := route.Spec.Hostnames

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

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

Expand Down
Loading

0 comments on commit e51ddaf

Please sign in to comment.