-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
263 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package mappers | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1" | ||
kuadrantv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2" | ||
"github.com/kuadrant/kuadrant-operator/pkg/library/utils" | ||
) | ||
|
||
type LimitadorToRateLimitPoliciesEventMapper struct { | ||
opts MapperOptions | ||
} | ||
|
||
func NewLimitadorToRateLimitPoliciesEventMapper(o ...MapperOption) *LimitadorToRateLimitPoliciesEventMapper { | ||
return &LimitadorToRateLimitPoliciesEventMapper{opts: Apply(o...)} | ||
} | ||
|
||
// Map limitador to RLP requests | ||
func (m *LimitadorToRateLimitPoliciesEventMapper) Map(ctx context.Context, obj client.Object) []reconcile.Request { | ||
kuadrantList := &kuadrantv1beta1.KuadrantList{} | ||
if err := m.opts.Client.List(ctx, kuadrantList, &client.ListOptions{Namespace: obj.GetNamespace()}); err != nil { | ||
m.opts.Logger.V(1).Error(err, "failed to list kuadrant in namespace", "namespace", obj.GetNamespace()) | ||
return []reconcile.Request{} | ||
} | ||
|
||
// No kuadrant in limitador namespace - skipping as it's not managed by kuadrant | ||
if len(kuadrantList.Items) == 0 { | ||
m.opts.Logger.V(1).Info("no kuadrant resources found in limitador namespace, skipping") | ||
return []reconcile.Request{} | ||
} | ||
|
||
// List all RLPs as there's been an event from Limitador which may affect RLP status | ||
rlpList := &kuadrantv1beta2.RateLimitPolicyList{} | ||
if err := m.opts.Client.List(ctx, rlpList); err != nil { | ||
m.opts.Logger.V(1).Error(err, "failed to list RLPs") | ||
return []reconcile.Request{} | ||
} | ||
|
||
return utils.Map(rlpList.Items, func(policy kuadrantv1beta2.RateLimitPolicy) reconcile.Request { | ||
return reconcile.Request{NamespacedName: types.NamespacedName{Namespace: policy.GetNamespace(), Name: policy.GetName()}} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package mappers | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
limitadorv1alpha1 "github.com/kuadrant/limitador-operator/api/v1alpha1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1" | ||
kuadrantv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2" | ||
"github.com/kuadrant/kuadrant-operator/pkg/log" | ||
) | ||
|
||
func TestLimitadorToRateLimitPoliciesEventMapper_Map(t *testing.T) { | ||
scheme := runtime.NewScheme() | ||
if err := kuadrantv1beta1.AddToScheme(scheme); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := kuadrantv1beta2.AddToScheme(scheme); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
type fields struct { | ||
opts MapperOptions | ||
} | ||
type args struct { | ||
ctx context.Context | ||
obj client.Object | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want []reconcile.Request | ||
}{ | ||
{ | ||
name: "no kuadrants in object ns", | ||
fields: fields{ | ||
opts: MapperOptions{Logger: log.NewLogger(), | ||
Client: fake.NewClientBuilder().WithObjects( | ||
&kuadrantv1beta1.Kuadrant{ObjectMeta: metav1.ObjectMeta{Namespace: "kuadrant"}}, | ||
).WithScheme(scheme).Build()}, | ||
}, | ||
args: args{ | ||
ctx: context.Background(), | ||
obj: &limitadorv1alpha1.Limitador{ObjectMeta: metav1.ObjectMeta{Namespace: "limitador"}}, | ||
}, | ||
want: []reconcile.Request{}, | ||
}, | ||
{ | ||
name: "kuadrant in object ns - map RLP to requests", | ||
fields: fields{ | ||
opts: MapperOptions{ | ||
Logger: log.NewLogger(), | ||
Client: fake.NewClientBuilder().WithObjects( | ||
&kuadrantv1beta1.Kuadrant{ObjectMeta: metav1.ObjectMeta{Namespace: "kuadrant"}}, | ||
&kuadrantv1beta2.RateLimitPolicy{ObjectMeta: metav1.ObjectMeta{Name: "rlp1", Namespace: "ns1"}}, | ||
&kuadrantv1beta2.RateLimitPolicy{ObjectMeta: metav1.ObjectMeta{Name: "rlp2", Namespace: "ns2"}}, | ||
).WithScheme(scheme).Build()}, | ||
}, | ||
args: args{ | ||
ctx: context.Background(), | ||
obj: &limitadorv1alpha1.Limitador{ObjectMeta: metav1.ObjectMeta{Namespace: "kuadrant"}}, | ||
}, | ||
want: []reconcile.Request{ | ||
{NamespacedName: types.NamespacedName{Name: "rlp1", Namespace: "ns1"}}, | ||
{NamespacedName: types.NamespacedName{Name: "rlp2", Namespace: "ns2"}}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
m := &LimitadorToRateLimitPoliciesEventMapper{ | ||
opts: tt.fields.opts, | ||
} | ||
if got := m.Map(tt.args.ctx, tt.args.obj); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Map() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |