-
Notifications
You must be signed in to change notification settings - Fork 58
fix(rbg-controller): override role.Replicas from bound RBGSA #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sebest
wants to merge
1
commit into
sgl-project:main
Choose a base branch
from
sebest:fix/rbg-prevent-helm-replicas-stomp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ import ( | |
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/interceptor" | ||
| "sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
| "sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
| "sigs.k8s.io/rbgs/pkg/reconciler" | ||
|
|
@@ -1018,6 +1019,200 @@ func TestRoleBasedGroupReconciler_ReconcileScalingAdapter_LabelUpdate(t *testing | |
| } | ||
| } | ||
|
|
||
| func TestRoleBasedGroupReconciler_applyRBGSAReplicasOverride(t *testing.T) { | ||
| testScheme := runtime.NewScheme() | ||
| _ = clientgoscheme.AddToScheme(testScheme) | ||
| _ = workloadsv1alpha2.AddToScheme(testScheme) | ||
|
|
||
| const ( | ||
| rbgName = "test-rbg" | ||
| ns = "default" | ||
| roleName = "leader" | ||
| ) | ||
| adapterName := scale.GenerateScalingAdapterName(rbgName, roleName) | ||
|
|
||
| makeRBG := func(roleReplicas *int32, scalingAdapterEnable bool) *workloadsv1alpha2.RoleBasedGroup { | ||
| role := workloadsv1alpha2.RoleSpec{ | ||
| Name: roleName, | ||
| Replicas: roleReplicas, | ||
| } | ||
| if scalingAdapterEnable { | ||
| role.ScalingAdapter = &workloadsv1alpha2.ScalingAdapter{Enable: true} | ||
| } | ||
| return &workloadsv1alpha2.RoleBasedGroup{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: rbgName, Namespace: ns}, | ||
| Spec: workloadsv1alpha2.RoleBasedGroupSpec{ | ||
| Roles: []workloadsv1alpha2.RoleSpec{role}, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| makeAdapter := func(phase constants.AdapterPhase, replicas *int32) *workloadsv1alpha2.RoleBasedGroupScalingAdapter { | ||
| return &workloadsv1alpha2.RoleBasedGroupScalingAdapter{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: adapterName, Namespace: ns}, | ||
| Spec: workloadsv1alpha2.RoleBasedGroupScalingAdapterSpec{ | ||
| Replicas: replicas, | ||
| }, | ||
| Status: workloadsv1alpha2.RoleBasedGroupScalingAdapterStatus{ | ||
| Phase: phase, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| rbg *workloadsv1alpha2.RoleBasedGroup | ||
| seedAdapter *workloadsv1alpha2.RoleBasedGroupScalingAdapter | ||
| expectedReplicas *int32 | ||
| expectEvent bool | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "scalingAdapter disabled: role.Replicas untouched", | ||
| rbg: makeRBG(ptr.To[int32](3), false), | ||
| seedAdapter: makeAdapter(constants.AdapterPhaseBound, ptr.To[int32](10)), | ||
| expectedReplicas: ptr.To[int32](3), | ||
| }, | ||
| { | ||
| name: "RBGSA missing: role.Replicas untouched", | ||
| rbg: makeRBG(ptr.To[int32](1), true), | ||
| seedAdapter: nil, | ||
| expectedReplicas: ptr.To[int32](1), | ||
| }, | ||
| { | ||
| name: "RBGSA unbound: role.Replicas untouched", | ||
| rbg: makeRBG(ptr.To[int32](1), true), | ||
| seedAdapter: makeAdapter(constants.AdapterPhaseNotBound, ptr.To[int32](10)), | ||
| expectedReplicas: ptr.To[int32](1), | ||
| }, | ||
| { | ||
| name: "RBGSA bound but spec.replicas nil: role.Replicas untouched", | ||
| rbg: makeRBG(ptr.To[int32](1), true), | ||
| seedAdapter: makeAdapter(constants.AdapterPhaseBound, nil), | ||
| expectedReplicas: ptr.To[int32](1), | ||
| }, | ||
| { | ||
| name: "RBGSA bound, replicas equal: no change", | ||
| rbg: makeRBG(ptr.To[int32](10), true), | ||
| seedAdapter: makeAdapter(constants.AdapterPhaseBound, ptr.To[int32](10)), | ||
| expectedReplicas: ptr.To[int32](10), | ||
| }, | ||
| { | ||
| name: "RBGSA bound, replicas differ: override to adapter value", | ||
| rbg: makeRBG(ptr.To[int32](1), true), | ||
| seedAdapter: makeAdapter(constants.AdapterPhaseBound, ptr.To[int32](10)), | ||
| expectedReplicas: ptr.To[int32](10), | ||
| expectEvent: true, | ||
| }, | ||
| { | ||
| name: "RBGSA bound, role.Replicas nil: override to adapter value", | ||
| rbg: makeRBG(nil, true), | ||
| seedAdapter: makeAdapter(constants.AdapterPhaseBound, ptr.To[int32](7)), | ||
| expectedReplicas: ptr.To[int32](7), | ||
| expectEvent: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| builder := fake.NewClientBuilder().WithScheme(testScheme) | ||
| if tt.seedAdapter != nil { | ||
| builder = builder.WithObjects(tt.seedAdapter) | ||
| } | ||
| fakeClient := builder.Build() | ||
| recorder := record.NewFakeRecorder(2) | ||
| r := &RoleBasedGroupReconciler{ | ||
| client: fakeClient, | ||
| apiReader: fakeClient, | ||
| scheme: testScheme, | ||
| recorder: recorder, | ||
| } | ||
|
|
||
| err := r.applyRBGSAReplicasOverride(context.Background(), tt.rbg) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Fatalf("applyRBGSAReplicasOverride() err=%v wantErr=%v", err, tt.wantErr) | ||
| } | ||
|
|
||
| got := tt.rbg.Spec.Roles[0].Replicas | ||
| switch { | ||
| case tt.expectedReplicas == nil && got != nil: | ||
| t.Fatalf("expected nil replicas, got %d", *got) | ||
| case tt.expectedReplicas != nil && got == nil: | ||
| t.Fatalf("expected %d replicas, got nil", *tt.expectedReplicas) | ||
| case tt.expectedReplicas != nil && got != nil && *tt.expectedReplicas != *got: | ||
| t.Fatalf("expected %d replicas, got %d", *tt.expectedReplicas, *got) | ||
| } | ||
|
|
||
| if tt.expectEvent { | ||
| select { | ||
| case ev := <-recorder.Events: | ||
| if !strings.Contains(ev, ReplicasOverriddenByAdapter) { | ||
| t.Errorf("expected event with reason %q, got %q", ReplicasOverriddenByAdapter, ev) | ||
| } | ||
| if !strings.Contains(ev, "Warning") { | ||
| t.Errorf("expected Warning event, got %q", ev) | ||
| } | ||
| default: | ||
| t.Errorf("expected an Event but none was recorded") | ||
| } | ||
| } else { | ||
| select { | ||
| case ev := <-recorder.Events: | ||
| t.Errorf("expected no Event, got %q", ev) | ||
| default: | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRoleBasedGroupReconciler_applyRBGSAReplicasOverride_GetError(t *testing.T) { | ||
| testScheme := runtime.NewScheme() | ||
| _ = clientgoscheme.AddToScheme(testScheme) | ||
| _ = workloadsv1alpha2.AddToScheme(testScheme) | ||
|
|
||
| rbg := &workloadsv1alpha2.RoleBasedGroup{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "rbg", Namespace: "default"}, | ||
| Spec: workloadsv1alpha2.RoleBasedGroupSpec{ | ||
| Roles: []workloadsv1alpha2.RoleSpec{ | ||
| { | ||
| Name: "leader", | ||
| Replicas: ptr.To[int32](3), | ||
| ScalingAdapter: &workloadsv1alpha2.ScalingAdapter{Enable: true}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| getErr := fmt.Errorf("transient apiserver error") | ||
| fakeClient := fake.NewClientBuilder(). | ||
| WithScheme(testScheme). | ||
| WithInterceptorFuncs(interceptor.Funcs{ | ||
| Get: func(ctx context.Context, c client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { | ||
| if _, ok := obj.(*workloadsv1alpha2.RoleBasedGroupScalingAdapter); ok { | ||
| return getErr | ||
| } | ||
| return c.Get(ctx, key, obj, opts...) | ||
| }, | ||
| }). | ||
| Build() | ||
|
|
||
| r := &RoleBasedGroupReconciler{ | ||
| client: fakeClient, | ||
| apiReader: fakeClient, | ||
| scheme: testScheme, | ||
| recorder: record.NewFakeRecorder(1), | ||
| } | ||
|
|
||
| err := r.applyRBGSAReplicasOverride(context.Background(), rbg) | ||
| if err == nil { | ||
| t.Fatal("expected error, got nil") | ||
| } | ||
| if !strings.Contains(err.Error(), "get scaling adapter") { | ||
| t.Fatalf("expected wrapped error mentioning 'get scaling adapter', got %v", err) | ||
| } | ||
|
Comment on lines
+1207
to
+1213
|
||
| } | ||
|
|
||
| func TestRoleBasedGroupReconciler_CleanupOrphanedScalingAdapters(t *testing.T) { | ||
| testScheme := runtime.NewScheme() | ||
| _ = clientgoscheme.AddToScheme(testScheme) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
applyRBGSAReplicasOverridereads the scaling adapter usingr.client.Get, which may be served from the controller-runtime cache. If the cache is stale (or the informer hasn’t observed the latest update yet), this can overriderole.Replicasto an out-of-date adapter value and potentially scale workloads incorrectly. Consider usingr.apiReader.Get(similar toupdateRBGStatus, which explicitly bypasses cache for freshness) for this read so the adapter is treated as the authoritative source of truth.