forked from opendatahub-io/models-as-a-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaassubscription_controller_test.go
More file actions
928 lines (824 loc) · 37.3 KB
/
maassubscription_controller_test.go
File metadata and controls
928 lines (824 loc) · 37.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
/*
Copyright 2025.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package maas
import (
"context"
"fmt"
"strings"
"testing"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
maasv1alpha1 "github.com/opendatahub-io/models-as-a-service/maas-controller/api/maas/v1alpha1"
)
// subscriptionModelRefIndexer is the field indexer function for MaaSSubscription.
// Extracted as a helper to be reused across tests with fake clients.
func subscriptionModelRefIndexer(obj client.Object) []string {
sub := obj.(*maasv1alpha1.MaaSSubscription)
var refs []string
for _, modelRef := range sub.Spec.ModelRefs {
refs = append(refs, modelRef.Namespace+"/"+modelRef.Name)
}
return refs
}
// newPreexistingTRLP builds a Kuadrant TokenRateLimitPolicy as an unstructured object
// with a sentinel value in spec.targetRef.name. Tests use this to detect whether
// the controller overwrote the spec or left it untouched.
func newPreexistingTRLP(name, namespace, modelName string, annotations map[string]string) *unstructured.Unstructured {
p := &unstructured.Unstructured{}
p.SetGroupVersionKind(schema.GroupVersionKind{Group: "kuadrant.io", Version: "v1alpha1", Kind: "TokenRateLimitPolicy"})
p.SetName(name)
p.SetNamespace(namespace)
p.SetLabels(map[string]string{
"maas.opendatahub.io/model": modelName,
"maas.opendatahub.io/model-namespace": namespace,
"app.kubernetes.io/managed-by": "maas-controller",
"app.kubernetes.io/part-of": "maas-subscription",
"app.kubernetes.io/component": "token-rate-limit-policy",
})
p.SetAnnotations(annotations)
_ = unstructured.SetNestedField(p.Object, "sentinel-route", "spec", "targetRef", "name")
return p
}
// TestMaaSSubscriptionReconciler_ManagedAnnotation verifies the opt-out behaviour of the
// "opendatahub.io/managed" annotation on generated Kuadrant TokenRateLimitPolicy resources.
//
// When the annotation is set to "false" on an existing TokenRateLimitPolicy, the controller
// must leave the resource untouched. Any other value (or the annotation being absent) means
// the controller owns the resource and must overwrite its spec.
func TestMaaSSubscriptionReconciler_ManagedAnnotation(t *testing.T) {
const (
modelName = "llm"
namespace = "default"
httpRouteName = "maas-model-" + modelName // ExternalModel naming convention
trlpName = "maas-trlp-" + modelName // generated by the controller
maasSubName = "sub-a"
)
tests := []struct {
name string
annotations map[string]string
wantSpecChanged bool // true → controller should overwrite spec; false → must leave it alone
}{
{
name: "annotation absent: controller overwrites spec",
annotations: map[string]string{},
wantSpecChanged: true,
},
{
name: "opendatahub.io/managed=false: controller skips update (opt-out)",
annotations: map[string]string{ManagedByODHOperator: "false"},
wantSpecChanged: false,
},
{
name: "opendatahub.io/managed=true: controller overwrites spec",
annotations: map[string]string{ManagedByODHOperator: "true"},
wantSpecChanged: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
model := newMaaSModelRef(modelName, namespace, "ExternalModel", modelName)
route := newHTTPRoute(httpRouteName, namespace)
// The subscription must have at least one owner group so that the membership
// check is non-empty and the controller writes a limits entry into the TokenRateLimitPolicy spec.
maasSub := newMaaSSubscription(maasSubName, namespace, "team-a", modelName, 100)
// Pre-populate the store with a generated TokenRateLimitPolicy whose spec contains a
// sentinel targetRef. After reconciliation we check whether it changed.
existingTRLP := newPreexistingTRLP(trlpName, namespace, modelName, tc.annotations)
c := fake.NewClientBuilder().
WithScheme(scheme).
WithRESTMapper(testRESTMapper()).
WithObjects(model, route, maasSub, existingTRLP).
WithStatusSubresource(&maasv1alpha1.MaaSSubscription{}).
WithIndex(&maasv1alpha1.MaaSSubscription{}, "spec.modelRef", subscriptionModelRefIndexer).
Build()
r := &MaaSSubscriptionReconciler{Client: c, Scheme: scheme}
req := ctrl.Request{NamespacedName: types.NamespacedName{Name: maasSubName, Namespace: namespace}}
if _, err := r.Reconcile(context.Background(), req); err != nil {
t.Fatalf("Reconcile: unexpected error: %v", err)
}
got := &unstructured.Unstructured{}
got.SetGroupVersionKind(schema.GroupVersionKind{Group: "kuadrant.io", Version: "v1alpha1", Kind: "TokenRateLimitPolicy"})
if err := c.Get(context.Background(), types.NamespacedName{Name: trlpName, Namespace: namespace}, got); err != nil {
t.Fatalf("Get TokenRateLimitPolicy %q: %v", trlpName, err)
}
// The controller sets spec.targetRef.name to the HTTPRoute name on update.
// A sentinel value means no update occurred.
targetRefName, found, err := unstructured.NestedString(got.Object, "spec", "targetRef", "name")
if err != nil || !found {
t.Fatalf("spec.targetRef.name missing or invalid: found=%v err=%v", found, err)
}
if tc.wantSpecChanged {
if targetRefName != httpRouteName {
t.Errorf("spec.targetRef.name = %q: expected %q", targetRefName, httpRouteName)
}
} else if targetRefName != "sentinel-route" {
t.Errorf("spec.targetRef.name = %q: expected sentinel %q (managed=false opt-out)", targetRefName, "sentinel-route")
}
})
}
}
// TestMaaSSubscriptionReconciler_DuplicateReconciliation verifies that reconciling
// multiple subscriptions for the same model does not produce redundant TokenRateLimitPolicy updates.
//
// When N subscriptions reference the same model, each reconciliation builds the same
// aggregated TokenRateLimitPolicy. The controller must skip the Update call when the
// content is identical to what already exists, otherwise each Update triggers a watch
// event that cascades into O(N²) reconciliations.
func TestMaaSSubscriptionReconciler_DuplicateReconciliation(t *testing.T) {
const (
modelName = "llm"
namespace = "default"
httpRouteName = "maas-model-" + modelName
trlpName = "maas-trlp-" + modelName
)
model := newMaaSModelRef(modelName, namespace, "ExternalModel", modelName)
route := newHTTPRoute(httpRouteName, namespace)
subA := newMaaSSubscription("sub-a", namespace, "team-a", modelName, 100)
subB := newMaaSSubscription("sub-b", namespace, "team-b", modelName, 200)
c := fake.NewClientBuilder().
WithScheme(scheme).
WithRESTMapper(testRESTMapper()).
WithObjects(model, route, subA, subB).
WithStatusSubresource(&maasv1alpha1.MaaSSubscription{}).
WithIndex(&maasv1alpha1.MaaSSubscription{}, "spec.modelRef", subscriptionModelRefIndexer).
Build()
r := &MaaSSubscriptionReconciler{Client: c, Scheme: scheme}
ctx := context.Background()
// Reconcile sub-a: creates the aggregated TokenRateLimitPolicy (covering both sub-a and sub-b).
reqA := ctrl.Request{NamespacedName: types.NamespacedName{Name: "sub-a", Namespace: namespace}}
if _, err := r.Reconcile(ctx, reqA); err != nil {
t.Fatalf("Reconcile sub-a: %v", err)
}
// Capture TokenRateLimitPolicy ResourceVersion after first reconciliation.
trlp := &unstructured.Unstructured{}
trlp.SetGroupVersionKind(schema.GroupVersionKind{Group: "kuadrant.io", Version: "v1alpha1", Kind: "TokenRateLimitPolicy"})
if err := c.Get(ctx, types.NamespacedName{Name: trlpName, Namespace: namespace}, trlp); err != nil {
t.Fatalf("Get TokenRateLimitPolicy after sub-a reconcile: %v", err)
}
rvAfterA := trlp.GetResourceVersion()
// Reconcile sub-b: both subscriptions are still present, so the aggregated TokenRateLimitPolicy
// content is identical. The controller should detect this and skip the Update.
reqB := ctrl.Request{NamespacedName: types.NamespacedName{Name: "sub-b", Namespace: namespace}}
if _, err := r.Reconcile(ctx, reqB); err != nil {
t.Fatalf("Reconcile sub-b: %v", err)
}
if err := c.Get(ctx, types.NamespacedName{Name: trlpName, Namespace: namespace}, trlp); err != nil {
t.Fatalf("Get TokenRateLimitPolicy after sub-b reconcile: %v", err)
}
rvAfterB := trlp.GetResourceVersion()
if rvAfterA != rvAfterB {
t.Errorf("redundant TokenRateLimitPolicy update: ResourceVersion changed from %s to %s; "+
"reconciling sub-b should not update the TokenRateLimitPolicy when content is identical to sub-a's reconciliation",
rvAfterA, rvAfterB)
}
}
// TestMaaSSubscriptionReconciler_SpecPriorityDuplicateCondition checks scanForDuplicatePriority in one pass:
// sub-a and sub-b share spec.priority; sub-c has a unique priority.
func TestMaaSSubscriptionReconciler_SpecPriorityDuplicateCondition(t *testing.T) {
const (
modelName = "llm"
namespace = "default"
httpRouteName = "maas-model-" + modelName
)
model := newMaaSModelRef(modelName, namespace, "ExternalModel", modelName)
route := newHTTPRoute(httpRouteName, namespace)
subA := newMaaSSubscription("sub-a", namespace, "team-a", modelName, 100)
subA.Spec.Priority = 5
subB := newMaaSSubscription("sub-b", namespace, "team-b", modelName, 200)
subB.Spec.Priority = 5
subC := newMaaSSubscription("sub-c", namespace, "team-c", modelName, 300)
subC.Spec.Priority = 10
c := fake.NewClientBuilder().
WithScheme(scheme).
WithRESTMapper(testRESTMapper()).
WithObjects(model, route, subA, subB, subC).
WithStatusSubresource(&maasv1alpha1.MaaSSubscription{}).
WithIndex(&maasv1alpha1.MaaSSubscription{}, "spec.modelRef", subscriptionModelRefIndexer).
Build()
r := &MaaSSubscriptionReconciler{Client: c, Scheme: scheme}
ctx := context.Background()
if _, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: "sub-a", Namespace: namespace}}); err != nil {
t.Fatalf("Reconcile sub-a: %v", err)
}
r.scanForDuplicatePriority(ctx)
assertSpecPriorityDuplicate := func(t *testing.T, subName string, wantTrue bool, mustContainPeer string) {
t.Helper()
var got maasv1alpha1.MaaSSubscription
if err := c.Get(ctx, types.NamespacedName{Name: subName, Namespace: namespace}, &got); err != nil {
t.Fatalf("Get %s: %v", subName, err)
}
cond := apimeta.FindStatusCondition(got.Status.Conditions, ConditionSpecPriorityDuplicate)
if cond == nil {
t.Fatalf("expected SpecPriorityDuplicate condition on %s", subName)
}
if wantTrue {
if cond.Status != metav1.ConditionTrue {
t.Fatalf("%s: SpecPriorityDuplicate.Status = %s, want True", subName, cond.Status)
}
if mustContainPeer != "" && !strings.Contains(cond.Message, mustContainPeer) {
t.Fatalf("%s: message should mention peer %q, got %q", subName, mustContainPeer, cond.Message)
}
} else if cond.Status != metav1.ConditionFalse {
t.Fatalf("%s: SpecPriorityDuplicate.Status = %s, want False", subName, cond.Status)
}
}
t.Run("sub-a shares priority with sub-b", func(t *testing.T) {
assertSpecPriorityDuplicate(t, "sub-a", true, namespace+"/sub-b")
})
t.Run("sub-b shares priority with sub-a", func(t *testing.T) {
assertSpecPriorityDuplicate(t, "sub-b", true, namespace+"/sub-a")
})
t.Run("sub-c has unique priority", func(t *testing.T) {
assertSpecPriorityDuplicate(t, "sub-c", false, "")
})
}
// TestMaaSSubscriptionReconciler_DeleteAnnotation verifies that the Reconcile deletion
// path respects the opt-out annotation: a TokenRateLimitPolicy with
// opendatahub.io/managed=false must not be deleted when the parent MaaSSubscription is removed.
func TestMaaSSubscriptionReconciler_DeleteAnnotation(t *testing.T) {
const (
modelName = "llm"
namespace = "default"
trlpName = "maas-trlp-" + modelName
maasSubName = "sub-a"
)
tests := []struct {
name string
annotations map[string]string
wantDeleted bool
}{
{
name: "annotation absent: controller deletes",
annotations: map[string]string{},
wantDeleted: true,
},
{
name: "opendatahub.io/managed=true: controller deletes",
annotations: map[string]string{ManagedByODHOperator: "true"},
wantDeleted: true,
},
{
name: "opendatahub.io/managed=false: controller must not delete",
annotations: map[string]string{ManagedByODHOperator: "false"},
wantDeleted: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
existingTRLP := newPreexistingTRLP(trlpName, namespace, modelName, tc.annotations)
// Create MaaSSubscription with finalizer so handleDeletion processes it.
maasSub := newMaaSSubscription(maasSubName, namespace, "team-a", modelName, 100)
maasSub.Finalizers = []string{maasSubscriptionFinalizer}
c := fake.NewClientBuilder().
WithScheme(scheme).
WithRESTMapper(testRESTMapper()).
WithObjects(maasSub, existingTRLP).
WithIndex(&maasv1alpha1.MaaSSubscription{}, "spec.modelRef", subscriptionModelRefIndexer).
Build()
// Simulate deletion: the fake client sets DeletionTimestamp while the
// finalizer keeps the object in the store.
if err := c.Delete(context.Background(), maasSub); err != nil {
t.Fatalf("Delete MaaSSubscription: %v", err)
}
r := &MaaSSubscriptionReconciler{Client: c, Scheme: scheme}
req := ctrl.Request{NamespacedName: types.NamespacedName{Name: maasSubName, Namespace: namespace}}
if _, err := r.Reconcile(context.Background(), req); err != nil {
t.Fatalf("Reconcile: unexpected error: %v", err)
}
got := &unstructured.Unstructured{}
got.SetGroupVersionKind(schema.GroupVersionKind{Group: "kuadrant.io", Version: "v1alpha1", Kind: "TokenRateLimitPolicy"})
err := c.Get(context.Background(), types.NamespacedName{Name: trlpName, Namespace: namespace}, got)
if tc.wantDeleted {
if !apierrors.IsNotFound(err) {
t.Errorf("expected TokenRateLimitPolicy %q to be deleted, but it still exists", trlpName)
}
} else {
if err != nil {
t.Errorf("expected TokenRateLimitPolicy %q to survive deletion (managed=false opt-out), but got: %v", trlpName, err)
}
}
})
}
}
// TestMaaSSubscriptionReconciler_RemoveModelRef verifies that removing a modelRef from
// a MaaSSubscription deletes the aggregated TokenRateLimitPolicy for the removed model
// while keeping the TRLP for the remaining model intact.
func TestMaaSSubscriptionReconciler_RemoveModelRef(t *testing.T) {
const (
modelA = "model-a"
modelB = "model-b"
namespace = "default"
httpRouteA = "maas-model-" + modelA
httpRouteB = "maas-model-" + modelB
trlpA = "maas-trlp-" + modelA
trlpB = "maas-trlp-" + modelB
subName = "sub-1"
)
modelRefA := newMaaSModelRef(modelA, namespace, "ExternalModel", modelA)
modelRefB := newMaaSModelRef(modelB, namespace, "ExternalModel", modelB)
routeA := newHTTPRoute(httpRouteA, namespace)
routeB := newHTTPRoute(httpRouteB, namespace)
sub := &maasv1alpha1.MaaSSubscription{
ObjectMeta: metav1.ObjectMeta{Name: subName, Namespace: namespace},
Spec: maasv1alpha1.MaaSSubscriptionSpec{
Owner: maasv1alpha1.OwnerSpec{Groups: []maasv1alpha1.GroupReference{{Name: "team-a"}}},
ModelRefs: []maasv1alpha1.ModelSubscriptionRef{
{Name: modelA, Namespace: namespace, TokenRateLimits: []maasv1alpha1.TokenRateLimit{{Limit: 100, Window: "1m"}}},
{Name: modelB, Namespace: namespace, TokenRateLimits: []maasv1alpha1.TokenRateLimit{{Limit: 200, Window: "1m"}}},
},
},
}
c := fake.NewClientBuilder().
WithScheme(scheme).
WithRESTMapper(testRESTMapper()).
WithObjects(modelRefA, modelRefB, routeA, routeB, sub).
WithStatusSubresource(&maasv1alpha1.MaaSSubscription{}).
WithIndex(&maasv1alpha1.MaaSSubscription{}, "spec.modelRef", subscriptionModelRefIndexer).
Build()
r := &MaaSSubscriptionReconciler{Client: c, Scheme: scheme}
ctx := context.Background()
req := ctrl.Request{NamespacedName: types.NamespacedName{Name: subName, Namespace: namespace}}
if _, err := r.Reconcile(ctx, req); err != nil {
t.Fatalf("Initial reconcile: %v", err)
}
// Both TRLPs should exist
for _, name := range []string{trlpA, trlpB} {
trlp := &unstructured.Unstructured{}
trlp.SetGroupVersionKind(schema.GroupVersionKind{Group: "kuadrant.io", Version: "v1alpha1", Kind: "TokenRateLimitPolicy"})
if err := c.Get(ctx, types.NamespacedName{Name: name, Namespace: namespace}, trlp); err != nil {
t.Fatalf("TRLP %q not found after initial reconcile: %v", name, err)
}
}
// Remove model B from the subscription
freshSub := &maasv1alpha1.MaaSSubscription{}
if err := c.Get(ctx, types.NamespacedName{Name: subName, Namespace: namespace}, freshSub); err != nil {
t.Fatalf("Get MaaSSubscription: %v", err)
}
freshSub.Spec.ModelRefs = []maasv1alpha1.ModelSubscriptionRef{
{Name: modelA, Namespace: namespace, TokenRateLimits: []maasv1alpha1.TokenRateLimit{{Limit: 100, Window: "1m"}}},
}
if err := c.Update(ctx, freshSub); err != nil {
t.Fatalf("Update MaaSSubscription: %v", err)
}
if _, err := r.Reconcile(ctx, req); err != nil {
t.Fatalf("Reconcile after removing modelRef: %v", err)
}
// TRLP for model A should still exist
trlpObjA := &unstructured.Unstructured{}
trlpObjA.SetGroupVersionKind(schema.GroupVersionKind{Group: "kuadrant.io", Version: "v1alpha1", Kind: "TokenRateLimitPolicy"})
if err := c.Get(ctx, types.NamespacedName{Name: trlpA, Namespace: namespace}, trlpObjA); err != nil {
t.Errorf("TRLP for model A should still exist: %v", err)
}
// TRLP for model B should be DELETED
trlpObjB := &unstructured.Unstructured{}
trlpObjB.SetGroupVersionKind(schema.GroupVersionKind{Group: "kuadrant.io", Version: "v1alpha1", Kind: "TokenRateLimitPolicy"})
err := c.Get(ctx, types.NamespacedName{Name: trlpB, Namespace: namespace}, trlpObjB)
if !apierrors.IsNotFound(err) {
t.Errorf("TRLP for model B should be deleted after removing modelRef, but got: %v", err)
}
}
// TestMaaSSubscriptionReconciler_RemoveModelRef_Aggregation verifies that when one
// subscription drops a model that another subscription still references, the aggregated
// TRLP is deleted (forcing a rebuild by the remaining subscription).
func TestMaaSSubscriptionReconciler_RemoveModelRef_Aggregation(t *testing.T) {
const (
modelA = "model-a"
modelB = "model-b"
namespace = "default"
httpRouteA = "maas-model-" + modelA
httpRouteB = "maas-model-" + modelB
trlpB = "maas-trlp-" + modelB
)
modelRefA := newMaaSModelRef(modelA, namespace, "ExternalModel", modelA)
modelRefB := newMaaSModelRef(modelB, namespace, "ExternalModel", modelB)
routeA := newHTTPRoute(httpRouteA, namespace)
routeB := newHTTPRoute(httpRouteB, namespace)
// sub1 references [A, B], sub2 references [B]
sub1 := &maasv1alpha1.MaaSSubscription{
ObjectMeta: metav1.ObjectMeta{Name: "sub1", Namespace: namespace},
Spec: maasv1alpha1.MaaSSubscriptionSpec{
Owner: maasv1alpha1.OwnerSpec{Groups: []maasv1alpha1.GroupReference{{Name: "team-1"}}},
ModelRefs: []maasv1alpha1.ModelSubscriptionRef{
{Name: modelA, Namespace: namespace, TokenRateLimits: []maasv1alpha1.TokenRateLimit{{Limit: 100, Window: "1m"}}},
{Name: modelB, Namespace: namespace, TokenRateLimits: []maasv1alpha1.TokenRateLimit{{Limit: 100, Window: "1m"}}},
},
},
}
sub2 := &maasv1alpha1.MaaSSubscription{
ObjectMeta: metav1.ObjectMeta{Name: "sub2", Namespace: namespace},
Spec: maasv1alpha1.MaaSSubscriptionSpec{
Owner: maasv1alpha1.OwnerSpec{Groups: []maasv1alpha1.GroupReference{{Name: "team-2"}}},
ModelRefs: []maasv1alpha1.ModelSubscriptionRef{
{Name: modelB, Namespace: namespace, TokenRateLimits: []maasv1alpha1.TokenRateLimit{{Limit: 200, Window: "1m"}}},
},
},
}
c := fake.NewClientBuilder().
WithScheme(scheme).
WithRESTMapper(testRESTMapper()).
WithObjects(modelRefA, modelRefB, routeA, routeB, sub1, sub2).
WithStatusSubresource(&maasv1alpha1.MaaSSubscription{}).
WithIndex(&maasv1alpha1.MaaSSubscription{}, "spec.modelRef", subscriptionModelRefIndexer).
Build()
r := &MaaSSubscriptionReconciler{Client: c, Scheme: scheme}
ctx := context.Background()
req1 := ctrl.Request{NamespacedName: types.NamespacedName{Name: "sub1", Namespace: namespace}}
req2 := ctrl.Request{NamespacedName: types.NamespacedName{Name: "sub2", Namespace: namespace}}
if _, err := r.Reconcile(ctx, req1); err != nil {
t.Fatalf("Reconcile sub1: %v", err)
}
if _, err := r.Reconcile(ctx, req2); err != nil {
t.Fatalf("Reconcile sub2: %v", err)
}
// TRLP for model B should exist with both subscriptions
trlpObj := &unstructured.Unstructured{}
trlpObj.SetGroupVersionKind(schema.GroupVersionKind{Group: "kuadrant.io", Version: "v1alpha1", Kind: "TokenRateLimitPolicy"})
if err := c.Get(ctx, types.NamespacedName{Name: trlpB, Namespace: namespace}, trlpObj); err != nil {
t.Fatalf("TRLP for model B not found: %v", err)
}
// Remove model B from sub1
freshSub1 := &maasv1alpha1.MaaSSubscription{}
if err := c.Get(ctx, types.NamespacedName{Name: "sub1", Namespace: namespace}, freshSub1); err != nil {
t.Fatalf("Get sub1: %v", err)
}
freshSub1.Spec.ModelRefs = []maasv1alpha1.ModelSubscriptionRef{
{Name: modelA, Namespace: namespace, TokenRateLimits: []maasv1alpha1.TokenRateLimit{{Limit: 100, Window: "1m"}}},
}
if err := c.Update(ctx, freshSub1); err != nil {
t.Fatalf("Update sub1: %v", err)
}
// Reconcile sub1 → should delete stale TRLP for model B
if _, err := r.Reconcile(ctx, req1); err != nil {
t.Fatalf("Reconcile sub1 after removing modelRef: %v", err)
}
trlpObj = &unstructured.Unstructured{}
trlpObj.SetGroupVersionKind(schema.GroupVersionKind{Group: "kuadrant.io", Version: "v1alpha1", Kind: "TokenRateLimitPolicy"})
err := c.Get(ctx, types.NamespacedName{Name: trlpB, Namespace: namespace}, trlpObj)
if !apierrors.IsNotFound(err) {
t.Fatalf("TRLP for model B should be deleted after sub1 drops it, but got: %v", err)
}
// Reconcile sub2 → should recreate TRLP for model B with only sub2's limits
if _, err := r.Reconcile(ctx, req2); err != nil {
t.Fatalf("Reconcile sub2 after sub1 drop: %v", err)
}
trlpObj = &unstructured.Unstructured{}
trlpObj.SetGroupVersionKind(schema.GroupVersionKind{Group: "kuadrant.io", Version: "v1alpha1", Kind: "TokenRateLimitPolicy"})
if err := c.Get(ctx, types.NamespacedName{Name: trlpB, Namespace: namespace}, trlpObj); err != nil {
t.Errorf("TRLP for model B should be rebuilt by sub2: %v", err)
}
// Verify only sub2 is in the annotation
ann := trlpObj.GetAnnotations()["maas.opendatahub.io/subscriptions"]
if ann != "sub2" {
t.Errorf("TRLP annotation = %q, want %q (only sub2 should contribute)", ann, "sub2")
}
}
// TestMaaSSubscriptionReconciler_MultipleSubscriptionsDeletion verifies that when multiple
// MaaSSubscriptions reference the same model, deleting one does not delete the aggregated
// TokenRateLimitPolicy, but deleting the last one does.
func TestMaaSSubscriptionReconciler_MultipleSubscriptionsDeletion(t *testing.T) {
const (
modelName = "shared-model"
modelNamespace = "llm"
httpRouteName = "maas-model-" + modelName
trlpName = "maas-trlp-" + modelName
sub1Name = "subscription-1"
sub2Name = "subscription-2"
subNS = "opendatahub"
)
// Create model and HTTPRoute
model := &maasv1alpha1.MaaSModelRef{
ObjectMeta: metav1.ObjectMeta{Name: modelName, Namespace: modelNamespace},
Spec: maasv1alpha1.MaaSModelSpec{
ModelRef: maasv1alpha1.ModelReference{Kind: "ExternalModel", Name: modelName},
},
}
route := &gatewayapiv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{Name: httpRouteName, Namespace: modelNamespace},
}
// Create two MaaSSubscriptions both referencing the same model
sub1 := &maasv1alpha1.MaaSSubscription{
ObjectMeta: metav1.ObjectMeta{
Name: sub1Name,
Namespace: subNS,
Finalizers: []string{maasSubscriptionFinalizer},
},
Spec: maasv1alpha1.MaaSSubscriptionSpec{
Owner: maasv1alpha1.OwnerSpec{Groups: []maasv1alpha1.GroupReference{{Name: "team-1"}}},
ModelRefs: []maasv1alpha1.ModelSubscriptionRef{
{
Name: modelName,
Namespace: modelNamespace,
TokenRateLimits: []maasv1alpha1.TokenRateLimit{{Limit: 100, Window: "1m"}},
},
},
},
}
sub2 := &maasv1alpha1.MaaSSubscription{
ObjectMeta: metav1.ObjectMeta{
Name: sub2Name,
Namespace: subNS,
Finalizers: []string{maasSubscriptionFinalizer},
},
Spec: maasv1alpha1.MaaSSubscriptionSpec{
Owner: maasv1alpha1.OwnerSpec{Groups: []maasv1alpha1.GroupReference{{Name: "team-2"}}},
ModelRefs: []maasv1alpha1.ModelSubscriptionRef{
{
Name: modelName,
Namespace: modelNamespace,
TokenRateLimits: []maasv1alpha1.TokenRateLimit{{Limit: 200, Window: "1m"}},
},
},
},
}
c := fake.NewClientBuilder().
WithScheme(scheme).
WithRESTMapper(testRESTMapper()).
WithObjects(model, route, sub1, sub2).
WithStatusSubresource(&maasv1alpha1.MaaSSubscription{}).
WithIndex(&maasv1alpha1.MaaSSubscription{}, "spec.modelRef", subscriptionModelRefIndexer).
Build()
r := &MaaSSubscriptionReconciler{Client: c, Scheme: scheme}
// Reconcile both subscriptions to create the aggregated TokenRateLimitPolicy
req1 := ctrl.Request{NamespacedName: types.NamespacedName{Name: sub1Name, Namespace: subNS}}
if _, err := r.Reconcile(context.Background(), req1); err != nil {
t.Fatalf("Reconcile sub1: %v", err)
}
req2 := ctrl.Request{NamespacedName: types.NamespacedName{Name: sub2Name, Namespace: subNS}}
if _, err := r.Reconcile(context.Background(), req2); err != nil {
t.Fatalf("Reconcile sub2: %v", err)
}
// Verify aggregated TRLP was created
trlp := &unstructured.Unstructured{}
trlp.SetGroupVersionKind(schema.GroupVersionKind{Group: "kuadrant.io", Version: "v1alpha1", Kind: "TokenRateLimitPolicy"})
if err := c.Get(context.Background(), types.NamespacedName{Name: trlpName, Namespace: modelNamespace}, trlp); err != nil {
t.Fatalf("TokenRateLimitPolicy not found before deletion: %v", err)
}
// Delete sub1 (but sub2 still exists)
if err := c.Delete(context.Background(), sub1); err != nil {
t.Fatalf("Delete sub1: %v", err)
}
// Reconcile sub1 deletion - this should rebuild the TRLP in-place without sub1's limits
if _, err := r.Reconcile(context.Background(), req1); err != nil {
t.Fatalf("Reconcile sub1 deletion: %v", err)
}
// Aggregated TRLP should still exist (rebuilt in-place by sub1's deletion handler)
trlp = &unstructured.Unstructured{}
trlp.SetGroupVersionKind(schema.GroupVersionKind{Group: "kuadrant.io", Version: "v1alpha1", Kind: "TokenRateLimitPolicy"})
if err := c.Get(context.Background(), types.NamespacedName{Name: trlpName, Namespace: modelNamespace}, trlp); err != nil {
t.Fatalf("TokenRateLimitPolicy should still exist after sub1 deletion (rebuilt in-place): %v", err)
}
// Verify TRLP only contains sub2's limits (sub1's limits should be removed)
limits, found, err := unstructured.NestedMap(trlp.Object, "spec", "limits")
if err != nil || !found {
t.Fatalf("failed to get spec.limits from TRLP: found=%v err=%v", found, err)
}
// Check that sub2's limit exists (map keys use namespace-name format)
sub2Key := fmt.Sprintf("%s-%s-%s-tokens", subNS, sub2Name, modelName)
if _, exists := limits[sub2Key]; !exists {
t.Errorf("TRLP should contain limits for %s after sub1 deletion", sub2Key)
}
// Check that sub1's limit is removed
sub1Key := fmt.Sprintf("%s-%s-%s-tokens", subNS, sub1Name, modelName)
if _, exists := limits[sub1Key]; exists {
t.Errorf("TRLP should NOT contain limits for %s after its deletion", sub1Key)
}
// Verify subscription annotation only contains sub2
annotations := trlp.GetAnnotations()
if subs, ok := annotations["maas.opendatahub.io/subscriptions"]; !ok || subs != sub2Name {
t.Errorf("TRLP annotation should only contain %s, got: %s", sub2Name, subs)
}
// Now delete sub2 (the last one)
if err := c.Delete(context.Background(), sub2); err != nil {
t.Fatalf("Delete sub2: %v", err)
}
if _, err := r.Reconcile(context.Background(), req2); err != nil {
t.Fatalf("Reconcile sub2 deletion: %v", err)
}
// Aggregated TRLP should NOW BE DELETED (no remaining parents)
trlp = &unstructured.Unstructured{}
trlp.SetGroupVersionKind(schema.GroupVersionKind{Group: "kuadrant.io", Version: "v1alpha1", Kind: "TokenRateLimitPolicy"})
err = c.Get(context.Background(), types.NamespacedName{Name: trlpName, Namespace: modelNamespace}, trlp)
if !apierrors.IsNotFound(err) {
t.Errorf("TokenRateLimitPolicy should be deleted after deleting last parent subscription, but got error: %v", err)
}
}
// TestMaaSSubscriptionReconciler_SimplifiedTRLP verifies the TRLP no longer contains
// membership checks, header validation, or deny rules. It should trust auth.identity.selected_subscription.
func TestMaaSSubscriptionReconciler_SimplifiedTRLP(t *testing.T) {
const (
modelName = "llm"
namespace = "default"
httpRouteName = "maas-model-" + modelName
trlpName = "maas-trlp-" + modelName
maasSubName = "sub-a"
)
model := newMaaSModelRef(modelName, namespace, "ExternalModel", modelName)
route := newHTTPRoute(httpRouteName, namespace)
maasSub := newMaaSSubscription(maasSubName, namespace, "team-a", modelName, 100)
c := fake.NewClientBuilder().
WithScheme(scheme).
WithRESTMapper(testRESTMapper()).
WithObjects(model, route, maasSub).
WithStatusSubresource(&maasv1alpha1.MaaSSubscription{}).
WithIndex(&maasv1alpha1.MaaSSubscription{}, "spec.modelRef", subscriptionModelRefIndexer).
Build()
r := &MaaSSubscriptionReconciler{Client: c, Scheme: scheme}
req := ctrl.Request{NamespacedName: types.NamespacedName{Name: maasSubName, Namespace: namespace}}
if _, err := r.Reconcile(context.Background(), req); err != nil {
t.Fatalf("Reconcile: unexpected error: %v", err)
}
trlp := &unstructured.Unstructured{}
trlp.SetGroupVersionKind(schema.GroupVersionKind{Group: "kuadrant.io", Version: "v1alpha1", Kind: "TokenRateLimitPolicy"})
if err := c.Get(context.Background(), types.NamespacedName{Name: trlpName, Namespace: namespace}, trlp); err != nil {
t.Fatalf("Get TokenRateLimitPolicy %q: %v", trlpName, err)
}
limitsMap, found, err := unstructured.NestedMap(trlp.Object, "spec", "limits")
if err != nil || !found {
t.Fatalf("spec.limits not found: found=%v err=%v", found, err)
}
// Should have exactly 1 limit entry (no deny rules)
if len(limitsMap) != 1 {
t.Errorf("expected 1 limit entry, got %d: %v", len(limitsMap), limitsMap)
}
// Check the limit entry key (now includes namespace: "namespace-name-model-tokens")
expectedKey := namespace + "-" + maasSubName + "-" + modelName + "-tokens"
limitEntry, ok := limitsMap[expectedKey]
if !ok {
t.Fatalf("expected limit entry %q not found, got keys: %v", expectedKey, getKeys(limitsMap))
}
// Verify it has a single, simple predicate
limitMap, ok := limitEntry.(map[string]any)
if !ok {
t.Fatalf("limitEntry is not map[string]interface{}: %T", limitEntry)
}
whenSlice, found, err := unstructured.NestedSlice(limitMap, "when")
if err != nil || !found {
t.Fatalf("when predicates not found: found=%v err=%v", found, err)
}
if len(whenSlice) != 1 {
t.Errorf("expected 1 when predicate, got %d", len(whenSlice))
}
predMap, ok := whenSlice[0].(map[string]any)
if !ok {
t.Fatalf("whenSlice[0] is not map[string]interface{}: %T", whenSlice[0])
}
pred, ok := predMap["predicate"].(string)
if !ok {
t.Fatalf("predicate not a string: %T", predMap["predicate"])
}
// Predicate now uses model-scoped key: namespace/name@modelNamespace/modelName
expected := fmt.Sprintf(`auth.identity.selected_subscription_key == "%s/%s@%s/%s"`, namespace, maasSubName, namespace, modelName)
if pred != expected {
t.Errorf("predicate = %q, want %q", pred, expected)
}
// Verify no membership/header checks in predicate
if strings.Contains(pred, "groups_str") || strings.Contains(pred, "request.headers") || strings.Contains(pred, "exists(") {
t.Errorf("predicate should not contain membership/header checks: %s", pred)
}
// Verify no deny rules
for key := range limitsMap {
if strings.Contains(key, "deny-") {
t.Errorf("found deny rule %q, expected none", key)
}
}
}
// TestMaaSSubscriptionReconciler_MultipleSubscriptionsSimplified verifies that
// multiple subscriptions generate simple predicates without exclusion logic.
func TestMaaSSubscriptionReconciler_MultipleSubscriptionsSimplified(t *testing.T) {
const (
modelName = "llm"
namespace = "default"
httpRouteName = "maas-model-" + modelName
trlpName = "maas-trlp-" + modelName
)
model := newMaaSModelRef(modelName, namespace, "ExternalModel", modelName)
route := newHTTPRoute(httpRouteName, namespace)
subA := newMaaSSubscription("sub-a", namespace, "team-a", modelName, 100)
subB := newMaaSSubscription("sub-b", namespace, "team-b", modelName, 200)
c := fake.NewClientBuilder().
WithScheme(scheme).
WithRESTMapper(testRESTMapper()).
WithObjects(model, route, subA, subB).
WithStatusSubresource(&maasv1alpha1.MaaSSubscription{}).
WithIndex(&maasv1alpha1.MaaSSubscription{}, "spec.modelRef", subscriptionModelRefIndexer).
Build()
r := &MaaSSubscriptionReconciler{Client: c, Scheme: scheme}
// Reconcile both subscriptions
reqA := ctrl.Request{NamespacedName: types.NamespacedName{Name: "sub-a", Namespace: namespace}}
if _, err := r.Reconcile(context.Background(), reqA); err != nil {
t.Fatalf("Reconcile sub-a: %v", err)
}
trlp := &unstructured.Unstructured{}
trlp.SetGroupVersionKind(schema.GroupVersionKind{Group: "kuadrant.io", Version: "v1alpha1", Kind: "TokenRateLimitPolicy"})
if err := c.Get(context.Background(), types.NamespacedName{Name: trlpName, Namespace: namespace}, trlp); err != nil {
t.Fatalf("Get TokenRateLimitPolicy: %v", err)
}
limitsMap, found, err := unstructured.NestedMap(trlp.Object, "spec", "limits")
if err != nil || !found {
t.Fatalf("spec.limits not found: found=%v err=%v", found, err)
}
// Should have exactly 2 limit entries (one per subscription, no deny rules)
if len(limitsMap) != 2 {
t.Errorf("expected 2 limit entries, got %d: %v", len(limitsMap), getKeys(limitsMap))
}
// Verify sub-a limit entry (now includes namespace in key)
subAKey := namespace + "-sub-a-" + modelName + "-tokens"
if limitA, ok := limitsMap[subAKey]; ok {
limitAMap, ok := limitA.(map[string]any)
if !ok {
t.Fatalf("sub-a limitEntry is not map[string]interface{}: %T", limitA)
}
whenSlice, found, err := unstructured.NestedSlice(limitAMap, "when")
if err != nil || !found {
t.Fatalf("sub-a when predicates not found: found=%v err=%v", found, err)
}
if len(whenSlice) != 1 {
t.Errorf("sub-a: expected 1 when predicate, got %d", len(whenSlice))
}
predMap, ok := whenSlice[0].(map[string]any)
if !ok {
t.Fatalf("sub-a whenSlice[0] is not map[string]interface{}: %T", whenSlice[0])
}
pred, ok := predMap["predicate"].(string)
if !ok {
t.Fatalf("sub-a predicate not a string: %T", predMap["predicate"])
}
// Predicate now uses model-scoped key: namespace/name@modelNamespace/modelName
expected := fmt.Sprintf(`auth.identity.selected_subscription_key == "%s/sub-a@%s/%s"`, namespace, namespace, modelName)
if pred != expected {
t.Errorf("sub-a predicate = %q, want %q", pred, expected)
}
// No exclusion logic (no reference to sub-b or team-b)
if strings.Contains(pred, "sub-b") || strings.Contains(pred, "team-b") {
t.Errorf("sub-a predicate should not reference sub-b: %s", pred)
}
} else {
t.Errorf("sub-a limit entry not found, got keys: %v", getKeys(limitsMap))
}
// Verify sub-b limit entry (now includes namespace in key)
subBKey := namespace + "-sub-b-" + modelName + "-tokens"
if limitB, ok := limitsMap[subBKey]; ok {
limitBMap, ok := limitB.(map[string]any)
if !ok {
t.Fatalf("sub-b limitEntry is not map[string]interface{}: %T", limitB)
}
whenSlice, found, err := unstructured.NestedSlice(limitBMap, "when")
if err != nil || !found {
t.Fatalf("sub-b when predicates not found: found=%v err=%v", found, err)
}
if len(whenSlice) != 1 {
t.Errorf("sub-b: expected 1 when predicate, got %d", len(whenSlice))
}
predMap, ok := whenSlice[0].(map[string]any)
if !ok {
t.Fatalf("sub-b whenSlice[0] is not map[string]interface{}: %T", whenSlice[0])
}
pred, ok := predMap["predicate"].(string)
if !ok {
t.Fatalf("sub-b predicate not a string: %T", predMap["predicate"])
}
// Predicate now uses model-scoped key: namespace/name@modelNamespace/modelName
expected := fmt.Sprintf(`auth.identity.selected_subscription_key == "%s/sub-b@%s/%s"`, namespace, namespace, modelName)
if pred != expected {
t.Errorf("sub-b predicate = %q, want %q", pred, expected)
}
// No exclusion logic (no reference to sub-a or team-a)
if strings.Contains(pred, "sub-a") || strings.Contains(pred, "team-a") {
t.Errorf("sub-b predicate should not reference sub-a: %s", pred)
}
} else {
t.Errorf("sub-b limit entry not found, got keys: %v", getKeys(limitsMap))
}
// Verify no deny rules
for key := range limitsMap {
if strings.Contains(key, "deny-") {
t.Errorf("found deny rule %q, expected none", key)
}
}
}
// Helper to get map keys
func getKeys(m map[string]any) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}