Skip to content

Commit 216d2a9

Browse files
author
Mengqi Yu
committed
✨ move webhook generator from CR to CT
1 parent 262e236 commit 216d2a9

File tree

14 files changed

+730
-936
lines changed

14 files changed

+730
-936
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/internal/codegen/parse/apis.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (b *APIs) parseStructs(apigroup *codegen.APIGroup) {
120120
}
121121
done.Insert(next.Type.Name.Name)
122122

123-
// Generate the struct and append to the list
123+
// GenerateObjects the struct and append to the list
124124
result, additionalTypes := parseType(next.Type)
125125

126126
// This is a resource, so generate the client
@@ -153,7 +153,7 @@ func parseType(t *types.Type) (*codegen.Struct, []*types.Type) {
153153
s := &codegen.Struct{
154154
Name: t.Name.Name,
155155
GenClient: false,
156-
GenUnversioned: true, // Generate unversioned structs by default
156+
GenUnversioned: true, // GenerateObjects unversioned structs by default
157157
}
158158

159159
for _, c := range t.CommentLines {

pkg/rbac/manifests.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (o *ManifestOptions) Validate() error {
6767
return nil
6868
}
6969

70-
// Generate generates RBAC manifests by parsing the RBAC annotations in Go source
70+
// GenerateObjects generates RBAC manifests by parsing the RBAC annotations in Go source
7171
// files specified in the input directory.
7272
func Generate(o *ManifestOptions) error {
7373
if err := o.Validate(); err != nil {

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+
// Type is the webhook type, i.e. mutating, validating
35+
Type 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.Type == WebhookTypeMutating {
58+
w.Path = "/mutate-" + w.Rules[0].Resources[0]
59+
} else if w.Type == 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.Type
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.Type != WebhookTypeMutating && w.Type != WebhookTypeValidating {
98+
return fmt.Errorf("unsupported Type: %v, only WebhookTypeMutating and WebhookTypeValidating are supported", w.Type)
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)