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