Skip to content

Commit 1d4053e

Browse files
committed
✨ Add support for MatchPolicy in webhook marker
Signed-off-by: Vince Prignano <[email protected]>
1 parent 2ba5d4b commit 1d4053e

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

pkg/webhook/parser.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ limitations under the License.
1919
//
2020
// The markers take the form:
2121
//
22-
// +kubebuilder:webhook:failurePolicy=<string>,groups=<[]string>,resources=<[]string>,verbs=<[]string>,versions=<[]string>,name=<string>,path=<string>,mutating=<bool>
22+
// +kubebuilder:webhook:failurePolicy=<string>,matchPolicy=<string>,groups=<[]string>,resources=<[]string>,verbs=<[]string>,versions=<[]string>,name=<string>,path=<string>,mutating=<bool>
2323
package webhook
2424

2525
import (
@@ -57,6 +57,10 @@ type Config struct {
5757
// It may be either "ignore" (to skip the webhook and continue on) or "fail" (to reject
5858
// the object in question).
5959
FailurePolicy string
60+
// MatchPolicy defines how the "rules" list is used to match incoming requests.
61+
// Allowed values are "Exact" (match only if it exactly matches the specified rule)
62+
// or "Equivalent" (match a request if it modifies a resource listed in rules, even via another API group or version).
63+
MatchPolicy string
6064

6165
// Groups specifies the API groups that this webhook receives requests for.
6266
Groups []string
@@ -111,6 +115,7 @@ func (c Config) ToMutatingWebhook() (admissionreg.MutatingWebhook, error) {
111115
Name: c.Name,
112116
Rules: c.rules(),
113117
FailurePolicy: c.failurePolicy(),
118+
MatchPolicy: c.matchPolicy(),
114119
ClientConfig: c.clientConfig(),
115120
}, nil
116121
}
@@ -125,6 +130,7 @@ func (c Config) ToValidatingWebhook() (admissionreg.ValidatingWebhook, error) {
125130
Name: c.Name,
126131
Rules: c.rules(),
127132
FailurePolicy: c.failurePolicy(),
133+
MatchPolicy: c.matchPolicy(),
128134
ClientConfig: c.clientConfig(),
129135
}, nil
130136
}
@@ -170,6 +176,21 @@ func (c Config) failurePolicy() *admissionreg.FailurePolicyType {
170176
return &failurePolicy
171177
}
172178

179+
// matchPolicy converts the string value to the proper value for the API.
180+
// Unrecognized values are passed through.
181+
func (c Config) matchPolicy() *admissionreg.MatchPolicyType {
182+
var matchPolicy admissionreg.MatchPolicyType
183+
switch strings.ToLower(c.MatchPolicy) {
184+
case strings.ToLower(string(admissionreg.Exact)):
185+
matchPolicy = admissionreg.Exact
186+
case strings.ToLower(string(admissionreg.Equivalent)):
187+
matchPolicy = admissionreg.Equivalent
188+
default:
189+
matchPolicy = admissionreg.MatchPolicyType(c.MatchPolicy)
190+
}
191+
return &matchPolicy
192+
}
193+
173194
// clientConfig returns the client config for a webhook.
174195
func (c Config) clientConfig() admissionreg.WebhookClientConfig {
175196
path := c.Path

0 commit comments

Comments
 (0)