k8s.io/apiserver@v0.31.1/pkg/server/options/coreapi.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 options
    18  
    19  import (
    20  	"fmt"
    21  	"time"
    22  
    23  	"github.com/spf13/pflag"
    24  	"k8s.io/apiserver/pkg/features"
    25  	"k8s.io/apiserver/pkg/server"
    26  	"k8s.io/apiserver/pkg/util/feature"
    27  	clientgoinformers "k8s.io/client-go/informers"
    28  	clientgoclientset "k8s.io/client-go/kubernetes"
    29  	"k8s.io/client-go/rest"
    30  	"k8s.io/client-go/tools/clientcmd"
    31  	tracing "k8s.io/component-base/tracing"
    32  )
    33  
    34  // CoreAPIOptions contains options to configure the connection to a core API Kubernetes apiserver.
    35  type CoreAPIOptions struct {
    36  	// CoreAPIKubeconfigPath is a filename for a kubeconfig file to contact the core API server with.
    37  	// If it is not set, the in cluster config is used.
    38  	CoreAPIKubeconfigPath string
    39  }
    40  
    41  func NewCoreAPIOptions() *CoreAPIOptions {
    42  	return &CoreAPIOptions{}
    43  }
    44  
    45  func (o *CoreAPIOptions) AddFlags(fs *pflag.FlagSet) {
    46  	if o == nil {
    47  		return
    48  	}
    49  
    50  	fs.StringVar(&o.CoreAPIKubeconfigPath, "kubeconfig", o.CoreAPIKubeconfigPath,
    51  		"kubeconfig file pointing at the 'core' kubernetes server.")
    52  }
    53  
    54  func (o *CoreAPIOptions) ApplyTo(config *server.RecommendedConfig) error {
    55  	if o == nil {
    56  		return nil
    57  	}
    58  
    59  	// create shared informer for Kubernetes APIs
    60  	var kubeconfig *rest.Config
    61  	var err error
    62  	if len(o.CoreAPIKubeconfigPath) > 0 {
    63  		loadingRules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: o.CoreAPIKubeconfigPath}
    64  		loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
    65  		kubeconfig, err = loader.ClientConfig()
    66  		if err != nil {
    67  			return fmt.Errorf("failed to load kubeconfig at %q: %v", o.CoreAPIKubeconfigPath, err)
    68  		}
    69  	} else {
    70  		kubeconfig, err = rest.InClusterConfig()
    71  		if err != nil {
    72  			return err
    73  		}
    74  	}
    75  	if feature.DefaultFeatureGate.Enabled(features.APIServerTracing) {
    76  		kubeconfig.Wrap(tracing.WrapperFor(config.TracerProvider))
    77  	}
    78  	clientgoExternalClient, err := clientgoclientset.NewForConfig(kubeconfig)
    79  	if err != nil {
    80  		return fmt.Errorf("failed to create Kubernetes clientset: %v", err)
    81  	}
    82  	config.ClientConfig = kubeconfig
    83  	config.SharedInformerFactory = clientgoinformers.NewSharedInformerFactory(clientgoExternalClient, 10*time.Minute)
    84  
    85  	return nil
    86  }
    87  
    88  func (o *CoreAPIOptions) Validate() []error {
    89  	return nil
    90  }