Skip to content

Commit 62f8261

Browse files
authored
fix ratelimit namespace and handle deletion of rl policy (Kuadrant#94)
* fix ratelimit namespace and handle deletion of rl policy * ratelimit cr name template rlp-%s-%s-%d
1 parent fa680c2 commit 62f8261

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ istio-manifest-update-test: generate-istio-manifests
260260

261261
.PHONY: local-setup
262262
local-setup: local-cleanup local-setup-kind manifests kustomize generate
263-
./utils/local-deployment/local-setup.sh
263+
export PATH=$(PROJECT_PATH)/bin:$$PATH; ./utils/local-deployment/local-setup.sh
264264

265265
# Deploys all services and manifests required by kuadrant to run
266266
# kuadrant is not deployed

controllers/apim/ratelimitpolicy_controller.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package apim
1919
import (
2020
"context"
2121
"encoding/json"
22-
"fmt"
2322

2423
"github.com/go-logr/logr"
2524
"github.com/gogo/protobuf/types"
@@ -82,21 +81,25 @@ func (r *RateLimitPolicyReconciler) Reconcile(eventCtx context.Context, req ctrl
8281
}
8382

8483
if rlp.GetDeletionTimestamp() != nil && controllerutil.ContainsFinalizer(&rlp, patchesFinalizer) {
85-
logger.Info("Removing finalizers")
84+
logger.V(1).Info("Handling removal of ratelimitpolicy object")
8685
if err := r.finalizeEnvoyFilters(ctx, &rlp); err != nil {
8786
logger.Error(err, "failed to remove ownerRlp entry from filters patch")
8887
return ctrl.Result{}, err
8988
}
89+
if err := r.deleteRateLimits(ctx, &rlp); err != nil {
90+
logger.Error(err, "failed to delete RateLimt objects")
91+
return ctrl.Result{}, err
92+
}
9093
controllerutil.RemoveFinalizer(&rlp, patchesFinalizer)
91-
if err := r.BaseReconciler.UpdateResource(ctx, &rlp); client.IgnoreNotFound(err) != nil {
94+
if err := r.UpdateResource(ctx, &rlp); client.IgnoreNotFound(err) != nil {
9295
return ctrl.Result{}, err
9396
}
9497
return ctrl.Result{}, nil
9598
}
9699

97100
if !controllerutil.ContainsFinalizer(&rlp, patchesFinalizer) {
98101
controllerutil.AddFinalizer(&rlp, patchesFinalizer)
99-
if err := r.BaseReconciler.UpdateResource(ctx, &rlp); client.IgnoreNotFound(err) != nil {
102+
if err := r.UpdateResource(ctx, &rlp); client.IgnoreNotFound(err) != nil {
100103
return ctrl.Result{Requeue: true}, err
101104
}
102105
}
@@ -530,14 +533,17 @@ func virtualHostRateLimitsPatch(vHostName string, rateLimits []*apimv1alpha1.Rat
530533
}
531534

532535
func (r *RateLimitPolicyReconciler) reconcileLimits(ctx context.Context, rlp *apimv1alpha1.RateLimitPolicy) error {
533-
logger := r.Logger()
536+
logger := logr.FromContext(ctx)
537+
rlpKey := client.ObjectKeyFromObject(rlp)
534538

535539
// create the RateLimit resource
536540
for i, rlSpec := range rlp.Spec.Limits {
537541
ratelimitfactory := common.RateLimitFactory{
538542
Key: client.ObjectKey{
539-
Name: fmt.Sprintf("%s-limit-%d", rlp.Name, i+1),
540-
Namespace: rlp.Namespace,
543+
Name: limitadorRatelimitsName(rlpKey, i+1),
544+
// Currently, Limitador Operator (v0.2.0) will configure limitador services with
545+
// RateLimit CRs created in the same namespace.
546+
Namespace: common.KuadrantNamespace,
541547
},
542548
Conditions: rlSpec.Conditions,
543549
MaxValue: rlSpec.MaxValue,
@@ -547,11 +553,7 @@ func (r *RateLimitPolicyReconciler) reconcileLimits(ctx context.Context, rlp *ap
547553
}
548554

549555
ratelimit := ratelimitfactory.RateLimit()
550-
if err := controllerutil.SetOwnerReference(rlp, ratelimit, r.Client().Scheme()); err != nil {
551-
logger.Error(err, "failed to add owner ref to RateLimit resource")
552-
return err
553-
}
554-
err := r.BaseReconciler.ReconcileResource(ctx, &v1alpha1.RateLimit{}, ratelimit, alwaysUpdateRateLimit)
556+
err := r.ReconcileResource(ctx, &v1alpha1.RateLimit{}, ratelimit, alwaysUpdateRateLimit)
555557
if err != nil && !apierrors.IsAlreadyExists(err) {
556558
logger.Error(err, "ReconcileResource failed to create/update RateLimit resource")
557559
return err

controllers/apim/ratelimitpolicy_finalizers.go

+25
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,28 @@ func (r *RateLimitPolicyReconciler) addParentRefEntry(ctx context.Context, patch
156156
logger.Info("Successfully added parentRef entry to the EnvoyFilter")
157157
return nil
158158
}
159+
160+
func (r *RateLimitPolicyReconciler) deleteRateLimits(ctx context.Context, rlp *apimv1alpha1.RateLimitPolicy) error {
161+
logger := logr.FromContext(ctx)
162+
rlpKey := client.ObjectKeyFromObject(rlp)
163+
for i := range rlp.Spec.Limits {
164+
ratelimitfactory := common.RateLimitFactory{
165+
Key: client.ObjectKey{
166+
Name: limitadorRatelimitsName(rlpKey, i+1),
167+
// Currently, Limitador Operator (v0.2.0) will configure limitador services with
168+
// RateLimit CRs created in the same namespace.
169+
Namespace: common.KuadrantNamespace,
170+
},
171+
// rest of the parameters empty
172+
}
173+
174+
rateLimit := ratelimitfactory.RateLimit()
175+
err := r.DeleteResource(ctx, rateLimit)
176+
logger.V(1).Info("Removing rate limit", "ratelimit", client.ObjectKeyFromObject(rateLimit), "error", err)
177+
if err != nil && !apierrors.IsNotFound(err) {
178+
return err
179+
}
180+
}
181+
182+
return nil
183+
}

controllers/apim/utils.go

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ func ratelimitsPatchName(gwName string, networkKey client.ObjectKey) string {
3131
return fmt.Sprintf("ratelimits-on-%s-using-%s-%s", gwName, networkKey.Namespace, networkKey.Name)
3232
}
3333

34+
// limitadorRatelimitsName returns the name of Limitador RateLimit CR.
35+
func limitadorRatelimitsName(objKey client.ObjectKey, idx int) string {
36+
return fmt.Sprintf("rlp-%s-%s-%d", objKey.Namespace, objKey.Name, idx)
37+
}
38+
3439
// getAuthPolicyName generates the name of an AuthorizationPolicy using VirtualService info.
3540
func getAuthPolicyName(gwName, vsName string) string {
3641
return fmt.Sprintf("on-%s-using-%s", gwName, vsName)

0 commit comments

Comments
 (0)