Skip to content

Commit 8b7cf0f

Browse files
authored
Merge pull request kubernetes#86395 from yutedz/nil-map-conversion
Allocate map when out parameter points to nil map
2 parents be20826 + 53080bd commit 8b7cf0f

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

staging/src/k8s.io/client-go/tools/clientcmd/api/v1/conversion.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ func Convert_Slice_v1_NamedCluster_To_Map_string_To_Pointer_api_Cluster(in *[]Na
3131
if err := Convert_v1_Cluster_To_api_Cluster(&curr.Cluster, newCluster, s); err != nil {
3232
return err
3333
}
34+
if *out == nil {
35+
*out = make(map[string]*api.Cluster)
36+
}
3437
if (*out)[curr.Name] == nil {
3538
(*out)[curr.Name] = newCluster
3639
} else {
@@ -65,6 +68,9 @@ func Convert_Slice_v1_NamedAuthInfo_To_Map_string_To_Pointer_api_AuthInfo(in *[]
6568
if err := Convert_v1_AuthInfo_To_api_AuthInfo(&curr.AuthInfo, newAuthInfo, s); err != nil {
6669
return err
6770
}
71+
if *out == nil {
72+
*out = make(map[string]*api.AuthInfo)
73+
}
6874
if (*out)[curr.Name] == nil {
6975
(*out)[curr.Name] = newAuthInfo
7076
} else {
@@ -99,6 +105,9 @@ func Convert_Slice_v1_NamedContext_To_Map_string_To_Pointer_api_Context(in *[]Na
99105
if err := Convert_v1_Context_To_api_Context(&curr.Context, newContext, s); err != nil {
100106
return err
101107
}
108+
if *out == nil {
109+
*out = make(map[string]*api.Context)
110+
}
102111
if (*out)[curr.Name] == nil {
103112
(*out)[curr.Name] = newContext
104113
} else {
@@ -133,6 +142,9 @@ func Convert_Slice_v1_NamedExtension_To_Map_string_To_runtime_Object(in *[]Named
133142
if err := runtime.Convert_runtime_RawExtension_To_runtime_Object(&curr.Extension, &newExtension, s); err != nil {
134143
return err
135144
}
145+
if *out == nil {
146+
*out = make(map[string]runtime.Object)
147+
}
136148
if (*out)[curr.Name] == nil {
137149
(*out)[curr.Name] = newExtension
138150
} else {

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ var (
8080
}
8181
)
8282

83+
func TestNilOutMap(t *testing.T) {
84+
var fakeKubeconfigData = `apiVersion: v1
85+
kind: Config
86+
clusters:
87+
- cluster:
88+
certificate-authority-data: UEhPTlkK
89+
server: https://1.1.1.1
90+
name: production
91+
contexts:
92+
- context:
93+
cluster: production
94+
user: production
95+
name: production
96+
current-context: production
97+
users:
98+
- name: production
99+
user:
100+
auth-provider:
101+
name: gcp`
102+
103+
_, _, err := clientcmdlatest.Codec.Decode([]byte(fakeKubeconfigData), nil, nil)
104+
if err != nil {
105+
t.Fatalf("unexpected error: %v", err)
106+
}
107+
}
108+
83109
func TestNonExistentCommandLineFile(t *testing.T) {
84110
loadingRules := ClientConfigLoadingRules{
85111
ExplicitPath: "bogus_file",

0 commit comments

Comments
 (0)