Skip to content

Commit b5b6754

Browse files
authored
Merge pull request kubernetes#86173 from soltysh/cli_defaults
stop defaulting kubeconfig to http://localhost:8080
2 parents f692f5c + 07dc17f commit b5b6754

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
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"
@@ -55,7 +56,17 @@ const (
5556
flagHTTPCacheDir = "cache-dir"
5657
)
5758

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

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

116132
// ToRawKubeConfigLoader binds config flag values to config overrides

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,14 @@ import (
3535
var (
3636
// ClusterDefaults has the same behavior as the old EnvVar and DefaultCluster fields
3737
// DEPRECATED will be replaced
38-
ClusterDefaults = clientcmdapi.Cluster{Server: getDefaultServer()}
38+
ClusterDefaults = clientcmdapi.Cluster{Server: os.Getenv("KUBERNETES_MASTER")}
3939
// DefaultClientConfig represents the legacy behavior of this package for defaulting
4040
// DEPRECATED will be replace
4141
DefaultClientConfig = DirectClientConfig{*clientcmdapi.NewConfig(), "", &ConfigOverrides{
4242
ClusterDefaults: ClusterDefaults,
4343
}, nil, NewDefaultClientConfigLoadingRules(), promptedCredentials{}}
4444
)
4545

46-
// getDefaultServer returns a default setting for DefaultClientConfig
47-
// DEPRECATED
48-
func getDefaultServer() string {
49-
if server := os.Getenv("KUBERNETES_MASTER"); len(server) > 0 {
50-
return server
51-
}
52-
return "http://localhost:8080"
53-
}
54-
5546
// ClientConfig is used to make it easy to get an api server client
5647
type ClientConfig interface {
5748
// RawConfig returns the merged result of all 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
)

test/cmd/legacy-script.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ runTests() {
325325
exit 1
326326
fi
327327
kube::log::status "Checking kubectl version"
328+
export KUBERNETES_MASTER=http://127.0.0.1:${API_PORT}
328329
kubectl version
329330

330331
# Generate a random namespace name, based on the current time (to make

0 commit comments

Comments
 (0)