Skip to content

Commit

Permalink
Add owner references to mutator function
Browse files Browse the repository at this point in the history
Fixes edge case.
The user is using limitador as a standalone product before using kuadrant and has the limitador CR called `limitador`. Once kuadrant is installed to the same namespace the kuadrant operator should take ownership of the limitador CR.

The update in this commit is only required for this use case.
  • Loading branch information
Boomatang committed Jan 16, 2024
1 parent c1ca4ec commit c029047
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
5 changes: 5 additions & 0 deletions controllers/kuadrant_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"context"
"encoding/json"
"fmt"
"reflect"

"github.com/kuadrant/kuadrant-operator/pkg/kuadranttools"
Expand Down Expand Up @@ -471,6 +472,9 @@ func (r *KuadrantReconciler) removeAnnotationFromGateways(ctx context.Context, k
}

func (r *KuadrantReconciler) reconcileLimitador(ctx context.Context, kObj *kuadrantv1beta1.Kuadrant) error {
logger, _ := logr.FromContext(ctx)
logger = logger.WithName("limitador reconcile")

limitadorKey := client.ObjectKey{Name: common.LimitadorName, Namespace: kObj.Namespace}
limitador := &limitadorv1alpha1.Limitador{}
err := r.Client().Get(ctx, limitadorKey, limitador)
Expand Down Expand Up @@ -525,6 +529,7 @@ func (r *KuadrantReconciler) reconcileLimitador(ctx context.Context, kObj *kuadr
}

Check warning on line 529 in controllers/kuadrant_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/kuadrant_controller.go#L528-L529

Added lines #L528 - L529 were not covered by tests

if limitador.OwnerReferences != nil && !reflect.DeepEqual(tmp.OwnerReferences, limitador.OwnerReferences) {
logger.Error(fmt.Errorf("limitador CR owned by different controller: %v", limitador.OwnerReferences[0].Name), "check limitador CR ownerReference")
return nil
}

Expand Down
43 changes: 38 additions & 5 deletions controllers/kuadrant_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"context"
"fmt"
"reflect"
"slices"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -94,7 +95,7 @@ func (r *KuadrantReconciler) readyCondition(ctx context.Context, kObj *kuadrantv
return cond, nil
}

reason, err := r.checkLimitadorReady(ctx, kObj)
reason, err := r.checkLimitador(ctx, kObj)
if err != nil {
return nil, err
}
Expand All @@ -119,7 +120,7 @@ func (r *KuadrantReconciler) readyCondition(ctx context.Context, kObj *kuadrantv
return cond, nil
}

func (r *KuadrantReconciler) checkLimitadorReady(ctx context.Context, kObj *kuadrantv1beta1.Kuadrant) (*string, error) {
func (r *KuadrantReconciler) checkLimitador(ctx context.Context, kObj *kuadrantv1beta1.Kuadrant) (*string, error) {
limitadorObj := &limitadorv1alpha1.Limitador{}
limitadorKey := client.ObjectKey{Name: common.LimitadorName, Namespace: kObj.Namespace}

Expand All @@ -133,16 +134,48 @@ func (r *KuadrantReconciler) checkLimitadorReady(ctx context.Context, kObj *kuad
return &reason, nil
}

reason := r.checkLimitadorReady(limitadorObj)
if reason != nil || err != nil {
return reason, err
}
reason, err = r.checkLimitadorUserReference(limitadorObj, kObj)
if reason != nil || err != nil {
return reason, err
}
return nil, nil
}

func (r *KuadrantReconciler) checkLimitadorUserReference(limitadorObj *limitadorv1alpha1.Limitador, kObj *kuadrantv1beta1.Kuadrant) (*string, error) {
tmp := &limitadorv1alpha1.Limitador{
ObjectMeta: metav1.ObjectMeta{
Name: "temp",
Namespace: kObj.Namespace,
},
}
err := r.SetOwnerReference(kObj, tmp)
if err != nil {
return nil, err
}

if limitadorObj.OwnerReferences != nil && !reflect.DeepEqual(tmp.OwnerReferences, limitadorObj.OwnerReferences) {
reason := fmt.Sprintf("Unable to reconcile limitador CR, Own Reference belongs to different controller %v", limitadorObj.OwnerReferences[0].Name)
return &reason, nil
}

return nil, nil
}

func (r *KuadrantReconciler) checkLimitadorReady(limitadorObj *limitadorv1alpha1.Limitador) *string {
statusConditionReady := meta.FindStatusCondition(limitadorObj.Status.Conditions, "Ready")
if statusConditionReady == nil {
reason := "Ready condition not found"
return &reason, nil
return &reason
}
if statusConditionReady.Status != metav1.ConditionTrue {
return &statusConditionReady.Message, nil
return &statusConditionReady.Message
}

return nil, nil
return nil
}

func (r *KuadrantReconciler) checkAuthorinoAvailable(ctx context.Context, kObj *kuadrantv1beta1.Kuadrant) (*string, error) {
Expand Down
5 changes: 5 additions & 0 deletions doc/reference/kuadrant.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## kuadrant

<details>
<summary>Note on Limitador</summary>
The Kuadrant operator creates a Limitador CR named `limitador` in the same namespace as the Kuadrant CR. If there is a pre-existing Limitador CR of the same name the kuadrant operator will take ownership of that Limitador CR. If the existing Limitador CR has the `ownerReference` set and owned by a different controller, the kuadrant operator will not take control of the limitador CR. The kuadrant operator will also not reconcile any limitador configuration when the CR is not owned by the kuadrant operator. This issue will be reflected in the Kuadrant CR status block.
</details>

| **Field** | **Type** | **Required** | **Description** |
|-----------|-----------------------------------|:------------:|-------------------------------------------------|
| `spec` | [KuadrantSpec](#kuadrantspec) | No | The specification for Kuadrant custom resource. |
Expand Down
5 changes: 5 additions & 0 deletions pkg/kuadranttools/limitador_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func LimitadorMutator(existingObj, desiredObj client.Object) (bool, error) {
return false, fmt.Errorf("desireObj %T is not a *limitadorv1alpha1.Limitador", desiredObj)
}

if !reflect.DeepEqual(existing.OwnerReferences, desired.OwnerReferences) {
update = true
existing.OwnerReferences = desired.OwnerReferences
}

Check warning on line 26 in pkg/kuadranttools/limitador_tools.go

View check run for this annotation

Codecov / codecov/patch

pkg/kuadranttools/limitador_tools.go#L24-L26

Added lines #L24 - L26 were not covered by tests

existingSpec := limitadorSpecSubSet(existing.Spec)
desiredSpec := limitadorSpecSubSet(desired.Spec)

Expand Down

0 comments on commit c029047

Please sign in to comment.