Skip to content

Commit

Permalink
Merge pull request #86414 from yutedz/automated-cherry-pick-of-#86395…
Browse files Browse the repository at this point in the history
…-upstream-release-1.17

[v1.17.1] cherry pick of #86395 : Allocate map when out points to nil map
  • Loading branch information
k8s-ci-robot committed Dec 25, 2019
2 parents 47def41 + bc5fbb8 commit 589b7b5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions staging/src/k8s.io/client-go/tools/clientcmd/api/v1/conversion.go
Expand Up @@ -31,6 +31,9 @@ func Convert_Slice_v1_NamedCluster_To_Map_string_To_Pointer_api_Cluster(in *[]Na
if err := Convert_v1_Cluster_To_api_Cluster(&curr.Cluster, newCluster, s); err != nil {
return err
}
if *out == nil {
*out = make(map[string]*api.Cluster)
}
if (*out)[curr.Name] == nil {
(*out)[curr.Name] = newCluster
} else {
Expand Down Expand Up @@ -65,6 +68,9 @@ func Convert_Slice_v1_NamedAuthInfo_To_Map_string_To_Pointer_api_AuthInfo(in *[]
if err := Convert_v1_AuthInfo_To_api_AuthInfo(&curr.AuthInfo, newAuthInfo, s); err != nil {
return err
}
if *out == nil {
*out = make(map[string]*api.AuthInfo)
}
if (*out)[curr.Name] == nil {
(*out)[curr.Name] = newAuthInfo
} else {
Expand Down Expand Up @@ -99,6 +105,9 @@ func Convert_Slice_v1_NamedContext_To_Map_string_To_Pointer_api_Context(in *[]Na
if err := Convert_v1_Context_To_api_Context(&curr.Context, newContext, s); err != nil {
return err
}
if *out == nil {
*out = make(map[string]*api.Context)
}
if (*out)[curr.Name] == nil {
(*out)[curr.Name] = newContext
} else {
Expand Down Expand Up @@ -133,6 +142,9 @@ func Convert_Slice_v1_NamedExtension_To_Map_string_To_runtime_Object(in *[]Named
if err := runtime.Convert_runtime_RawExtension_To_runtime_Object(&curr.Extension, &newExtension, s); err != nil {
return err
}
if *out == nil {
*out = make(map[string]runtime.Object)
}
if (*out)[curr.Name] == nil {
(*out)[curr.Name] = newExtension
} else {
Expand Down
26 changes: 26 additions & 0 deletions staging/src/k8s.io/client-go/tools/clientcmd/loader_test.go
Expand Up @@ -78,6 +78,32 @@ var (
}
)

func TestNilOutMap(t *testing.T) {
var fakeKubeconfigData = `apiVersion: v1
kind: Config
clusters:
- cluster:
certificate-authority-data: UEhPTlkK
server: https://1.1.1.1
name: production
contexts:
- context:
cluster: production
user: production
name: production
current-context: production
users:
- name: production
user:
auth-provider:
name: gcp`

_, _, err := clientcmdlatest.Codec.Decode([]byte(fakeKubeconfigData), nil, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}

func TestNonExistentCommandLineFile(t *testing.T) {
loadingRules := ClientConfigLoadingRules{
ExplicitPath: "bogus_file",
Expand Down

0 comments on commit 589b7b5

Please sign in to comment.