Skip to content

Commit

Permalink
Merge pull request #411 from vincepri/generate-marker-help
Browse files Browse the repository at this point in the history
🏃 Make webhook marker MatchPolicy optional
  • Loading branch information
k8s-ci-robot committed Mar 11, 2020
2 parents 65bd5c2 + 932c305 commit 1c6d384
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
23 changes: 16 additions & 7 deletions pkg/webhook/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Config struct {
// MatchPolicy defines how the "rules" list is used to match incoming requests.
// Allowed values are "Exact" (match only if it exactly matches the specified rule)
// or "Equivalent" (match a request if it modifies a resource listed in rules, even via another API group or version).
MatchPolicy string
MatchPolicy string `marker:",optional"`

// Groups specifies the API groups that this webhook receives requests for.
Groups []string
Expand Down Expand Up @@ -111,11 +111,16 @@ func (c Config) ToMutatingWebhook() (admissionreg.MutatingWebhook, error) {
return admissionreg.MutatingWebhook{}, fmt.Errorf("%s is a validating webhook", c.Name)
}

matchPolicy, err := c.matchPolicy()
if err != nil {
return admissionreg.MutatingWebhook{}, err
}

return admissionreg.MutatingWebhook{
Name: c.Name,
Rules: c.rules(),
FailurePolicy: c.failurePolicy(),
MatchPolicy: c.matchPolicy(),
MatchPolicy: matchPolicy,
ClientConfig: c.clientConfig(),
}, nil
}
Expand All @@ -126,11 +131,16 @@ func (c Config) ToValidatingWebhook() (admissionreg.ValidatingWebhook, error) {
return admissionreg.ValidatingWebhook{}, fmt.Errorf("%s is a mutating webhook", c.Name)
}

matchPolicy, err := c.matchPolicy()
if err != nil {
return admissionreg.ValidatingWebhook{}, err
}

return admissionreg.ValidatingWebhook{
Name: c.Name,
Rules: c.rules(),
FailurePolicy: c.failurePolicy(),
MatchPolicy: c.matchPolicy(),
MatchPolicy: matchPolicy,
ClientConfig: c.clientConfig(),
}, nil
}
Expand Down Expand Up @@ -177,18 +187,17 @@ func (c Config) failurePolicy() *admissionreg.FailurePolicyType {
}

// matchPolicy converts the string value to the proper value for the API.
// Unrecognized values are passed through.
func (c Config) matchPolicy() *admissionreg.MatchPolicyType {
func (c Config) matchPolicy() (*admissionreg.MatchPolicyType, error) {
var matchPolicy admissionreg.MatchPolicyType
switch strings.ToLower(c.MatchPolicy) {
case strings.ToLower(string(admissionreg.Exact)):
matchPolicy = admissionreg.Exact
case strings.ToLower(string(admissionreg.Equivalent)):
matchPolicy = admissionreg.Equivalent
default:
matchPolicy = admissionreg.MatchPolicyType(c.MatchPolicy)
return nil, fmt.Errorf("unknown value %q for matchPolicy", c.MatchPolicy)
}
return &matchPolicy
return &matchPolicy, nil
}

// clientConfig returns the client config for a webhook.
Expand Down
8 changes: 6 additions & 2 deletions pkg/webhook/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1c6d384

Please sign in to comment.