...

Source file src/k8s.io/cli-runtime/pkg/genericclioptions/client_config.go

Documentation: k8s.io/cli-runtime/pkg/genericclioptions

     1  /*
     2  Copyright 2020 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 genericclioptions
    18  
    19  import (
    20  	restclient "k8s.io/client-go/rest"
    21  	"k8s.io/client-go/tools/clientcmd"
    22  	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
    23  )
    24  
    25  var (
    26  	// ErrEmptyConfig is the error message to be displayed if the configuration info is missing or incomplete
    27  	ErrEmptyConfig = clientcmd.NewEmptyConfigError(`Missing or incomplete configuration info.  Please point to an existing, complete config file:
    28  
    29  
    30    1. Via the command-line flag --kubeconfig
    31    2. Via the KUBECONFIG environment variable
    32    3. In your home directory as ~/.kube/config
    33  
    34  To view or setup config directly use the 'config' command.`)
    35  )
    36  
    37  var _ = clientcmd.ClientConfig(&clientConfig{})
    38  
    39  type clientConfig struct {
    40  	defaultClientConfig clientcmd.ClientConfig
    41  }
    42  
    43  func (c *clientConfig) RawConfig() (clientcmdapi.Config, error) {
    44  	config, err := c.defaultClientConfig.RawConfig()
    45  	// replace client-go's ErrEmptyConfig error with our custom, more verbose version
    46  	if clientcmd.IsEmptyConfig(err) {
    47  		return config, ErrEmptyConfig
    48  	}
    49  	return config, err
    50  }
    51  
    52  func (c *clientConfig) ClientConfig() (*restclient.Config, error) {
    53  	config, err := c.defaultClientConfig.ClientConfig()
    54  	// replace client-go's ErrEmptyConfig error with our custom, more verbose version
    55  	if clientcmd.IsEmptyConfig(err) {
    56  		return config, ErrEmptyConfig
    57  	}
    58  	return config, err
    59  }
    60  
    61  func (c *clientConfig) Namespace() (string, bool, error) {
    62  	namespace, ok, err := c.defaultClientConfig.Namespace()
    63  	// replace client-go's ErrEmptyConfig error with our custom, more verbose version
    64  	if clientcmd.IsEmptyConfig(err) {
    65  		return namespace, ok, ErrEmptyConfig
    66  	}
    67  	return namespace, ok, err
    68  }
    69  
    70  func (c *clientConfig) ConfigAccess() clientcmd.ConfigAccess {
    71  	return c.defaultClientConfig.ConfigAccess()
    72  }
    73  

View as plain text