Skip to content

Commit 23c8932

Browse files
author
Mengqi Yu
committed
✨ move webhook generator from CR to CT
1 parent f3ba955 commit 23c8932

15 files changed

+994
-949
lines changed

cmd/controller-gen/main.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ Usage:
158158
log.Fatal(err)
159159
}
160160
fmt.Printf("RBAC manifests generated under '%s' \n", rbacOptions.OutputDir)
161+
162+
o := &webhook.Options{
163+
WriterOptions: webhook.WriterOptions{
164+
InputDir: filepath.Join(projectDir, "pkg"),
165+
OutputDir: filepath.Join(projectDir, "config", "webhook"),
166+
PatchOutputDir: filepath.Join(projectDir, "config", "default"),
167+
},
168+
}
169+
o.SetDefaults()
170+
if err := webhook.Generate(o); err != nil {
171+
log.Fatal(err)
172+
}
173+
fmt.Printf("webhook manifests generated under '%s' directory\n", o.OutputDir)
161174
},
162175
}
163176
f := cmd.Flags()
@@ -167,7 +180,7 @@ Usage:
167180
}
168181

169182
func newWebhookCmd() *cobra.Command {
170-
o := &webhook.ManifestOptions{}
183+
o := &webhook.Options{}
171184
o.SetDefaults()
172185

173186
cmd := &cobra.Command{

pkg/webhook/admission.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package webhook
18+
19+
import (
20+
"errors"
21+
"fmt"
22+
"regexp"
23+
"strings"
24+
25+
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
)
28+
29+
// admissionWebhook contains bits needed for generating a admissionWebhook Configuration
30+
type admissionWebhook struct {
31+
// name is the name of the webhook
32+
name string
33+
// typ is the webhook type, i.e. mutating, validating
34+
typ webhookType
35+
// path is the path this webhook will serve.
36+
path string
37+
// rules maps to the rules field in admissionregistrationv1beta1.admissionWebhook
38+
rules []admissionregistrationv1beta1.RuleWithOperations
39+
// failurePolicy maps to the failurePolicy field in admissionregistrationv1beta1.admissionWebhook
40+
// This optional. If not set, will be defaulted to Ignore (fail-open) by the server.
41+
// More details: https://github.com/kubernetes/api/blob/f5c295feaba2cbc946f0bbb8b535fc5f6a0345ee/admissionregistration/v1beta1/types.go#L144-L147
42+
failurePolicy *admissionregistrationv1beta1.FailurePolicyType
43+
// namespaceSelector maps to the namespaceSelector field in admissionregistrationv1beta1.admissionWebhook
44+
// This optional.
45+
namespaceSelector *metav1.LabelSelector
46+
}
47+
48+
func (w *admissionWebhook) setDefaults() {
49+
if len(w.path) == 0 {
50+
if len(w.rules) == 0 || len(w.rules[0].Resources) == 0 {
51+
// can't do defaulting, skip it.
52+
return
53+
}
54+
if w.typ == mutatingWebhook {
55+
w.path = "/mutate-" + w.rules[0].Resources[0]
56+
} else if w.typ == validatingWebhook {
57+
w.path = "/validate-" + w.rules[0].Resources[0]
58+
}
59+
}
60+
if len(w.name) == 0 {
61+
reg := regexp.MustCompile("[^a-zA-Z0-9]+")
62+
processedPath := strings.ToLower(reg.ReplaceAllString(w.path, ""))
63+
w.name = processedPath + ".example.com"
64+
}
65+
}
66+
67+
var _ webhook = &admissionWebhook{}
68+
69+
// GetType returns the type of the webhook.
70+
func (w *admissionWebhook) GetType() webhookType {
71+
return w.typ
72+
}
73+
74+
// Validate validates if the webhook is valid.
75+
func (w *admissionWebhook) Validate() error {
76+
if len(w.rules) == 0 {
77+
return errors.New("field rules should not be empty")
78+
}
79+
if len(w.name) == 0 {
80+
return errors.New("field name should not be empty")
81+
}
82+
if w.typ != mutatingWebhook && w.typ != validatingWebhook {
83+
return fmt.Errorf("unsupported Type: %v, only mutatingWebhook and validatingWebhook are supported", w.typ)
84+
}
85+
if len(w.path) == 0 {
86+
return errors.New("field path should not be empty")
87+
}
88+
return nil
89+
}

0 commit comments

Comments
 (0)