k8s.io/apiserver@v0.31.1/pkg/admission/plugin/webhook/validating/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 validating
    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 = "ValidatingAdmissionWebhook"
    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 := NewValidatingAdmissionWebhook(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.ValidationInterface = &Plugin{}
    51  
    52  // NewValidatingAdmissionWebhook returns a generic admission webhook plugin.
    53  func NewValidatingAdmissionWebhook(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.NewValidatingWebhookConfigurationManager, newValidatingDispatcher(p))
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	return p, nil
    62  }
    63  
    64  // Validate makes an admission decision based on the request attributes.
    65  func (a *Plugin) Validate(ctx context.Context, attr admission.Attributes, o admission.ObjectInterfaces) error {
    66  	return a.Webhook.Dispatch(ctx, attr, o)
    67  }