Skip to content

Commit 2e8c98e

Browse files
committed
Let ReactionChain continue in EmulateProviderConfigLabelingWebhook
Previously, the reactor manually called tracker.Add and returned handled=true, short-circuiting the chain and sometimes causing double-add/AlreadyExists. Now we only label the object and return handled=false so the default ObjectReaction persists it, reducing flakes. Also initialize the labels map and remove the unused FakeTracker parameter.
1 parent 67a075a commit 2e8c98e

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

pkg/multiproject/start/start_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func TestStartProviderConfigIntegration(t *testing.T) {
207207

208208
// This simulates the automatic labeling that the real environment does.
209209
// ProviderConfig name label is set to the namespace of the object.
210-
testutil.EmulateProviderConfigLabelingWebhook(svcNegClient.Tracker(), &svcNegClient.Fake, "servicenetworkendpointgroups")
210+
testutil.EmulateProviderConfigLabelingWebhook(&svcNegClient.Fake, "servicenetworkendpointgroups")
211211

212212
logger := klog.TODO()
213213
gceCreator := multiprojectgce.NewGCEFake()
@@ -326,7 +326,7 @@ func TestSharedInformers_PC1Stops_PC2AndPC3KeepWorking(t *testing.T) {
326326
nodeTopoClient := nodetopologyfake.NewSimpleClientset()
327327

328328
// Simulate webhook: label SvcNEGs with provider-config name == namespace.
329-
testutil.EmulateProviderConfigLabelingWebhook(svcNegClient.Tracker(), &svcNegClient.Fake, "servicenetworkendpointgroups")
329+
testutil.EmulateProviderConfigLabelingWebhook(&svcNegClient.Fake, "servicenetworkendpointgroups")
330330

331331
informersFactory := informers.NewSharedInformerFactoryWithOptions(kubeClient, flags.F.ResyncPeriod)
332332
svcNegFactory := informersvcneg.NewSharedInformerFactoryWithOptions(svcNegClient, flags.F.ResyncPeriod)
@@ -936,7 +936,7 @@ func TestProviderConfigErrorCases(t *testing.T) {
936936
networkClient := networkfake.NewSimpleClientset()
937937
nodeTopologyClient := nodetopologyfake.NewSimpleClientset()
938938

939-
testutil.EmulateProviderConfigLabelingWebhook(svcNegClient.Tracker(), &svcNegClient.Fake, "servicenetworkendpointgroups")
939+
testutil.EmulateProviderConfigLabelingWebhook(&svcNegClient.Fake, "servicenetworkendpointgroups")
940940

941941
logger := klog.TODO()
942942
gceCreator := multiprojectgce.NewGCEFake()

pkg/multiproject/testutil/providerconfigwebhook.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import (
88
klog "k8s.io/klog/v2"
99
)
1010

11-
type FakeTracker interface {
12-
Add(obj runtime.Object) error
13-
}
14-
1511
// EmulateProviderConfigLabelingWebhook is a helper function that emulates the behaviour
1612
// of the providerconfig webhook.
1713
// It will set the providerconfig name label on the object, on creation.
@@ -22,19 +18,32 @@ type FakeTracker interface {
2218
// However, in the real world, multiple namespaces can have the same providerconfig name.
2319
//
2420
// The function takes a fake client and the name of the CRD.
25-
func EmulateProviderConfigLabelingWebhook(tracker FakeTracker, fake *testing.Fake, crName string) {
21+
func EmulateProviderConfigLabelingWebhook(fake *testing.Fake, crName string) {
2622
fake.PrependReactor("create", crName, func(action testing.Action) (handled bool, ret runtime.Object, err error) {
27-
createAction := action.(testing.CreateAction)
23+
createAction, ok := action.(testing.CreateAction)
24+
if !ok {
25+
return false, nil, nil
26+
}
27+
2828
obj := createAction.GetObject()
29-
pc := obj.(metav1.Object)
30-
pc.GetLabels()[flags.F.ProviderConfigNameLabelKey] = pc.GetNamespace()
29+
pc, ok := obj.(metav1.Object)
30+
if !ok {
31+
klog.Errorf("EmulateProviderConfigWebhook: object does not implement metav1.Object: %T", obj)
32+
return false, nil, nil
33+
}
3134

32-
err = tracker.Add(obj)
33-
if err != nil {
34-
klog.Errorf("Failed to add object to tracker: %v", err)
35+
labels := pc.GetLabels()
36+
if labels == nil {
37+
labels = map[string]string{}
3538
}
39+
labels[flags.F.ProviderConfigNameLabelKey] = pc.GetNamespace()
40+
pc.SetLabels(labels)
3641

3742
klog.Infof("EmulateProviderConfigWebhook: %s/%s", pc.GetNamespace(), pc.GetName())
38-
return true, obj, nil
43+
44+
// Returning false means the ReactionChain will continue and the object will be
45+
// stored by the standard fake reactors after we mutated it. If we returned true,
46+
// the reaction chain would stop here.
47+
return false, nil, nil
3948
})
4049
}

0 commit comments

Comments
 (0)