@@ -8,10 +8,6 @@ import (
8
8
klog "k8s.io/klog/v2"
9
9
)
10
10
11
- type FakeTracker interface {
12
- Add (obj runtime.Object ) error
13
- }
14
-
15
11
// EmulateProviderConfigLabelingWebhook is a helper function that emulates the behaviour
16
12
// of the providerconfig webhook.
17
13
// It will set the providerconfig name label on the object, on creation.
@@ -22,19 +18,32 @@ type FakeTracker interface {
22
18
// However, in the real world, multiple namespaces can have the same providerconfig name.
23
19
//
24
20
// 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 ) {
26
22
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
+
28
28
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
+ }
31
34
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 {}
35
38
}
39
+ labels [flags .F .ProviderConfigNameLabelKey ] = pc .GetNamespace ()
40
+ pc .SetLabels (labels )
36
41
37
42
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
39
48
})
40
49
}
0 commit comments