@@ -28,6 +28,18 @@ import (
28
28
"k8s.io/apimachinery/pkg/runtime"
29
29
)
30
30
31
+ // ObjectHandler defines functions for handling objects on webhooks.
32
+ type ObjectHandler interface {
33
+ Handle (ctx context.Context , raw []byte , obj runtime.Object ) Response
34
+ }
35
+
36
+ // WithObjectHandler creates a new Webhook for an ObjectHandler interface.
37
+ func WithObjectHandler (scheme * runtime.Scheme , obj runtime.Object , handler ObjectHandler ) * Webhook {
38
+ return & Webhook {
39
+ Handler : & handlerForType {object : obj , handler : handler , decoder : NewDecoder (scheme )},
40
+ }
41
+ }
42
+
31
43
// CustomDefaulter defines functions for setting defaults on resources.
32
44
type CustomDefaulter interface {
33
45
Default (ctx context.Context , obj runtime.Object ) error
@@ -36,22 +48,22 @@ type CustomDefaulter interface {
36
48
// WithCustomDefaulter creates a new Webhook for a CustomDefaulter interface.
37
49
func WithCustomDefaulter (scheme * runtime.Scheme , obj runtime.Object , defaulter CustomDefaulter ) * Webhook {
38
50
return & Webhook {
39
- Handler : & defaulterForType {object : obj , defaulter : defaulter , decoder : NewDecoder (scheme )},
51
+ Handler : & handlerForType {object : obj , handler : & handlerForDefaulter { defaulter : defaulter } , decoder : NewDecoder (scheme )},
40
52
}
41
53
}
42
54
43
- type defaulterForType struct {
44
- defaulter CustomDefaulter
45
- object runtime.Object
46
- decoder Decoder
55
+ type handlerForType struct {
56
+ handler ObjectHandler
57
+ object runtime.Object
58
+ decoder Decoder
47
59
}
48
60
49
61
// Handle handles admission requests.
50
- func (h * defaulterForType ) Handle (ctx context.Context , req Request ) Response {
62
+ func (h * handlerForType ) Handle (ctx context.Context , req Request ) Response {
51
63
if h .decoder == nil {
52
64
panic ("decoder should never be nil" )
53
65
}
54
- if h .defaulter == nil {
66
+ if h .handler == nil {
55
67
panic ("defaulter should never be nil" )
56
68
}
57
69
if h .object == nil {
@@ -76,7 +88,14 @@ func (h *defaulterForType) Handle(ctx context.Context, req Request) Response {
76
88
return Errored (http .StatusBadRequest , err )
77
89
}
78
90
79
- // Default the object
91
+ return h .handler .Handle (ctx , req .Object .Raw , obj )
92
+ }
93
+
94
+ type handlerForDefaulter struct {
95
+ defaulter CustomDefaulter
96
+ }
97
+
98
+ func (h * handlerForDefaulter ) Handle (ctx context.Context , raw []byte , obj runtime.Object ) Response {
80
99
if err := h .defaulter .Default (ctx , obj ); err != nil {
81
100
var apiStatus apierrors.APIStatus
82
101
if errors .As (err , & apiStatus ) {
@@ -90,5 +109,5 @@ func (h *defaulterForType) Handle(ctx context.Context, req Request) Response {
90
109
if err != nil {
91
110
return Errored (http .StatusInternalServerError , err )
92
111
}
93
- return PatchResponseFromRaw (req . Object . Raw , marshalled )
112
+ return PatchResponseFromRaw (raw , marshalled )
94
113
}
0 commit comments