mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-09 12:07:47 +00:00
Use correct home directory on Windows
This commit is contained in:
parent
be803eaf64
commit
97d106e5ab
@ -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 (
|
|||||||
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 (
|
|||||||
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 {
|
|||||||
// 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 {
|
|||||||
|
|
||||||
} 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(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
40
pkg/util/homedir/homedir.go
Normal file
40
pkg/util/homedir/homedir.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
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")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user