k8s.io/apiserver@v0.31.1/pkg/admission/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  	"k8s.io/apimachinery/pkg/api/meta"
    21  	"k8s.io/apiserver/pkg/admission"
    22  	"k8s.io/apiserver/pkg/authorization/authorizer"
    23  	"k8s.io/client-go/dynamic"
    24  	"k8s.io/client-go/informers"
    25  	"k8s.io/client-go/kubernetes"
    26  	"k8s.io/component-base/featuregate"
    27  )
    28  
    29  type pluginInitializer struct {
    30  	externalClient    kubernetes.Interface
    31  	dynamicClient     dynamic.Interface
    32  	externalInformers informers.SharedInformerFactory
    33  	authorizer        authorizer.Authorizer
    34  	featureGates      featuregate.FeatureGate
    35  	stopCh            <-chan struct{}
    36  	restMapper        meta.RESTMapper
    37  }
    38  
    39  // New creates an instance of admission plugins initializer.
    40  // This constructor is public with a long param list so that callers immediately know that new information can be expected
    41  // during compilation when they update a level.
    42  func New(
    43  	extClientset kubernetes.Interface,
    44  	dynamicClient dynamic.Interface,
    45  	extInformers informers.SharedInformerFactory,
    46  	authz authorizer.Authorizer,
    47  	featureGates featuregate.FeatureGate,
    48  	stopCh <-chan struct{},
    49  	restMapper meta.RESTMapper,
    50  ) pluginInitializer {
    51  	return pluginInitializer{
    52  		externalClient:    extClientset,
    53  		dynamicClient:     dynamicClient,
    54  		externalInformers: extInformers,
    55  		authorizer:        authz,
    56  		featureGates:      featureGates,
    57  		stopCh:            stopCh,
    58  		restMapper:        restMapper,
    59  	}
    60  }
    61  
    62  // Initialize checks the initialization interfaces implemented by a plugin
    63  // and provide the appropriate initialization data
    64  func (i pluginInitializer) Initialize(plugin admission.Interface) {
    65  	// First tell the plugin about drained notification, so it can pass it to further initializations.
    66  	if wants, ok := plugin.(WantsDrainedNotification); ok {
    67  		wants.SetDrainedNotification(i.stopCh)
    68  	}
    69  
    70  	// Second tell the plugin about enabled features, so it can decide whether to start informers or not
    71  	if wants, ok := plugin.(WantsFeatures); ok {
    72  		wants.InspectFeatureGates(i.featureGates)
    73  	}
    74  
    75  	if wants, ok := plugin.(WantsExternalKubeClientSet); ok {
    76  		wants.SetExternalKubeClientSet(i.externalClient)
    77  	}
    78  
    79  	if wants, ok := plugin.(WantsDynamicClient); ok {
    80  		wants.SetDynamicClient(i.dynamicClient)
    81  	}
    82  
    83  	if wants, ok := plugin.(WantsExternalKubeInformerFactory); ok {
    84  		wants.SetExternalKubeInformerFactory(i.externalInformers)
    85  	}
    86  
    87  	if wants, ok := plugin.(WantsAuthorizer); ok {
    88  		wants.SetAuthorizer(i.authorizer)
    89  	}
    90  	if wants, ok := plugin.(WantsRESTMapper); ok {
    91  		wants.SetRESTMapper(i.restMapper)
    92  	}
    93  }
    94  
    95  var _ admission.PluginInitializer = pluginInitializer{}