k8s.io/apiserver@v0.31.1/pkg/admission/plugin/webhook/initializer/initializer.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 initializer
    18  
    19  import (
    20  	"net/url"
    21  
    22  	"k8s.io/apiserver/pkg/admission"
    23  	"k8s.io/apiserver/pkg/util/webhook"
    24  )
    25  
    26  // WantsServiceResolver defines a function that accepts a ServiceResolver for
    27  // admission plugins that need to make calls to services.
    28  type WantsServiceResolver interface {
    29  	SetServiceResolver(webhook.ServiceResolver)
    30  }
    31  
    32  // ServiceResolver knows how to convert a service reference into an actual
    33  // location.
    34  type ServiceResolver interface {
    35  	ResolveEndpoint(namespace, name string, port int32) (*url.URL, error)
    36  }
    37  
    38  // WantsAuthenticationInfoResolverWrapper defines a function that wraps the standard AuthenticationInfoResolver
    39  // to allow the apiserver to control what is returned as auth info
    40  type WantsAuthenticationInfoResolverWrapper interface {
    41  	SetAuthenticationInfoResolverWrapper(wrapper webhook.AuthenticationInfoResolverWrapper)
    42  	admission.InitializationValidator
    43  }
    44  
    45  // PluginInitializer is used for initialization of the webhook admission plugin.
    46  type PluginInitializer struct {
    47  	serviceResolver                   webhook.ServiceResolver
    48  	authenticationInfoResolverWrapper webhook.AuthenticationInfoResolverWrapper
    49  }
    50  
    51  var _ admission.PluginInitializer = &PluginInitializer{}
    52  
    53  // NewPluginInitializer constructs new instance of PluginInitializer
    54  func NewPluginInitializer(
    55  	authenticationInfoResolverWrapper webhook.AuthenticationInfoResolverWrapper,
    56  	serviceResolver webhook.ServiceResolver,
    57  ) *PluginInitializer {
    58  	return &PluginInitializer{
    59  		authenticationInfoResolverWrapper: authenticationInfoResolverWrapper,
    60  		serviceResolver:                   serviceResolver,
    61  	}
    62  }
    63  
    64  // Initialize checks the initialization interfaces implemented by each plugin
    65  // and provide the appropriate initialization data
    66  func (i *PluginInitializer) Initialize(plugin admission.Interface) {
    67  	if wants, ok := plugin.(WantsServiceResolver); ok {
    68  		wants.SetServiceResolver(i.serviceResolver)
    69  	}
    70  
    71  	if wants, ok := plugin.(WantsAuthenticationInfoResolverWrapper); ok {
    72  		if i.authenticationInfoResolverWrapper != nil {
    73  			wants.SetAuthenticationInfoResolverWrapper(i.authenticationInfoResolverWrapper)
    74  		}
    75  	}
    76  }