From 15753fd077ee2b4af5acea2aa54c7158f267f682 Mon Sep 17 00:00:00 2001 From: KevFan Date: Fri, 2 Feb 2024 11:50:42 +0000 Subject: [PATCH] refactor: package import naming & use consts for reference annotation strings --- api/v1beta2/authpolicy_types.go | 9 +++++++-- api/v1beta2/ratelimitpolicy_types.go | 7 +++++-- controllers/authpolicy_controller.go | 13 ++++++------- controllers/ratelimitpolicy_controller.go | 10 +++++----- controllers/suite_test.go | 6 +++--- main.go | 6 +++--- 6 files changed, 29 insertions(+), 22 deletions(-) diff --git a/api/v1beta2/authpolicy_types.go b/api/v1beta2/authpolicy_types.go index 86e910d82..7555ccda0 100644 --- a/api/v1beta2/authpolicy_types.go +++ b/api/v1beta2/authpolicy_types.go @@ -14,6 +14,11 @@ import ( "github.com/kuadrant/kuadrant-operator/pkg/library/utils" ) +const ( + AuthPolicyBackReferenceAnnotationName = "kuadrant.io/authpolicies" + AuthPolicyDirectReferenceAnnotationName = "kuadrant.io/authpolicy" +) + type AuthSchemeSpec struct { // Authentication configs. // At least one config MUST evaluate to a valid identity object for the auth request to be successful. @@ -286,11 +291,11 @@ func (ap *AuthPolicy) Kind() string { } func (ap *AuthPolicy) BackReferenceAnnotationName() string { - return "kuadrant.io/authpolicies" + return AuthPolicyBackReferenceAnnotationName } func (ap *AuthPolicy) DirectReferenceAnnotationName() string { - return "kuadrant.io/authpolicy" + return AuthPolicyDirectReferenceAnnotationName } //+kubebuilder:object:root=true diff --git a/api/v1beta2/ratelimitpolicy_types.go b/api/v1beta2/ratelimitpolicy_types.go index 2e151dec7..03bdad808 100644 --- a/api/v1beta2/ratelimitpolicy_types.go +++ b/api/v1beta2/ratelimitpolicy_types.go @@ -52,6 +52,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 @@ -250,11 +253,11 @@ func (r *RateLimitPolicy) Kind() string { } func (r *RateLimitPolicy) BackReferenceAnnotationName() string { - return "kuadrant.io/ratelimitpolicies" + return RateLimitPolicyBackReferenceAnnotationName } func (r *RateLimitPolicy) DirectReferenceAnnotationName() string { - return "kuadrant.io/ratelimitpolicy" + return RateLimitPolicyDirectReferenceAnnotationName } func init() { diff --git a/controllers/authpolicy_controller.go b/controllers/authpolicy_controller.go index 68ca3d8f8..814cda312 100644 --- a/controllers/authpolicy_controller.go +++ b/controllers/authpolicy_controller.go @@ -5,6 +5,7 @@ import ( "encoding/json" "github.com/go-logr/logr" + authorinoapi "github.com/kuadrant/authorino/api/v1beta2" apierrors "k8s.io/apimachinery/pkg/api/errors" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -13,11 +14,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1" - authorinoapi "github.com/kuadrant/authorino/api/v1beta2" - api "github.com/kuadrant/kuadrant-operator/api/v1beta2" "github.com/kuadrant/kuadrant-operator/pkg/library/mappers" - reconcilers2 "github.com/kuadrant/kuadrant-operator/pkg/library/reconcilers" + reconcilerutils "github.com/kuadrant/kuadrant-operator/pkg/library/reconcilers" "github.com/kuadrant/kuadrant-operator/pkg/library/utils" "github.com/kuadrant/kuadrant-operator/pkg/reconcilers" ) @@ -27,7 +26,7 @@ const authPolicyFinalizer = "authpolicy.kuadrant.io/finalizer" // AuthPolicyReconciler reconciles a AuthPolicy object type AuthPolicyReconciler struct { *reconcilers.BaseReconciler - TargetRefReconciler reconcilers2.TargetRefReconciler + TargetRefReconciler reconcilerutils.TargetRefReconciler // OverriddenPolicyMap tracks the overridden policies to report their status. OverriddenPolicyMap *utils.OverriddenPolicyMap } @@ -65,7 +64,7 @@ func (r *AuthPolicyReconciler) Reconcile(eventCtx context.Context, req ctrl.Requ markedForDeletion := ap.GetDeletionTimestamp() != nil // fetch the target network object - targetNetworkObject, err := reconcilers2.FetchTargetRefObject(ctx, r.Client(), ap.GetTargetRef(), ap.Namespace) + targetNetworkObject, err := reconcilerutils.FetchTargetRefObject(ctx, r.Client(), ap.GetTargetRef(), ap.Namespace) if err != nil { if !markedForDeletion { if apierrors.IsNotFound(err) { @@ -156,7 +155,7 @@ func (r *AuthPolicyReconciler) reconcileResources(ctx context.Context, ap *api.A } // reconcile based on gateway diffs - gatewayDiffObj, err := reconcilers2.ComputeGatewayDiffs(ctx, r.Client(), ap, targetNetworkObject) + gatewayDiffObj, err := reconcilerutils.ComputeGatewayDiffs(ctx, r.Client(), ap, targetNetworkObject) if err != nil { return err } @@ -180,7 +179,7 @@ func (r *AuthPolicyReconciler) reconcileResources(ctx context.Context, ap *api.A func (r *AuthPolicyReconciler) deleteResources(ctx context.Context, ap *api.AuthPolicy, targetNetworkObject client.Object) error { // delete based on gateway diffs - gatewayDiffObj, err := reconcilers2.ComputeGatewayDiffs(ctx, r.Client(), ap, targetNetworkObject) + gatewayDiffObj, err := reconcilerutils.ComputeGatewayDiffs(ctx, r.Client(), ap, targetNetworkObject) if err != nil { return err } diff --git a/controllers/ratelimitpolicy_controller.go b/controllers/ratelimitpolicy_controller.go index a2d5f99de..da0a23cd6 100644 --- a/controllers/ratelimitpolicy_controller.go +++ b/controllers/ratelimitpolicy_controller.go @@ -31,7 +31,7 @@ import ( kuadrantv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2" "github.com/kuadrant/kuadrant-operator/pkg/library/mappers" - reconcilers2 "github.com/kuadrant/kuadrant-operator/pkg/library/reconcilers" + reconcilerutils "github.com/kuadrant/kuadrant-operator/pkg/library/reconcilers" "github.com/kuadrant/kuadrant-operator/pkg/library/utils" "github.com/kuadrant/kuadrant-operator/pkg/reconcilers" ) @@ -41,7 +41,7 @@ const rateLimitPolicyFinalizer = "ratelimitpolicy.kuadrant.io/finalizer" // RateLimitPolicyReconciler reconciles a RateLimitPolicy object type RateLimitPolicyReconciler struct { *reconcilers.BaseReconciler - TargetRefReconciler reconcilers2.TargetRefReconciler + TargetRefReconciler reconcilerutils.TargetRefReconciler } //+kubebuilder:rbac:groups=kuadrant.io,resources=ratelimitpolicies,verbs=get;list;watch;create;update;patch;delete @@ -88,7 +88,7 @@ func (r *RateLimitPolicyReconciler) Reconcile(eventCtx context.Context, req ctrl markedForDeletion := rlp.GetDeletionTimestamp() != nil // fetch the target network object - targetNetworkObject, err := reconcilers2.FetchTargetRefObject(ctx, r.Client(), rlp.GetTargetRef(), rlp.Namespace) + targetNetworkObject, err := reconcilerutils.FetchTargetRefObject(ctx, r.Client(), rlp.GetTargetRef(), rlp.Namespace) if err != nil { if !markedForDeletion { if apierrors.IsNotFound(err) { @@ -172,7 +172,7 @@ func (r *RateLimitPolicyReconciler) reconcileResources(ctx context.Context, rlp } // reconcile based on gateway diffs - gatewayDiffObj, err := reconcilers2.ComputeGatewayDiffs(ctx, r.Client(), rlp, targetNetworkObject) + gatewayDiffObj, err := reconcilerutils.ComputeGatewayDiffs(ctx, r.Client(), rlp, targetNetworkObject) if err != nil { return err } @@ -196,7 +196,7 @@ func (r *RateLimitPolicyReconciler) reconcileResources(ctx context.Context, rlp func (r *RateLimitPolicyReconciler) deleteResources(ctx context.Context, rlp *kuadrantv1beta2.RateLimitPolicy, targetNetworkObject client.Object) error { // delete based on gateway diffs - gatewayDiffObj, err := reconcilers2.ComputeGatewayDiffs(ctx, r.Client(), rlp, targetNetworkObject) + gatewayDiffObj, err := reconcilerutils.ComputeGatewayDiffs(ctx, r.Client(), rlp, targetNetworkObject) if err != nil { return err } diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 289acec34..21365d51f 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -44,7 +44,7 @@ import ( kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1" kuadrantv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2" - reconcilers2 "github.com/kuadrant/kuadrant-operator/pkg/library/reconcilers" + reconcilerutils "github.com/kuadrant/kuadrant-operator/pkg/library/reconcilers" "github.com/kuadrant/kuadrant-operator/pkg/library/utils" "github.com/kuadrant/kuadrant-operator/pkg/log" "github.com/kuadrant/kuadrant-operator/pkg/reconcilers" @@ -131,7 +131,7 @@ var _ = BeforeSuite(func() { err = (&AuthPolicyReconciler{ BaseReconciler: authPolicyBaseReconciler, - TargetRefReconciler: reconcilers2.TargetRefReconciler{Client: mgr.GetClient()}, + TargetRefReconciler: reconcilerutils.TargetRefReconciler{Client: mgr.GetClient()}, OverriddenPolicyMap: utils.NewOverriddenPolicyMap(), }).SetupWithManager(mgr) Expect(err).NotTo(HaveOccurred()) @@ -144,7 +144,7 @@ var _ = BeforeSuite(func() { err = (&RateLimitPolicyReconciler{ BaseReconciler: rateLimitPolicyBaseReconciler, - TargetRefReconciler: reconcilers2.TargetRefReconciler{Client: mgr.GetClient()}, + TargetRefReconciler: reconcilerutils.TargetRefReconciler{Client: mgr.GetClient()}, }).SetupWithManager(mgr) Expect(err).NotTo(HaveOccurred()) diff --git a/main.go b/main.go index 5b7e0a41a..c1b823c87 100644 --- a/main.go +++ b/main.go @@ -49,7 +49,7 @@ import ( kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1" kuadrantv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2" "github.com/kuadrant/kuadrant-operator/controllers" - reconcilers2 "github.com/kuadrant/kuadrant-operator/pkg/library/reconcilers" + reconcilerutils "github.com/kuadrant/kuadrant-operator/pkg/library/reconcilers" "github.com/kuadrant/kuadrant-operator/pkg/library/utils" "github.com/kuadrant/kuadrant-operator/pkg/log" "github.com/kuadrant/kuadrant-operator/pkg/reconcilers" @@ -151,7 +151,7 @@ func main() { ) if err = (&controllers.RateLimitPolicyReconciler{ - TargetRefReconciler: reconcilers2.TargetRefReconciler{Client: mgr.GetClient()}, + TargetRefReconciler: reconcilerutils.TargetRefReconciler{Client: mgr.GetClient()}, BaseReconciler: rateLimitPolicyBaseReconciler, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "RateLimitPolicy") @@ -165,7 +165,7 @@ func main() { ) if err = (&controllers.AuthPolicyReconciler{ - TargetRefReconciler: reconcilers2.TargetRefReconciler{Client: mgr.GetClient()}, + TargetRefReconciler: reconcilerutils.TargetRefReconciler{Client: mgr.GetClient()}, BaseReconciler: authPolicyBaseReconciler, OverriddenPolicyMap: utils.NewOverriddenPolicyMap(), }).SetupWithManager(mgr); err != nil {