@@ -18,7 +18,6 @@ package webhook
18
18
19
19
import (
20
20
"context"
21
- "fmt"
22
21
23
22
admissionregistration "k8s.io/api/admissionregistration/v1beta1"
24
23
corev1 "k8s.io/api/core/v1"
@@ -27,29 +26,34 @@ import (
27
26
"sigs.k8s.io/controller-runtime/pkg/client"
28
27
)
29
28
30
- type mutateFn func (current , desired runtime.Object ) error
29
+ type mutateFn func (current , desired * runtime.Object ) error
31
30
32
- var serviceFn = func (current , desired runtime.Object ) error {
33
- typedC := current .(* corev1.Service )
34
- typedD := desired .(* corev1.Service )
31
+ var serviceFn = func (current , desired * runtime.Object ) error {
32
+ typedC := ( * current ) .(* corev1.Service )
33
+ typedD := ( * desired ) .(* corev1.Service )
35
34
typedC .Spec .Selector = typedD .Spec .Selector
36
35
return nil
37
36
}
38
37
39
- var mutatingWebhookConfigFn = func (current , desired runtime.Object ) error {
40
- typedC := current .(* admissionregistration.MutatingWebhookConfiguration )
41
- typedD := desired .(* admissionregistration.MutatingWebhookConfiguration )
38
+ var mutatingWebhookConfigFn = func (current , desired * runtime.Object ) error {
39
+ typedC := ( * current ) .(* admissionregistration.MutatingWebhookConfiguration )
40
+ typedD := ( * desired ) .(* admissionregistration.MutatingWebhookConfiguration )
42
41
typedC .Webhooks = typedD .Webhooks
43
42
return nil
44
43
}
45
44
46
- var validatingWebhookConfigFn = func (current , desired runtime.Object ) error {
47
- typedC := current .(* admissionregistration.ValidatingWebhookConfiguration )
48
- typedD := desired .(* admissionregistration.ValidatingWebhookConfiguration )
45
+ var validatingWebhookConfigFn = func (current , desired * runtime.Object ) error {
46
+ typedC := ( * current ) .(* admissionregistration.ValidatingWebhookConfiguration )
47
+ typedD := ( * desired ) .(* admissionregistration.ValidatingWebhookConfiguration )
49
48
typedC .Webhooks = typedD .Webhooks
50
49
return nil
51
50
}
52
51
52
+ var genericFn = func (current , desired * runtime.Object ) error {
53
+ * current = * desired
54
+ return nil
55
+ }
56
+
53
57
// createOrReplaceHelper creates the object if it doesn't exist;
54
58
// otherwise, it will replace it.
55
59
// When replacing, fn should know how to preserve existing fields in the object GET from the APIServer.
@@ -70,7 +74,7 @@ func createOrReplaceHelper(c client.Client, obj runtime.Object, fn mutateFn) err
70
74
if err != nil {
71
75
return err
72
76
}
73
- err = fn (existing , obj )
77
+ err = fn (& existing , & obj )
74
78
if err != nil {
75
79
return err
76
80
}
@@ -83,6 +87,7 @@ func createOrReplaceHelper(c client.Client, obj runtime.Object, fn mutateFn) err
83
87
// otherwise, it will replace it.
84
88
// When replacing, it knows how to preserve existing fields in the object GET from the APIServer.
85
89
// It currently only support MutatingWebhookConfiguration, ValidatingWebhookConfiguration and Service.
90
+ // For other kinds, it uses genericFn to replace the whole object.
86
91
func createOrReplace (c client.Client , obj runtime.Object ) error {
87
92
if obj == nil {
88
93
return nil
@@ -95,7 +100,7 @@ func createOrReplace(c client.Client, obj runtime.Object) error {
95
100
case * corev1.Service :
96
101
return createOrReplaceHelper (c , obj , serviceFn )
97
102
default :
98
- return fmt . Errorf ( "unsupported GroupVersionKind: %#v" , obj . GetObjectKind (). GroupVersionKind () )
103
+ return createOrReplaceHelper ( c , obj , genericFn )
99
104
}
100
105
}
101
106
0 commit comments