Skip to content

Commit a019e3a

Browse files
author
Mengqi Yu
committed
✨ move webhook generator from CR to CT
1 parent facf507 commit a019e3a

File tree

12 files changed

+768
-948
lines changed

12 files changed

+768
-948
lines changed

cmd/controller-gen/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Usage:
167167
}
168168

169169
func newWebhookCmd() *cobra.Command {
170-
o := &webhook.ManifestOptions{}
170+
o := &webhook.Options{}
171171
o.SetDefaults()
172172

173173
cmd := &cobra.Command{

pkg/webhook/admission.go

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
"sync"
25+
26+
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
)
29+
30+
// admissionWebhook contains bits needed for generating a admissionWebhook Configuration
31+
type admissionWebhook struct {
32+
// name is the name of the webhook
33+
name string
34+
// t is the webhook type, i.e. mutating, validating
35+
t webhookType
36+
// path is the path this webhook will serve.
37+
path string
38+
// rules maps to the rules field in admissionregistrationv1beta1.admissionWebhook
39+
rules []admissionregistrationv1beta1.RuleWithOperations
40+
// failurePolicy maps to the failurePolicy field in admissionregistrationv1beta1.admissionWebhook
41+
// This optional. If not set, will be defaulted to Ignore (fail-open) by the server.
42+
// More details: https://github.com/kubernetes/api/blob/f5c295feaba2cbc946f0bbb8b535fc5f6a0345ee/admissionregistration/v1beta1/types.go#L144-L147
43+
failurePolicy *admissionregistrationv1beta1.FailurePolicyType
44+
// namespaceSelector maps to the namespaceSelector field in admissionregistrationv1beta1.admissionWebhook
45+
// This optional.
46+
namespaceSelector *metav1.LabelSelector
47+
48+
once sync.Once
49+
}
50+
51+
func (w *admissionWebhook) setDefaults() {
52+
if len(w.path) == 0 {
53+
if len(w.rules) == 0 || len(w.rules[0].Resources) == 0 {
54+
// can't do defaulting, skip it.
55+
return
56+
}
57+
if w.t == webhookTypeMutating {
58+
w.path = "/mutate-" + w.rules[0].Resources[0]
59+
} else if w.t == webhookTypeValidating {
60+
w.path = "/validate-" + w.rules[0].Resources[0]
61+
}
62+
}
63+
if len(w.name) == 0 {
64+
reg := regexp.MustCompile("[^a-zA-Z0-9]+")
65+
processedPath := strings.ToLower(reg.ReplaceAllString(w.path, ""))
66+
w.name = processedPath + ".example.com"
67+
}
68+
}
69+
70+
// GetName returns the name of the webhook.
71+
func (w *admissionWebhook) GetName() string {
72+
w.once.Do(w.setDefaults)
73+
return w.name
74+
}
75+
76+
// GetPath returns the path that the webhook registered.
77+
func (w *admissionWebhook) GetPath() string {
78+
w.once.Do(w.setDefaults)
79+
return w.path
80+
}
81+
82+
// GetType returns the type of the webhook.
83+
func (w *admissionWebhook) GetType() webhookType {
84+
w.once.Do(w.setDefaults)
85+
return w.t
86+
}
87+
88+
// Validate validates if the webhook is valid.
89+
func (w *admissionWebhook) Validate() error {
90+
w.once.Do(w.setDefaults)
91+
if len(w.rules) == 0 {
92+
return errors.New("field rules should not be empty")
93+
}
94+
if len(w.name) == 0 {
95+
return errors.New("field name should not be empty")
96+
}
97+
if w.t != webhookTypeMutating && w.t != webhookTypeValidating {
98+
return fmt.Errorf("unsupported Type: %v, only webhookTypeMutating and webhookTypeValidating are supported", w.t)
99+
}
100+
if len(w.path) == 0 {
101+
return errors.New("field path should not be empty")
102+
}
103+
return nil
104+
}

0 commit comments

Comments
 (0)