k8s.io/apiserver@v0.31.1/pkg/admission/plugin/webhook/mutating/plugin.go (about) 1 /* 2 Copyright 2017 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 mutating 18 19 import ( 20 "context" 21 "io" 22 23 "k8s.io/apiserver/pkg/admission" 24 "k8s.io/apiserver/pkg/admission/configuration" 25 "k8s.io/apiserver/pkg/admission/plugin/webhook/generic" 26 ) 27 28 const ( 29 // PluginName indicates the name of admission plug-in 30 PluginName = "MutatingAdmissionWebhook" 31 ) 32 33 // Register registers a plugin 34 func Register(plugins *admission.Plugins) { 35 plugins.Register(PluginName, func(configFile io.Reader) (admission.Interface, error) { 36 plugin, err := NewMutatingWebhook(configFile) 37 if err != nil { 38 return nil, err 39 } 40 41 return plugin, nil 42 }) 43 } 44 45 // Plugin is an implementation of admission.Interface. 46 type Plugin struct { 47 *generic.Webhook 48 } 49 50 var _ admission.MutationInterface = &Plugin{} 51 52 // NewMutatingWebhook returns a generic admission webhook plugin. 53 func NewMutatingWebhook(configFile io.Reader) (*Plugin, error) { 54 handler := admission.NewHandler(admission.Connect, admission.Create, admission.Delete, admission.Update) 55 p := &Plugin{} 56 var err error 57 p.Webhook, err = generic.NewWebhook(handler, configFile, configuration.NewMutatingWebhookConfigurationManager, newMutatingDispatcher(p)) 58 if err != nil { 59 return nil, err 60 } 61 62 return p, nil 63 } 64 65 // ValidateInitialization implements the InitializationValidator interface. 66 func (a *Plugin) ValidateInitialization() error { 67 if err := a.Webhook.ValidateInitialization(); err != nil { 68 return err 69 } 70 return nil 71 } 72 73 // Admit makes an admission decision based on the request attributes. 74 func (a *Plugin) Admit(ctx context.Context, attr admission.Attributes, o admission.ObjectInterfaces) error { 75 return a.Webhook.Dispatch(ctx, attr, o) 76 }