-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
webhook.go
156 lines (119 loc) · 5.43 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
)
var _ machinery.Template = &Webhook{}
// Webhook scaffolds the file that defines a webhook for a CRD or a builtin resource
type Webhook struct { // nolint:maligned
machinery.TemplateMixin
machinery.MultiGroupMixin
machinery.BoilerplateMixin
machinery.ResourceMixin
// Is the Group domain for the Resource replacing '.' with '-'
QualifiedGroupWithDash string
// Define value for AdmissionReviewVersions marker
AdmissionReviewVersions string
Force bool
}
// SetTemplateDefaults implements file.Template
func (f *Webhook) SetTemplateDefaults() error {
if f.Path == "" {
if f.MultiGroup && f.Resource.Group != "" {
f.Path = filepath.Join("api", "%[group]", "%[version]", "%[kind]_webhook.go")
} else {
f.Path = filepath.Join("api", "%[version]", "%[kind]_webhook.go")
}
}
f.Path = f.Resource.Replacer().Replace(f.Path)
log.Println(f.Path)
webhookTemplate := webhookTemplate
if f.Resource.HasDefaultingWebhook() {
webhookTemplate = webhookTemplate + defaultingWebhookTemplate
}
if f.Resource.HasValidationWebhook() {
webhookTemplate = webhookTemplate + validatingWebhookTemplate
}
f.TemplateBody = webhookTemplate
if f.Force {
f.IfExistsAction = machinery.OverwriteFile
} else {
f.IfExistsAction = machinery.Error
}
f.AdmissionReviewVersions = "v1"
f.QualifiedGroupWithDash = strings.Replace(f.Resource.QualifiedGroup(), ".", "-", -1)
return nil
}
const (
webhookTemplate = `{{ .Boilerplate }}
package {{ .Resource.Version }}
import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
{{- if .Resource.HasValidationWebhook }}
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
{{- end }}
{{- if or .Resource.HasValidationWebhook .Resource.HasDefaultingWebhook }}
"sigs.k8s.io/controller-runtime/pkg/webhook"
{{- end }}
)
// log is for logging in this package.
var {{ lower .Resource.Kind }}log = logf.Log.WithName("{{ lower .Resource.Kind }}-resource")
// SetupWebhookWithManager will setup the manager to manage the webhooks
func (r *{{ .Resource.Kind }}) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
`
//nolint:lll
defaultingWebhookTemplate = `
//+kubebuilder:webhook:{{ if ne .Resource.Webhooks.WebhookVersion "v1" }}webhookVersions={{"{"}}{{ .Resource.Webhooks.WebhookVersion }}{{"}"}},{{ end }}path=/mutate-{{ .QualifiedGroupWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=true,failurePolicy=fail,sideEffects=None,groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=m{{ lower .Resource.Kind }}.kb.io,admissionReviewVersions={{ .AdmissionReviewVersions }}
var _ webhook.Defaulter = &{{ .Resource.Kind }}{}
// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *{{ .Resource.Kind }}) Default() {
{{ lower .Resource.Kind }}log.Info("default", "name", r.Name)
// TODO(user): fill in your defaulting logic.
}
`
//nolint:lll
validatingWebhookTemplate = `
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
//+kubebuilder:webhook:{{ if ne .Resource.Webhooks.WebhookVersion "v1" }}webhookVersions={{"{"}}{{ .Resource.Webhooks.WebhookVersion }}{{"}"}},{{ end }}path=/validate-{{ .QualifiedGroupWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=false,failurePolicy=fail,sideEffects=None,groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=v{{ lower .Resource.Kind }}.kb.io,admissionReviewVersions={{ .AdmissionReviewVersions }}
var _ webhook.Validator = &{{ .Resource.Kind }}{}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *{{ .Resource.Kind }}) ValidateCreate() (admission.Warnings, error) {
{{ lower .Resource.Kind }}log.Info("validate create", "name", r.Name)
// TODO(user): fill in your validation logic upon object creation.
return nil, nil
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *{{ .Resource.Kind }}) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
{{ lower .Resource.Kind }}log.Info("validate update", "name", r.Name)
// TODO(user): fill in your validation logic upon object update.
return nil, nil
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *{{ .Resource.Kind }}) ValidateDelete() (admission.Warnings, error) {
{{ lower .Resource.Kind }}log.Info("validate delete", "name", r.Name)
// TODO(user): fill in your validation logic upon object deletion.
return nil,nil
}
`
)