From c7e15c84f8b232f61d97e66c6c31097f53ffd873 Mon Sep 17 00:00:00 2001 From: Michael Nairn Date: Fri, 1 Nov 2024 14:49:02 +0000 Subject: [PATCH] chore: TLS and DNS Policy API cleanup * Remove unnescaery implementation of kuadrant.Referrer * Mark implementation of kuadrant.Policy as Deprecated * Avoid the use of Kind() method in DNS/TLS policy validator tasks * Set type of missing resource to Gateway rather than the Policy. This isn't 100% correct since it could be a section name not found, but it's closer than using the policy. Signed-off-by: Michael Nairn --- api/v1alpha1/dnspolicy_types.go | 77 ++++------------------------ api/v1alpha1/tlspolicy_types.go | 74 +++----------------------- controllers/dnspolicies_validator.go | 4 +- controllers/tlspolicies_validator.go | 2 +- 4 files changed, 21 insertions(+), 136 deletions(-) diff --git a/api/v1alpha1/dnspolicy_types.go b/api/v1alpha1/dnspolicy_types.go index 5aa5ad64f..1b5aa2ce9 100644 --- a/api/v1alpha1/dnspolicy_types.go +++ b/api/v1alpha1/dnspolicy_types.go @@ -17,7 +17,6 @@ limitations under the License. package v1alpha1 import ( - "context" "fmt" "net" "strings" @@ -25,9 +24,7 @@ import ( dnsv1alpha1 "github.com/kuadrant/dns-operator/api/v1alpha1" corev1 "k8s.io/api/core/v1" 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" gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" @@ -36,21 +33,9 @@ import ( "github.com/kuadrant/kuadrant-operator/pkg/library/utils" ) -var ( - DNSPolicyGVK schema.GroupVersionKind = schema.GroupVersionKind{ - Group: GroupVersion.Group, - Version: GroupVersion.Version, - Kind: "DNSPolicy", - } -) - const ( - DefaultWeight int = 120 - DefaultGeo GeoCode = "default" - WildcardGeo GeoCode = "*" - - DNSPolicyBackReferenceAnnotationName = "kuadrant.io/dnspolicies" - DNSPolicyDirectReferenceAnnotationName = "kuadrant.io/dnspolicy" + DefaultGeo GeoCode = "default" + WildcardGeo GeoCode = "*" ) // DNSPolicySpec defines the desired state of DNSPolicy @@ -157,7 +142,6 @@ func (s *DNSPolicyStatus) GetConditions() []metav1.Condition { } var _ kuadrant.Policy = &DNSPolicy{} -var _ kuadrant.Referrer = &DNSPolicy{} // +kubebuilder:object:root=true // +kubebuilder:subresource:status @@ -181,42 +165,36 @@ func (p *DNSPolicy) Validate() error { return p.Spec.ExcludeAddresses.Validate() } +// Deprecated: kuadrant.Policy. func (p *DNSPolicy) GetWrappedNamespace() gatewayapiv1.Namespace { return gatewayapiv1.Namespace(p.Namespace) } +// Deprecated: kuadrant.Policy. func (p *DNSPolicy) GetRulesHostnames() []string { return make([]string, 0) } +// Deprecated: kuadrant.Policy. func (p *DNSPolicy) GetTargetRef() gatewayapiv1alpha2.LocalPolicyTargetReference { return p.Spec.TargetRef.LocalPolicyTargetReference } +// Deprecated: kuadrant.Policy. func (p *DNSPolicy) GetStatus() kuadrantgatewayapi.PolicyStatus { return &p.Status } +// Deprecated: kuadrant.Policy. func (p *DNSPolicy) Kind() string { - return NewDNSPolicyType().GetGVK().Kind -} - -func (p *DNSPolicy) TargetProgrammedGatewaysOnly() bool { - return true + return DNSPolicyGroupKind.Kind } +// Deprecated: kuadrant.Policy. func (p *DNSPolicy) PolicyClass() kuadrantgatewayapi.PolicyClass { return kuadrantgatewayapi.DirectPolicy } -func (p *DNSPolicy) BackReferenceAnnotationName() string { - return NewDNSPolicyType().BackReferenceAnnotationName() -} - -func (p *DNSPolicy) DirectReferenceAnnotationName() string { - return NewDNSPolicyType().DirectReferenceAnnotationName() -} - //+kubebuilder:object:root=true // DNSPolicyList contains a list of DNSPolicy @@ -226,6 +204,7 @@ type DNSPolicyList struct { Items []DNSPolicy `json:"items"` } +// Deprecated: kuadrant.PolicyList. func (l *DNSPolicyList) GetItems() []kuadrant.Policy { return utils.Map(l.Items, func(item DNSPolicy) kuadrant.Policy { return &item @@ -326,39 +305,3 @@ func (p *DNSPolicy) WithLoadBalancingFor(weight int, geo string, isDefaultGeo bo DefaultGeo: isDefaultGeo, }) } - -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 -} diff --git a/api/v1alpha1/tlspolicy_types.go b/api/v1alpha1/tlspolicy_types.go index ebf168a85..6d2022aa8 100644 --- a/api/v1alpha1/tlspolicy_types.go +++ b/api/v1alpha1/tlspolicy_types.go @@ -17,13 +17,9 @@ limitations under the License. package v1alpha1 import ( - "context" - 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" @@ -32,19 +28,6 @@ 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" -) - // TLSPolicySpec defines the desired state of TLSPolicy type TLSPolicySpec struct { // TargetRef identifies an API object to apply policy to. @@ -134,7 +117,6 @@ func (s *TLSPolicyStatus) GetConditions() []metav1.Condition { } var _ kuadrant.Policy = &TLSPolicy{} -var _ kuadrant.Referrer = &TLSPolicy{} // +kubebuilder:object:root=true // +kubebuilder:subresource:status @@ -154,42 +136,36 @@ type TLSPolicy struct { Status TLSPolicyStatus `json:"status,omitempty"` } +// Deprecated: kuadrant.Policy. func (p *TLSPolicy) Kind() string { - return NewTLSPolicyType().GetGVK().Kind -} - -func (p *TLSPolicy) TargetProgrammedGatewaysOnly() bool { - return false + return TLSPolicyGroupKind.Kind } +// Deprecated: kuadrant.Policy. func (p *TLSPolicy) PolicyClass() kuadrantgatewayapi.PolicyClass { return kuadrantgatewayapi.DirectPolicy } +// Deprecated: kuadrant.Policy. func (p *TLSPolicy) GetWrappedNamespace() gatewayapiv1.Namespace { return gatewayapiv1.Namespace(p.Namespace) } +// Deprecated: kuadrant.Policy. func (p *TLSPolicy) GetRulesHostnames() []string { return make([]string, 0) } +// Deprecated: kuadrant.Policy. func (p *TLSPolicy) GetTargetRef() gatewayapiv1alpha2.LocalPolicyTargetReference { return p.Spec.TargetRef } +// Deprecated: kuadrant.Policy. func (p *TLSPolicy) GetStatus() kuadrantgatewayapi.PolicyStatus { return &p.Status } -func (p *TLSPolicy) BackReferenceAnnotationName() string { - return NewTLSPolicyType().BackReferenceAnnotationName() -} - -func (p *TLSPolicy) DirectReferenceAnnotationName() string { - return NewTLSPolicyType().DirectReferenceAnnotationName() -} - //+kubebuilder:object:root=true // TLSPolicyList contains a list of TLSPolicy @@ -199,6 +175,7 @@ type TLSPolicyList struct { Items []TLSPolicy `json:"items"` } +// Deprecated: kuadrant.PolicyList. func (l *TLSPolicyList) GetItems() []kuadrant.Policy { return utils.Map(l.Items, func(item TLSPolicy) kuadrant.Policy { return &item @@ -238,38 +215,3 @@ 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 -} diff --git a/controllers/dnspolicies_validator.go b/controllers/dnspolicies_validator.go index cbe77d42d..510a8c566 100644 --- a/controllers/dnspolicies_validator.go +++ b/controllers/dnspolicies_validator.go @@ -40,8 +40,8 @@ func (r *DNSPoliciesValidator) validate(ctx context.Context, _ []controller.Reso state.Store(StateDNSPolicyAcceptedKey, lo.SliceToMap(policies, func(policy *kuadrantv1alpha1.DNSPolicy) (string, error) { if len(policy.GetTargetRefs()) == 0 || len(topology.Targetables().Children(policy)) == 0 { - return policy.GetLocator(), kuadrant.NewErrTargetNotFound(policy.Kind(), policy.GetTargetRef(), - apierrors.NewNotFound(kuadrantv1alpha1.DNSPoliciesResource.GroupResource(), policy.GetName())) + return policy.GetLocator(), kuadrant.NewErrTargetNotFound(kuadrantv1alpha1.DNSPolicyGroupKind.Kind, policy.GetTargetRef(), + apierrors.NewNotFound(controller.GatewaysResource.GroupResource(), policy.GetName())) } return policy.GetLocator(), policy.Validate() })) diff --git a/controllers/tlspolicies_validator.go b/controllers/tlspolicies_validator.go index 419b1e61a..e11ebcc1f 100644 --- a/controllers/tlspolicies_validator.go +++ b/controllers/tlspolicies_validator.go @@ -93,7 +93,7 @@ func (t *TLSPoliciesValidator) Validate(ctx context.Context, _ []controller.Reso // TODO: What should happen if multiple target refs is supported in the future in terms of reporting in log and policy status? func (t *TLSPoliciesValidator) isTargetRefsFound(topology *machinery.Topology, p *kuadrantv1alpha1.TLSPolicy) error { if len(p.GetTargetRefs()) != len(topology.Targetables().Children(p)) { - return kuadrant.NewErrTargetNotFound(p.Kind(), p.GetTargetRef(), apierrors.NewNotFound(kuadrantv1alpha1.TLSPoliciesResource.GroupResource(), p.GetName())) + return kuadrant.NewErrTargetNotFound(kuadrantv1alpha1.TLSPolicyGroupKind.Kind, p.GetTargetRef(), apierrors.NewNotFound(controller.GatewaysResource.GroupResource(), p.GetName())) } return nil