Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 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 validating |
| 18 | |
| 19 | import ( |
| 20 | "io" |
| 21 | |
| 22 | "k8s.io/apiserver/pkg/admission" |
| 23 | "k8s.io/apiserver/pkg/admission/configuration" |
| 24 | "k8s.io/apiserver/pkg/admission/plugin/webhook/generic" |
| 25 | ) |
| 26 | |
| 27 | const ( |
| 28 | // Name of admission plug-in |
| 29 | PluginName = "ValidatingAdmissionWebhook" |
| 30 | ) |
| 31 | |
| 32 | // Register registers a plugin |
| 33 | func Register(plugins *admission.Plugins) { |
| 34 | plugins.Register(PluginName, func(configFile io.Reader) (admission.Interface, error) { |
| 35 | plugin, err := NewValidatingAdmissionWebhook(configFile) |
| 36 | if err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | |
| 40 | return plugin, nil |
| 41 | }) |
| 42 | } |
| 43 | |
| 44 | // Plugin is an implementation of admission.Interface. |
| 45 | type Plugin struct { |
| 46 | *generic.Webhook |
| 47 | } |
| 48 | |
| 49 | var _ admission.ValidationInterface = &Plugin{} |
| 50 | |
| 51 | // NewValidatingAdmissionWebhook returns a generic admission webhook plugin. |
| 52 | func NewValidatingAdmissionWebhook(configFile io.Reader) (*Plugin, error) { |
| 53 | handler := admission.NewHandler(admission.Connect, admission.Create, admission.Delete, admission.Update) |
| 54 | webhook, err := generic.NewWebhook(handler, configFile, configuration.NewValidatingWebhookConfigurationManager, newValidatingDispatcher) |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | return &Plugin{webhook}, nil |
| 59 | } |
| 60 | |
| 61 | // Validate makes an admission decision based on the request attributes. |
| 62 | func (a *Plugin) Validate(attr admission.Attributes) error { |
| 63 | return a.Webhook.Dispatch(attr) |
| 64 | } |