-
Notifications
You must be signed in to change notification settings - Fork 256
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
update controller runtime #1800
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,22 +18,42 @@ package testing | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
"sigs.k8s.io/controller-runtime/pkg/client/interceptor" | ||
|
||
kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1" | ||
"sigs.k8s.io/kueue/pkg/controller/core/indexer" | ||
) | ||
|
||
func NewFakeClient(objs ...client.Object) client.Client { | ||
return NewClientBuilder().WithObjects(objs...).WithStatusSubresource(objs...).Build() | ||
return NewClientBuilder().WithObjects(objs...).WithStatusSubresource(objs...). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, this function already calls NewclientBuilder, so no need to add the interceptor again. |
||
WithInterceptorFuncs(interceptor.Funcs{SubResourcePatch: func(ctx context.Context, clnt client.Client, SubResourceName string, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error { | ||
// Apply patches are supposed to upsert, but fake client fails if the object doesn't exist, | ||
// if an apply patch occurs for an object that doesn't yet exist, create it. | ||
if patch.Type() != types.ApplyPatchType { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a debugger, I can confirm that we are hitting this function now. I don't really know what behavior we actually want in this function though. I took this code from an example about how to work around SSA patch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The existing logic is just trying to use a Create instead of a Patch when the object doesn't exist. That's one part of the problem, although we never use SSA for objects that don't exist in Kueue. The next one is the patch type. We need to make it a StrategicMergePatch. |
||
return clnt.Patch(ctx, obj, patch, &client.PatchOptions{}) | ||
} | ||
check, ok := obj.DeepCopyObject().(client.Object) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would expect you to override the type to Also, maybe we should have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only issue is that we hit loads of these failures throughout Kueue. Its whatever UpdateStatus is called so there are quite a few tests that fail with this. |
||
if !ok { | ||
return errors.New("could not check for object in fake client") | ||
} | ||
if err := clnt.Get(ctx, client.ObjectKeyFromObject(obj), check); apierrors.IsNotFound(err) { | ||
if err := clnt.Create(ctx, check); err != nil { | ||
return fmt.Errorf("could not inject object creation for fake: %w", err) | ||
} | ||
} | ||
return clnt.Patch(ctx, obj, patch, &client.PatchOptions{}) | ||
}}).Build() | ||
} | ||
|
||
func NewClientBuilder(addToSchemes ...func(s *runtime.Scheme) error) *fake.ClientBuilder { | ||
|
@@ -49,12 +69,28 @@ func NewClientBuilder(addToSchemes ...func(s *runtime.Scheme) error) *fake.Clien | |
panic(err) | ||
} | ||
} | ||
|
||
return fake.NewClientBuilder().WithScheme(scheme). | ||
WithIndex(&kueue.LocalQueue{}, indexer.QueueClusterQueueKey, indexer.IndexQueueClusterQueue). | ||
WithIndex(&kueue.Workload{}, indexer.WorkloadQueueKey, indexer.IndexWorkloadQueue). | ||
WithIndex(&kueue.Workload{}, indexer.WorkloadClusterQueueKey, indexer.IndexWorkloadClusterQueue). | ||
WithIndex(&kueue.Workload{}, indexer.OwnerReferenceUID, indexer.IndexOwnerUID) | ||
WithIndex(&kueue.Workload{}, indexer.OwnerReferenceUID, indexer.IndexOwnerUID). | ||
WithInterceptorFuncs(interceptor.Funcs{SubResourcePatch: func(ctx context.Context, clnt client.Client, SubResourceName string, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error { | ||
// Apply patches are supposed to upsert, but fake client fails if the object doesn't exist, | ||
// if an apply patch occurs for an object that doesn't yet exist, create it. | ||
if patch.Type() != types.ApplyPatchType { | ||
return clnt.Patch(ctx, obj, patch, &client.PatchOptions{}) | ||
} | ||
check, ok := obj.DeepCopyObject().(client.Object) | ||
if !ok { | ||
return errors.New("could not check for object in fake client") | ||
} | ||
if err := clnt.Get(ctx, client.ObjectKeyFromObject(obj), check); apierrors.IsNotFound(err) { | ||
if err := clnt.Create(ctx, check); err != nil { | ||
return fmt.Errorf("could not inject object creation for fake: %w", err) | ||
} | ||
} | ||
return clnt.Patch(ctx, obj, patch, &client.PatchOptions{}) | ||
}}) | ||
} | ||
|
||
type builderIndexer struct { | ||
|
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.
I wold just go with something like:
Then in the tests that require this , use this function as
Note: It cannot be just added in 'NewClientBuilder' since:
WithInterceptorFuncs
that might be needed afterNewClientBuilder
return will replace it.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.
We don't declare manageBuilder anywhere other than in multikueue.
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.
That is just an usage example