Skip to content

Commit 07dc17f

Browse files
committed
Provide more verbose empty config error based on the context
1 parent b19ad9e commit 07dc17f

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

staging/src/k8s.io/cli-runtime/pkg/genericclioptions/config_flags.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package genericclioptions
1818

1919
import (
20+
"errors"
2021
"os"
2122
"path/filepath"
2223
"regexp"
@@ -54,7 +55,17 @@ const (
5455
flagHTTPCacheDir = "cache-dir"
5556
)
5657

57-
var defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
58+
var (
59+
defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
60+
61+
ErrEmptyConfig = errors.New(`Missing or incomplete configuration info. Please point to an existing, complete config file:
62+
63+
1. Via the command-line flag --kubeconfig
64+
2. Via the KUBECONFIG environment variable
65+
3. In your home directory as ~/.kube/config
66+
67+
To view or setup config directly use the 'config' command.`)
68+
)
5869

5970
// RESTClientGetter is an interface that the ConfigFlags describe to provide an easier way to mock for commands
6071
// and eliminate the direct coupling to a struct type. Users may wish to duplicate this type in their own packages
@@ -108,7 +119,12 @@ type ConfigFlags struct {
108119
// to a .kubeconfig file, loading rules, and config flag overrides.
109120
// Expects the AddFlags method to have been called.
110121
func (f *ConfigFlags) ToRESTConfig() (*rest.Config, error) {
111-
return f.ToRawKubeConfigLoader().ClientConfig()
122+
config, err := f.ToRawKubeConfigLoader().ClientConfig()
123+
// replace client-go's ErrEmptyConfig error with our custom, more verbose version
124+
if clientcmd.IsEmptyConfig(err) {
125+
return nil, ErrEmptyConfig
126+
}
127+
return config, err
112128
}
113129

114130
// ToRawKubeConfigLoader binds config flag values to config overrides

staging/src/k8s.io/client-go/tools/clientcmd/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030

3131
var (
3232
ErrNoContext = errors.New("no context chosen")
33-
ErrEmptyConfig = errors.New("no configuration has been provided")
33+
ErrEmptyConfig = errors.New("no configuration has been provided, try setting KUBERNETES_MASTER environment variable")
3434
// message is for consistency with old behavior
3535
ErrEmptyCluster = errors.New("cluster has no server defined")
3636
)

0 commit comments

Comments
 (0)