@@ -101,51 +101,11 @@ func (c Config) ToMutatingWebhook() (admissionreg.MutatingWebhook, error) {
101
101
return admissionreg.MutatingWebhook {}, fmt .Errorf ("%s is a validating webhook" , c .Name )
102
102
}
103
103
104
- whConfig := admissionreg.RuleWithOperations {
105
- Rule : admissionreg.Rule {
106
- APIGroups : c .Groups ,
107
- APIVersions : c .Versions ,
108
- Resources : c .Resources ,
109
- },
110
- Operations : make ([]admissionreg.OperationType , len (c .Verbs )),
111
- }
112
-
113
- for i , verbRaw := range c .Verbs {
114
- whConfig .Operations [i ] = verbToAPIVariant (verbRaw )
115
- }
116
-
117
- // fix the group names, since letting people type "core" is nice
118
- for i , group := range whConfig .APIGroups {
119
- if group == "core" {
120
- whConfig .APIGroups [i ] = ""
121
- }
122
- }
123
-
124
- var failurePolicy admissionreg.FailurePolicyType
125
- switch strings .ToLower (c .FailurePolicy ) {
126
- case strings .ToLower (string (admissionreg .Ignore )):
127
- failurePolicy = admissionreg .Ignore
128
- case strings .ToLower (string (admissionreg .Fail )):
129
- failurePolicy = admissionreg .Fail
130
- default :
131
- failurePolicy = admissionreg .FailurePolicyType (c .FailurePolicy )
132
- }
133
- path := c .Path
134
104
return admissionreg.MutatingWebhook {
135
105
Name : c .Name ,
136
- Rules : []admissionreg.RuleWithOperations {whConfig },
137
- FailurePolicy : & failurePolicy ,
138
- ClientConfig : admissionreg.WebhookClientConfig {
139
- Service : & admissionreg.ServiceReference {
140
- Name : "webhook-service" ,
141
- Namespace : "system" ,
142
- Path : & path ,
143
- },
144
- // OpenAPI marks the field as required before 1.13 because of a bug that got fixed in
145
- // https://github.com/kubernetes/api/commit/e7d9121e9ffd63cea0288b36a82bcc87b073bd1b
146
- // Put "\n" as an placeholder as a workaround til 1.13+ is almost everywhere.
147
- CABundle : []byte ("\n " ),
148
- },
106
+ Rules : c .rules (),
107
+ FailurePolicy : c .failurePolicy (),
108
+ ClientConfig : c .clientConfig (),
149
109
}, nil
150
110
}
151
111
@@ -155,6 +115,17 @@ func (c Config) ToValidatingWebhook() (admissionreg.ValidatingWebhook, error) {
155
115
return admissionreg.ValidatingWebhook {}, fmt .Errorf ("%s is a mutating webhook" , c .Name )
156
116
}
157
117
118
+ return admissionreg.ValidatingWebhook {
119
+ Name : c .Name ,
120
+ Rules : c .rules (),
121
+ FailurePolicy : c .failurePolicy (),
122
+ ClientConfig : c .clientConfig (),
123
+ }, nil
124
+ }
125
+
126
+ // rules returns the configuration of what operations on what
127
+ // resources/subresources a webhook should care about.
128
+ func (c Config ) rules () []admissionreg.RuleWithOperations {
158
129
whConfig := admissionreg.RuleWithOperations {
159
130
Rule : admissionreg.Rule {
160
131
APIGroups : c .Groups ,
@@ -175,6 +146,12 @@ func (c Config) ToValidatingWebhook() (admissionreg.ValidatingWebhook, error) {
175
146
}
176
147
}
177
148
149
+ return []admissionreg.RuleWithOperations {whConfig }
150
+ }
151
+
152
+ // failurePolicy converts the string value to the proper value for the API.
153
+ // Unrecognized values are passed through.
154
+ func (c Config ) failurePolicy () * admissionreg.FailurePolicyType {
178
155
var failurePolicy admissionreg.FailurePolicyType
179
156
switch strings .ToLower (c .FailurePolicy ) {
180
157
case strings .ToLower (string (admissionreg .Ignore )):
@@ -184,23 +161,23 @@ func (c Config) ToValidatingWebhook() (admissionreg.ValidatingWebhook, error) {
184
161
default :
185
162
failurePolicy = admissionreg .FailurePolicyType (c .FailurePolicy )
186
163
}
164
+ return & failurePolicy
165
+ }
166
+
167
+ // clientConfig returns the client config for a webhook.
168
+ func (c Config ) clientConfig () admissionreg.WebhookClientConfig {
187
169
path := c .Path
188
- return admissionreg.ValidatingWebhook {
189
- Name : c .Name ,
190
- Rules : []admissionreg.RuleWithOperations {whConfig },
191
- FailurePolicy : & failurePolicy ,
192
- ClientConfig : admissionreg.WebhookClientConfig {
193
- Service : & admissionreg.ServiceReference {
194
- Name : "webhook-service" ,
195
- Namespace : "system" ,
196
- Path : & path ,
197
- },
198
- // OpenAPI marks the field as required before 1.13 because of a bug that got fixed in
199
- // https://github.com/kubernetes/api/commit/e7d9121e9ffd63cea0288b36a82bcc87b073bd1b
200
- // Put "\n" as an placeholder as a workaround til 1.13+ is almost everywhere.
201
- CABundle : []byte ("\n " ),
170
+ return admissionreg.WebhookClientConfig {
171
+ Service : & admissionreg.ServiceReference {
172
+ Name : "webhook-service" ,
173
+ Namespace : "system" ,
174
+ Path : & path ,
202
175
},
203
- }, nil
176
+ // OpenAPI marks the field as required before 1.13 because of a bug that got fixed in
177
+ // https://github.com/kubernetes/api/commit/e7d9121e9ffd63cea0288b36a82bcc87b073bd1b
178
+ // Put "\n" as an placeholder as a workaround til 1.13+ is almost everywhere.
179
+ CABundle : []byte ("\n " ),
180
+ }
204
181
}
205
182
206
183
// +controllertools:marker:generateHelp
0 commit comments