Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor map field handling for clientcmd.LoadFromFile #17279

Merged
merged 1 commit into from
Nov 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/client/unversioned/clientcmd/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ func LoadFromFile(filename string) (*clientcmdapi.Config, error) {
config.Contexts[key] = obj
}

if config.AuthInfos == nil {
config.AuthInfos = map[string]*clientcmdapi.AuthInfo{}
}
if config.Clusters == nil {
config.Clusters = map[string]*clientcmdapi.Cluster{}
}
if config.Contexts == nil {
config.Contexts = map[string]*clientcmdapi.Context{}
}

return config, nil
}

Expand Down
26 changes: 26 additions & 0 deletions pkg/client/unversioned/clientcmd/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,32 @@ func TestConflictingCurrentContext(t *testing.T) {
}
}

func TestLoadingEmptyMaps(t *testing.T) {
configFile, _ := ioutil.TempFile("", "")
defer os.Remove(configFile.Name())

mockConfig := clientcmdapi.Config{
CurrentContext: "any-context-value",
}

WriteToFile(mockConfig, configFile.Name())

config, err := LoadFromFile(configFile.Name())
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

if config.Clusters == nil {
t.Error("expected config.Clusters to be non-nil")
}
if config.AuthInfos == nil {
t.Error("expected config.AuthInfos to be non-nil")
}
if config.Contexts == nil {
t.Error("expected config.Contexts to be non-nil")
}
}

func TestResolveRelativePaths(t *testing.T) {
pathResolutionConfig1 := clientcmdapi.Config{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
Expand Down
4 changes: 0 additions & 4 deletions pkg/kubectl/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,6 @@ func getConfigFromFileOrDie(filename string) *clientcmdapi.Config {
return clientcmdapi.NewConfig()
}

if config.Clusters == nil {
config.Clusters = map[string]*clientcmdapi.Cluster{}
}

return config
}

Expand Down