sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v2/scaffolds/internal/templates/api/webhook.go (about) 1 /* 2 Copyright 2019 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 api 18 19 import ( 20 "path/filepath" 21 "strings" 22 23 log "github.com/sirupsen/logrus" 24 25 "sigs.k8s.io/kubebuilder/v3/pkg/machinery" 26 ) 27 28 var _ machinery.Template = &Webhook{} 29 30 // Webhook scaffolds the file that defines a webhook for a CRD or a builtin resource 31 type Webhook struct { // nolint:maligned 32 machinery.TemplateMixin 33 machinery.MultiGroupMixin 34 machinery.BoilerplateMixin 35 machinery.ResourceMixin 36 37 // Is the Group domain for the Resource replacing '.' with '-' 38 QualifiedGroupWithDash string 39 } 40 41 // SetTemplateDefaults implements file.Template 42 func (f *Webhook) SetTemplateDefaults() error { 43 if f.Path == "" { 44 if f.MultiGroup { 45 f.Path = filepath.Join("apis", "%[group]", "%[version]", "%[kind]_webhook.go") 46 } else { 47 f.Path = filepath.Join("api", "%[version]", "%[kind]_webhook.go") 48 } 49 } 50 f.Path = f.Resource.Replacer().Replace(f.Path) 51 log.Println(f.Path) 52 53 webhookTemplate := webhookTemplate 54 if f.Resource.HasDefaultingWebhook() { 55 webhookTemplate = webhookTemplate + defaultingWebhookTemplate 56 } 57 if f.Resource.HasValidationWebhook() { 58 webhookTemplate = webhookTemplate + validatingWebhookTemplate 59 } 60 f.TemplateBody = webhookTemplate 61 62 f.IfExistsAction = machinery.Error 63 64 f.QualifiedGroupWithDash = strings.Replace(f.Resource.QualifiedGroup(), ".", "-", -1) 65 66 return nil 67 } 68 69 const ( 70 webhookTemplate = `{{ .Boilerplate }} 71 72 package {{ .Resource.Version }} 73 74 import ( 75 ctrl "sigs.k8s.io/controller-runtime" 76 logf "sigs.k8s.io/controller-runtime/pkg/log" 77 {{- if .Resource.HasValidationWebhook }} 78 "k8s.io/apimachinery/pkg/runtime" 79 {{- end }} 80 {{- if or .Resource.HasValidationWebhook .Resource.HasDefaultingWebhook }} 81 "sigs.k8s.io/controller-runtime/pkg/webhook" 82 {{- end }} 83 ) 84 85 // log is for logging in this package. 86 var {{ lower .Resource.Kind }}log = logf.Log.WithName("{{ lower .Resource.Kind }}-resource") 87 88 func (r *{{ .Resource.Kind }}) SetupWebhookWithManager(mgr ctrl.Manager) error { 89 return ctrl.NewWebhookManagedBy(mgr). 90 For(r). 91 Complete() 92 } 93 94 // EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! 95 ` 96 97 //nolint:lll 98 defaultingWebhookTemplate = ` 99 //+kubebuilder:webhook:path=/mutate-{{ .QualifiedGroupWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=true,failurePolicy=fail,groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=m{{ lower .Resource.Kind }}.kb.io 100 101 var _ webhook.Defaulter = &{{ .Resource.Kind }}{} 102 103 // Default implements webhook.Defaulter so a webhook will be registered for the type 104 func (r *{{ .Resource.Kind }}) Default() { 105 {{ lower .Resource.Kind }}log.Info("default", "name", r.Name) 106 107 // TODO(user): fill in your defaulting logic. 108 } 109 ` 110 //nolint:lll 111 validatingWebhookTemplate = ` 112 // TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. 113 //+kubebuilder:webhook:verbs=create;update,path=/validate-{{ .QualifiedGroupWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=false,failurePolicy=fail,groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},versions={{ .Resource.Version }},name=v{{ lower .Resource.Kind }}.kb.io 114 115 var _ webhook.Validator = &{{ .Resource.Kind }}{} 116 117 // ValidateCreate implements webhook.Validator so a webhook will be registered for the type 118 func (r *{{ .Resource.Kind }}) ValidateCreate() error { 119 {{ lower .Resource.Kind }}log.Info("validate create", "name", r.Name) 120 121 // TODO(user): fill in your validation logic upon object creation. 122 return nil 123 } 124 125 // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type 126 func (r *{{ .Resource.Kind }}) ValidateUpdate(old runtime.Object) error { 127 {{ lower .Resource.Kind }}log.Info("validate update", "name", r.Name) 128 129 // TODO(user): fill in your validation logic upon object update. 130 return nil 131 } 132 133 // ValidateDelete implements webhook.Validator so a webhook will be registered for the type 134 func (r *{{ .Resource.Kind }}) ValidateDelete() error { 135 {{ lower .Resource.Kind }}log.Info("validate delete", "name", r.Name) 136 137 // TODO(user): fill in your validation logic upon object deletion. 138 return nil 139 } 140 ` 141 )