Skip to content

Commit 54bbec2

Browse files
committed
Factor code common to both types of webhooks into methods
1 parent 0469bf7 commit 54bbec2

File tree

1 file changed

+35
-58
lines changed

1 file changed

+35
-58
lines changed

pkg/webhook/parser.go

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -101,51 +101,11 @@ func (c Config) ToMutatingWebhook() (admissionreg.MutatingWebhook, error) {
101101
return admissionreg.MutatingWebhook{}, fmt.Errorf("%s is a validating webhook", c.Name)
102102
}
103103

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
134104
return admissionreg.MutatingWebhook{
135105
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(),
149109
}, nil
150110
}
151111

@@ -155,6 +115,17 @@ func (c Config) ToValidatingWebhook() (admissionreg.ValidatingWebhook, error) {
155115
return admissionreg.ValidatingWebhook{}, fmt.Errorf("%s is a mutating webhook", c.Name)
156116
}
157117

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 {
158129
whConfig := admissionreg.RuleWithOperations{
159130
Rule: admissionreg.Rule{
160131
APIGroups: c.Groups,
@@ -175,6 +146,12 @@ func (c Config) ToValidatingWebhook() (admissionreg.ValidatingWebhook, error) {
175146
}
176147
}
177148

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 {
178155
var failurePolicy admissionreg.FailurePolicyType
179156
switch strings.ToLower(c.FailurePolicy) {
180157
case strings.ToLower(string(admissionreg.Ignore)):
@@ -184,23 +161,23 @@ func (c Config) ToValidatingWebhook() (admissionreg.ValidatingWebhook, error) {
184161
default:
185162
failurePolicy = admissionreg.FailurePolicyType(c.FailurePolicy)
186163
}
164+
return &failurePolicy
165+
}
166+
167+
// clientConfig returns the client config for a webhook.
168+
func (c Config) clientConfig() admissionreg.WebhookClientConfig {
187169
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,
202175
},
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+
}
204181
}
205182

206183
// +controllertools:marker:generateHelp

0 commit comments

Comments
 (0)