Skip to content

Commit

Permalink
Object types (#777)
Browse files Browse the repository at this point in the history
* unittests verbose flag as opt-in

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

* policy types, gateway api types

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

---------

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>
  • Loading branch information
eguzki authored Jul 26, 2024
1 parent ef9c809 commit c1fb9b4
Show file tree
Hide file tree
Showing 20 changed files with 425 additions and 204 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,12 @@ clean-cov: ## Remove coverage reports
ifdef TEST_NAME
test-unit: TEST_PATTERN := --run $(TEST_NAME)
endif
ifdef VERBOSE
test-unit: VERBOSE_FLAG = -v
endif
test-unit: clean-cov generate fmt vet ## Run Unit tests.
mkdir -p $(PROJECT_PATH)/coverage/unit
go test $(UNIT_DIRS) -coverprofile $(PROJECT_PATH)/coverage/unit/cover.out -tags unit -v -timeout 0 $(TEST_PATTERN)
go test $(UNIT_DIRS) -coverprofile $(PROJECT_PATH)/coverage/unit/cover.out -tags unit $(VERBOSE_FLAG) -timeout 0 $(TEST_PATTERN)

##@ Build

Expand Down
66 changes: 49 additions & 17 deletions api/v1alpha1/dnspolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
Expand All @@ -32,6 +33,14 @@ import (
"github.com/kuadrant/kuadrant-operator/pkg/library/utils"
)

var (
DNSPolicyGVK schema.GroupVersionKind = schema.GroupVersionKind{
Group: GroupVersion.Group,
Version: GroupVersion.Version,
Kind: "DNSPolicy",
}
)

type RoutingStrategy string

const (
Expand Down Expand Up @@ -180,21 +189,8 @@ func (p *DNSPolicy) GetStatus() kuadrantgatewayapi.PolicyStatus {
return &p.Status
}

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

func (p *DNSPolicy) List(ctx context.Context, c client.Client, namespace string) []kuadrantgatewayapi.Policy {
policyList := &DNSPolicyList{}
listOptions := &client.ListOptions{Namespace: namespace}
err := c.List(ctx, policyList, listOptions)
if err != nil {
return []kuadrantgatewayapi.Policy{}
}
policies := make([]kuadrantgatewayapi.Policy, 0, len(policyList.Items))
for i := range policyList.Items {
policies = append(policies, &policyList.Items[i])
}

return policies
func (p *DNSPolicy) Kind() string {
return NewDNSPolicyType().GetGVK().Kind
}

func (p *DNSPolicy) TargetProgrammedGatewaysOnly() bool {
Expand All @@ -206,11 +202,11 @@ func (p *DNSPolicy) PolicyClass() kuadrantgatewayapi.PolicyClass {
}

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

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

//+kubebuilder:object:root=true
Expand Down Expand Up @@ -302,3 +298,39 @@ func (p *DNSPolicy) WithLoadBalancingFor(defaultWeight Weight, custom []*CustomW
},
})
}

type dnsPolicyType struct{}

func NewDNSPolicyType() kuadrantgatewayapi.PolicyType {
return &dnsPolicyType{}
}

func (d dnsPolicyType) GetGVK() schema.GroupVersionKind {
return DNSPolicyGVK
}

func (d dnsPolicyType) GetInstance() client.Object {
return &DNSPolicy{
TypeMeta: metav1.TypeMeta{
Kind: DNSPolicyGVK.Kind,
APIVersion: GroupVersion.String(),
},
}
}

func (d dnsPolicyType) GetList(ctx context.Context, cl client.Client, listOpts ...client.ListOption) ([]kuadrantgatewayapi.Policy, error) {
list := &DNSPolicyList{}
err := cl.List(ctx, list, listOpts...)
if err != nil {
return nil, err
}
return utils.Map(list.Items, func(p DNSPolicy) kuadrantgatewayapi.Policy { return &p }), nil
}

func (d dnsPolicyType) BackReferenceAnnotationName() string {
return DNSPolicyBackReferenceAnnotationName
}

func (d dnsPolicyType) DirectReferenceAnnotationName() string {
return DNSPolicyDirectReferenceAnnotationName
}
65 changes: 48 additions & 17 deletions api/v1alpha1/tlspolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
certmanv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
certmanmetav1 "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
Expand All @@ -31,6 +32,14 @@ import (
"github.com/kuadrant/kuadrant-operator/pkg/library/utils"
)

var (
TLSPolicyGVK schema.GroupVersionKind = schema.GroupVersionKind{
Group: GroupVersion.Group,
Version: GroupVersion.Version,
Kind: "TLSPolicy",
}
)

const (
TLSPolicyBackReferenceAnnotationName = "kuadrant.io/tlspolicies"
TLSPolicyDirectReferenceAnnotationName = "kuadrant.io/tlspolicy"
Expand Down Expand Up @@ -145,21 +154,8 @@ type TLSPolicy struct {
Status TLSPolicyStatus `json:"status,omitempty"`
}

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

func (p *TLSPolicy) List(ctx context.Context, c client.Client, namespace string) []kuadrantgatewayapi.Policy {
policyList := &TLSPolicyList{}
listOptions := &client.ListOptions{Namespace: namespace}
err := c.List(ctx, policyList, listOptions)
if err != nil {
return []kuadrantgatewayapi.Policy{}
}
policies := make([]kuadrantgatewayapi.Policy, 0, len(policyList.Items))
for i := range policyList.Items {
policies = append(policies, &policyList.Items[i])
}

return policies
func (p *TLSPolicy) Kind() string {
return NewTLSPolicyType().GetGVK().Kind
}

func (p *TLSPolicy) TargetProgrammedGatewaysOnly() bool {
Expand Down Expand Up @@ -187,11 +183,11 @@ func (p *TLSPolicy) GetStatus() kuadrantgatewayapi.PolicyStatus {
}

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

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

//+kubebuilder:object:root=true
Expand Down Expand Up @@ -242,3 +238,38 @@ func (p *TLSPolicy) WithIssuerRef(issuerRef certmanmetav1.ObjectReference) *TLSP
p.Spec.IssuerRef = issuerRef
return p
}

type tlsPolicyType struct{}

func NewTLSPolicyType() kuadrantgatewayapi.PolicyType {
return &tlsPolicyType{}
}

func (t tlsPolicyType) GetGVK() schema.GroupVersionKind {
return TLSPolicyGVK
}
func (t tlsPolicyType) GetInstance() client.Object {
return &TLSPolicy{
TypeMeta: metav1.TypeMeta{
Kind: TLSPolicyGVK.Kind,
APIVersion: GroupVersion.String(),
},
}
}

func (t tlsPolicyType) GetList(ctx context.Context, cl client.Client, listOpts ...client.ListOption) ([]kuadrantgatewayapi.Policy, error) {
list := &TLSPolicyList{}
err := cl.List(ctx, list, listOpts...)
if err != nil {
return nil, err
}
return utils.Map(list.Items, func(p TLSPolicy) kuadrantgatewayapi.Policy { return &p }), nil
}

func (t tlsPolicyType) BackReferenceAnnotationName() string {
return TLSPolicyBackReferenceAnnotationName
}

func (t tlsPolicyType) DirectReferenceAnnotationName() string {
return TLSPolicyDirectReferenceAnnotationName
}
65 changes: 47 additions & 18 deletions api/v1beta2/authpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/google/go-cmp/cmp"
authorinoapi "github.com/kuadrant/authorino/api/v1beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
Expand All @@ -16,6 +17,14 @@ import (
"github.com/kuadrant/kuadrant-operator/pkg/library/utils"
)

var (
AuthPolicyGVK schema.GroupVersionKind = schema.GroupVersionKind{
Group: GroupVersion.Group,
Version: GroupVersion.Version,
Kind: "AuthPolicy",
}
)

const (
AuthPolicyBackReferenceAnnotationName = "kuadrant.io/authpolicies"
AuthPolicyDirectReferenceAnnotationName = "kuadrant.io/authpolicy"
Expand Down Expand Up @@ -328,22 +337,7 @@ func (ap *AuthPolicy) GetRulesHostnames() (ruleHosts []string) {
}

func (ap *AuthPolicy) Kind() string {
return ap.TypeMeta.Kind
}

func (ap *AuthPolicy) List(ctx context.Context, c client.Client, namespace string) []kuadrantgatewayapi.Policy {
policyList := &AuthPolicyList{}
listOptions := &client.ListOptions{Namespace: namespace}
err := c.List(ctx, policyList, listOptions)
if err != nil {
return []kuadrantgatewayapi.Policy{}
}
policies := make([]kuadrantgatewayapi.Policy, 0, len(policyList.Items))
for i := range policyList.Items {
policies = append(policies, &policyList.Items[i])
}

return policies
return NewAuthPolicyType().GetGVK().Kind
}

func (ap *AuthPolicy) TargetProgrammedGatewaysOnly() bool {
Expand All @@ -355,11 +349,11 @@ func (ap *AuthPolicy) PolicyClass() kuadrantgatewayapi.PolicyClass {
}

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

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

func (ap *AuthPolicySpec) CommonSpec() *AuthPolicyCommonSpec {
Expand Down Expand Up @@ -389,6 +383,41 @@ func (l *AuthPolicyList) GetItems() []kuadrant.Policy {
})
}

type authPolicyType struct{}

func NewAuthPolicyType() kuadrantgatewayapi.PolicyType {
return &authPolicyType{}
}

func (a authPolicyType) GetGVK() schema.GroupVersionKind {
return AuthPolicyGVK
}
func (a authPolicyType) GetInstance() client.Object {
return &AuthPolicy{
TypeMeta: metav1.TypeMeta{
Kind: AuthPolicyGVK.Kind,
APIVersion: GroupVersion.String(),
},
}
}

func (a authPolicyType) GetList(ctx context.Context, cl client.Client, listOpts ...client.ListOption) ([]kuadrantgatewayapi.Policy, error) {
list := &AuthPolicyList{}
err := cl.List(ctx, list, listOpts...)
if err != nil {
return nil, err
}
return utils.Map(list.Items, func(p AuthPolicy) kuadrantgatewayapi.Policy { return &p }), nil
}

func (a authPolicyType) BackReferenceAnnotationName() string {
return AuthPolicyBackReferenceAnnotationName
}

func (a authPolicyType) DirectReferenceAnnotationName() string {
return AuthPolicyDirectReferenceAnnotationName
}

func init() {
SchemeBuilder.Register(&AuthPolicy{}, &AuthPolicyList{})
}
Loading

0 comments on commit c1fb9b4

Please sign in to comment.