Skip to content

Commit d89c544

Browse files
committed
Adopt controller-runtime change PR3229: fake client cleared apiversion/kind
Signed-off-by: RainbowMango <[email protected]>
1 parent f4b11d8 commit d89c544

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

pkg/controllers/execution/execution_controller.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,16 @@ func (c *Controller) updateWorkDispatchingConditionIfNeeded(ctx context.Context,
358358
return err
359359
}
360360

361+
if work.GetObjectKind().GroupVersionKind().Empty() {
362+
// In unit tests, we need to verify how many events are emitted. However, when using the fake client,
363+
// the work object may not have TypeMeta (APIVersion/Kind) set, which prevents building a proper
364+
// object reference and thus no event is actually emitted. This workaround ensures the GVK is set
365+
// so event emission, and thereby event counting in unit tests, works as expected.
366+
// Since controller-runtime v0.22.0, the group version kind is not set when using fake client.
367+
// See https://github.com/kubernetes-sigs/controller-runtime/pull/3229 for more details.
368+
work.SetGroupVersionKind(workv1alpha1.SchemeGroupVersion.WithKind("Work"))
369+
}
370+
361371
obj, err := helper.ToUnstructured(work)
362372
if err != nil {
363373
return err

pkg/controllers/namespace/namespace_sync_controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Reques
8282

8383
return controllerruntime.Result{}, err
8484
}
85+
if namespace.GetObjectKind().GroupVersionKind().Empty() {
86+
// In unit tests, we need to verify that a work object is created for the corresponding namespace.
87+
// However, the fake client does not set TypeMeta information, which results in an incorrect generated work object name.
88+
// This ensures correct GVK is set so work name generation in tests works as expected.
89+
// Since controller-runtime v0.22.0, the group version kind is not set when using fake client.
90+
// See https://github.com/kubernetes-sigs/controller-runtime/pull/3229 for more details.
91+
namespace.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Namespace"))
92+
}
8593

8694
if !namespace.DeletionTimestamp.IsZero() {
8795
// Do nothing, just return as we have added owner reference to Work.

0 commit comments

Comments
 (0)