blob: 8417ccffb1797758330ca7101f54503cc6203f05 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package validating
18
19import (
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
27const (
28 // Name of admission plug-in
29 PluginName = "ValidatingAdmissionWebhook"
30)
31
32// Register registers a plugin
33func 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.
45type Plugin struct {
46 *generic.Webhook
47}
48
49var _ admission.ValidationInterface = &Plugin{}
50
51// NewValidatingAdmissionWebhook returns a generic admission webhook plugin.
52func 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.
62func (a *Plugin) Validate(attr admission.Attributes) error {
63 return a.Webhook.Dispatch(attr)
64}