Commit 5c2f7585 authored by k8s-merge-robot's avatar k8s-merge-robot

Merge pull request #17590 from fabianofranz/cli_homedir_on_windows

Automatic merge from submit-queue Use correct home directory on Windows As of now the `kubectl` config mechanism that saves/loads `~/.kube/config` is broken on Windows, saving the config file in the *current* directory instead of the user's *home* dir. This happens because most Windows don't respect the `HOME` environment variable. This PR changes the config file loading mechanism to use the recommended way to detect the user home on Windows (`HOMEDRIVE`+`HOMEPATH` or `USERPROFILE`), and adds a migration for users that might be currently relying on existing config files.
parents 9b0ea8ff 97d106e5
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
goruntime "runtime"
"strings" "strings"
"github.com/golang/glog" "github.com/golang/glog"
...@@ -33,6 +34,7 @@ import ( ...@@ -33,6 +34,7 @@ import (
clientcmdlatest "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/latest" clientcmdlatest "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/latest"
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime"
utilerrors "k8s.io/kubernetes/pkg/util/errors" utilerrors "k8s.io/kubernetes/pkg/util/errors"
"k8s.io/kubernetes/pkg/util/homedir"
) )
const ( const (
...@@ -43,9 +45,23 @@ const ( ...@@ -43,9 +45,23 @@ const (
RecommendedSchemaName = "schema" RecommendedSchemaName = "schema"
) )
var OldRecommendedHomeFile = path.Join(os.Getenv("HOME"), "/.kube/.kubeconfig") var RecommendedHomeFile = path.Join(homedir.HomeDir(), RecommendedHomeDir, RecommendedFileName)
var RecommendedHomeFile = path.Join(os.Getenv("HOME"), RecommendedHomeDir, RecommendedFileName) var RecommendedSchemaFile = path.Join(homedir.HomeDir(), RecommendedHomeDir, RecommendedSchemaName)
var RecommendedSchemaFile = path.Join(os.Getenv("HOME"), RecommendedHomeDir, RecommendedSchemaName)
// currentMigrationRules returns a map that holds the history of recommended home directories used in previous versions.
// Any future changes to RecommendedHomeFile and related are expected to add a migration rule here, in order to make
// sure existing config files are migrated to their new locations properly.
func currentMigrationRules() map[string]string {
oldRecommendedHomeFile := path.Join(os.Getenv("HOME"), "/.kube/.kubeconfig")
oldRecommendedWindowsHomeFile := path.Join(os.Getenv("HOME"), RecommendedHomeDir, RecommendedFileName)
migrationRules := map[string]string{}
migrationRules[RecommendedHomeFile] = oldRecommendedHomeFile
if goruntime.GOOS == "windows" {
migrationRules[RecommendedHomeFile] = oldRecommendedWindowsHomeFile
}
return migrationRules
}
// ClientConfigLoadingRules is an ExplicitPath and string slice of specific locations that are used for merging together a Config // ClientConfigLoadingRules is an ExplicitPath and string slice of specific locations that are used for merging together a Config
// Callers can put the chain together however they want, but we'd recommend: // Callers can put the chain together however they want, but we'd recommend:
...@@ -68,7 +84,6 @@ type ClientConfigLoadingRules struct { ...@@ -68,7 +84,6 @@ type ClientConfigLoadingRules struct {
// use this constructor // use this constructor
func NewDefaultClientConfigLoadingRules() *ClientConfigLoadingRules { func NewDefaultClientConfigLoadingRules() *ClientConfigLoadingRules {
chain := []string{} chain := []string{}
migrationRules := map[string]string{}
envVarFiles := os.Getenv(RecommendedConfigPathEnvVar) envVarFiles := os.Getenv(RecommendedConfigPathEnvVar)
if len(envVarFiles) != 0 { if len(envVarFiles) != 0 {
...@@ -76,13 +91,11 @@ func NewDefaultClientConfigLoadingRules() *ClientConfigLoadingRules { ...@@ -76,13 +91,11 @@ func NewDefaultClientConfigLoadingRules() *ClientConfigLoadingRules {
} else { } else {
chain = append(chain, RecommendedHomeFile) chain = append(chain, RecommendedHomeFile)
migrationRules[RecommendedHomeFile] = OldRecommendedHomeFile
} }
return &ClientConfigLoadingRules{ return &ClientConfigLoadingRules{
Precedence: chain, Precedence: chain,
MigrationRules: migrationRules, MigrationRules: currentMigrationRules(),
} }
} }
......
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package homedir
import (
"os"
"runtime"
)
// HomeDir returns the home directory for the current user
func HomeDir() string {
if runtime.GOOS == "windows" {
if homeDrive, homePath := os.Getenv("HOMEDRIVE"), os.Getenv("HOMEPATH"); len(homeDrive) > 0 && len(homePath) > 0 {
homeDir := homeDrive + homePath
if _, err := os.Stat(homeDir); err == nil {
return homeDir
}
}
if userProfile := os.Getenv("USERPROFILE"); len(userProfile) > 0 {
if _, err := os.Stat(userProfile); err == nil {
return userProfile
}
}
}
return os.Getenv("HOME")
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment