Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: api machinary #396

Merged
merged 14 commits into from
Feb 28, 2024
Merged
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/utils"
)

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 _ utils.KuadrantPolicy = &DNSPolicy{}
var _ utils.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/utils"
)

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 _ utils.KuadrantPolicy = &TLSPolicy{}
var _ utils.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/utils"
)

// 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, _ := utils.ConditionMarshal(r.Conditions)
otherMarshaledJSON, _ := utils.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
30 changes: 22 additions & 8 deletions api/v1beta2/authpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ 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/utils"
)

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

type AuthSchemeSpec struct {
Expand Down Expand Up @@ -186,8 +191,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, _ := utils.ConditionMarshal(s.Conditions)
otherMarshaledJSON, _ := utils.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 +202,8 @@ func (s *AuthPolicyStatus) Equals(other *AuthPolicyStatus, logger logr.Logger) b
return true
}

var _ common.KuadrantPolicy = &AuthPolicy{}
var _ utils.KuadrantPolicy = &AuthPolicy{}
var _ utils.Referrer = &AuthPolicy{}
KevFan marked this conversation as resolved.
Show resolved Hide resolved

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
Expand Down Expand Up @@ -251,7 +257,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 +290,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 +307,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() []utils.KuadrantPolicy {
return utils.Map(l.Items, func(item AuthPolicy) utils.KuadrantPolicy {
return &item
})
}
Expand Down
12 changes: 6 additions & 6 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/utils"
)

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,7 +86,7 @@ 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].(utils.KuadrantPolicy)
if !ok {
t.Errorf("Expected item to be a KuadrantPolicy")
}
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
27 changes: 20 additions & 7 deletions api/v1beta2/ratelimitpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@

"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/utils"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand All @@ -51,6 +52,9 @@
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 +114,7 @@
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 +154,8 @@
}

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

var _ common.KuadrantPolicy = &RateLimitPolicy{}
var _ utils.KuadrantPolicy = &RateLimitPolicy{}
var _ utils.Referrer = &RateLimitPolicy{}

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

func (l *RateLimitPolicyList) GetItems() []common.KuadrantPolicy {
return common.Map(l.Items, func(item RateLimitPolicy) common.KuadrantPolicy {
func (l *RateLimitPolicyList) GetItems() []utils.KuadrantPolicy {
return utils.Map(l.Items, func(item RateLimitPolicy) utils.KuadrantPolicy {

Check warning on line 221 in api/v1beta2/ratelimitpolicy_types.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta2/ratelimitpolicy_types.go#L220-L221

Added lines #L220 - L221 were not covered by tests
return &item
})
}
Expand Down Expand Up @@ -247,6 +252,14 @@
return r.TypeMeta.Kind
}

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

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

func init() {
SchemeBuilder.Register(&RateLimitPolicy{}, &RateLimitPolicyList{})
}
6 changes: 3 additions & 3 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/utils"
)

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,7 +70,7 @@ 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].(utils.KuadrantPolicy)
if !ok {
t.Errorf("Expected item to be a KuadrantPolicy")
}
Expand Down
13 changes: 6 additions & 7 deletions api/v1beta2/route_selectors.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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/utils"
)

// RouteSelector defines semantics for matching an HTTP request based on conditions
Expand All @@ -32,7 +31,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 +40,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 := utils.HTTPRouteRuleSelector{HTTPRouteMatch: &routeSelectorMatch}
if rs.Selects(rule) {
rulesIndices.Set(idx, rule)
}
Expand All @@ -59,10 +58,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
Loading