@@ -17,14 +17,6 @@ limitations under the License.
17
17
package builder
18
18
19
19
import (
20
- "errors"
21
- "fmt"
22
-
23
- admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
24
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
- "k8s.io/apimachinery/pkg/runtime"
26
- "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
27
- "sigs.k8s.io/controller-runtime/pkg/manager"
28
20
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
29
21
"sigs.k8s.io/controller-runtime/pkg/webhook/types"
30
22
)
@@ -46,26 +38,6 @@ type WebhookBuilder struct {
46
38
// t specifies the type of the webhook.
47
39
// Currently, Mutating and Validating are supported.
48
40
t * types.WebhookType
49
-
50
- // operations define the operations this webhook cares.
51
- // only one of operations and Rules can be set.
52
- operations []admissionregistrationv1beta1.OperationType
53
- // apiType represents the resource that this webhook cares.
54
- // Only one of apiType and Rules can be set.
55
- apiType runtime.Object
56
- // rules contain a list of admissionregistrationv1beta1.RuleWithOperations
57
- // It overrides operations and apiType.
58
- rules []admissionregistrationv1beta1.RuleWithOperations
59
-
60
- // failurePolicy maps to the FailurePolicy in the admissionregistrationv1beta1.Webhook
61
- failurePolicy * admissionregistrationv1beta1.FailurePolicyType
62
-
63
- // namespaceSelector maps to the NamespaceSelector in the admissionregistrationv1beta1.Webhook
64
- namespaceSelector * metav1.LabelSelector
65
-
66
- // manager is the manager for the webhook.
67
- // It is used for provisioning various dependencies for the webhook. e.g. RESTMapper.
68
- manager manager.Manager
69
41
}
70
42
71
43
// NewWebhookBuilder creates an empty WebhookBuilder.
@@ -98,126 +70,32 @@ func (b *WebhookBuilder) Validating() *WebhookBuilder {
98
70
99
71
// Path sets the path for the webhook.
100
72
// Path needs to be unique among different webhooks.
101
- // This is optional . If not set, it will be built from the type and resource name.
73
+ // This is required . If not set, it will be built from the type and resource name.
102
74
// For example, a webhook that mutates pods has a default path of "/mutate-pods"
103
75
// If the defaulting logic can't find a unique path for it, user need to set it manually.
104
76
func (b * WebhookBuilder ) Path (path string ) * WebhookBuilder {
105
77
b .path = path
106
78
return b
107
79
}
108
80
109
- // Operations sets the operations that this webhook cares.
110
- // It will be overridden by Rules if Rules are not empty.
111
- // This is optional
112
- func (b * WebhookBuilder ) Operations (ops ... admissionregistrationv1beta1.OperationType ) * WebhookBuilder {
113
- b .operations = ops
114
- return b
115
- }
116
-
117
- // ForType sets the type of resources that the webhook will operate.
118
- // It will be overridden by Rules if Rules are not empty.
119
- func (b * WebhookBuilder ) ForType (obj runtime.Object ) * WebhookBuilder {
120
- b .apiType = obj
121
- return b
122
- }
123
-
124
- // Rules sets the RuleWithOperations for the webhook.
125
- // It overrides ForType and Operations.
126
- // This is optional and for advanced user.
127
- func (b * WebhookBuilder ) Rules (rules ... admissionregistrationv1beta1.RuleWithOperations ) * WebhookBuilder {
128
- b .rules = rules
129
- return b
130
- }
131
-
132
- // FailurePolicy sets the FailurePolicy of the webhook.
133
- // If not set, it will be defaulted by the server.
134
- // This is optional
135
- func (b * WebhookBuilder ) FailurePolicy (policy admissionregistrationv1beta1.FailurePolicyType ) * WebhookBuilder {
136
- b .failurePolicy = & policy
137
- return b
138
- }
139
-
140
- // NamespaceSelector sets the NamespaceSelector for the webhook.
141
- // This is optional
142
- func (b * WebhookBuilder ) NamespaceSelector (namespaceSelector * metav1.LabelSelector ) * WebhookBuilder {
143
- b .namespaceSelector = namespaceSelector
144
- return b
145
- }
146
-
147
- // WithManager set the manager for the webhook for provisioning various dependencies. e.g. client etc.
148
- func (b * WebhookBuilder ) WithManager (mgr manager.Manager ) * WebhookBuilder {
149
- b .manager = mgr
150
- return b
151
- }
152
-
153
81
// Handlers sets the handlers of the webhook.
154
82
func (b * WebhookBuilder ) Handlers (handlers ... admission.Handler ) * WebhookBuilder {
155
83
b .handlers = handlers
156
84
return b
157
85
}
158
86
159
- func (b * WebhookBuilder ) validate () error {
160
- if b .t == nil {
161
- return errors .New ("webhook type cannot be nil" )
162
- }
163
- if b .rules == nil && b .apiType == nil {
164
- return fmt .Errorf ("ForType should be set" )
165
- }
166
- if b .rules != nil && b .apiType != nil {
167
- return fmt .Errorf ("at most one of ForType and Rules can be set" )
168
- }
169
- return nil
170
- }
171
-
172
87
// Build creates the Webhook based on the options provided.
173
- func (b * WebhookBuilder ) Build () (* admission.Webhook , error ) {
174
- err := b .validate ()
175
- if err != nil {
176
- return nil , err
88
+ func (b * WebhookBuilder ) Build () * admission.Webhook {
89
+ if b .t == nil {
90
+ b .Mutating ()
177
91
}
178
92
179
93
w := & admission.Webhook {
180
- Name : b .name ,
181
- Type : * b .t ,
182
- Path : b .path ,
183
- FailurePolicy : b .failurePolicy ,
184
- NamespaceSelector : b .namespaceSelector ,
185
- Handlers : b .handlers ,
186
- }
187
-
188
- if b .rules != nil {
189
- w .Rules = b .rules
190
- } else {
191
- if b .manager == nil {
192
- return nil , errors .New ("manager should be set using WithManager" )
193
- }
194
- gvk , err := apiutil .GVKForObject (b .apiType , b .manager .GetScheme ())
195
- if err != nil {
196
- return nil , err
197
- }
198
- mapper := b .manager .GetRESTMapper ()
199
- mapping , err := mapper .RESTMapping (gvk .GroupKind (), gvk .Version )
200
- if err != nil {
201
- return nil , err
202
- }
203
-
204
- if b .operations == nil {
205
- b .operations = []admissionregistrationv1beta1.OperationType {
206
- admissionregistrationv1beta1 .Create ,
207
- admissionregistrationv1beta1 .Update ,
208
- }
209
- }
210
- w .Rules = []admissionregistrationv1beta1.RuleWithOperations {
211
- {
212
- Operations : b .operations ,
213
- Rule : admissionregistrationv1beta1.Rule {
214
- APIGroups : []string {gvk .Group },
215
- APIVersions : []string {gvk .Version },
216
- Resources : []string {mapping .Resource .Resource },
217
- },
218
- },
219
- }
94
+ Name : b .name ,
95
+ Type : * b .t ,
96
+ Path : b .path ,
97
+ Handlers : b .handlers ,
220
98
}
221
99
222
- return w , nil
100
+ return w
223
101
}
0 commit comments