Skip to content

Commit a9cba03

Browse files
committed
Check for required name parameter in dynamic client
The Create, Delete, Get, Patch, Update and UpdateStatus methods in the dynamic client all expect the name parameter to be non-empty, but did not validate this requirement, which could lead to a panic. Add explicit checks to these methods.
1 parent ad47274 commit a9cba03

File tree

1 file changed

+23
-2
lines changed
  • staging/src/k8s.io/client-go/dynamic

1 file changed

+23
-2
lines changed

staging/src/k8s.io/client-go/dynamic/simple.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package dynamic
1818

1919
import (
20+
"fmt"
2021
"io"
2122

2223
"k8s.io/apimachinery/pkg/api/meta"
@@ -102,6 +103,9 @@ func (c *dynamicResourceClient) Create(obj *unstructured.Unstructured, opts meta
102103
return nil, err
103104
}
104105
name = accessor.GetName()
106+
if len(name) == 0 {
107+
return nil, fmt.Errorf("name is required")
108+
}
105109
}
106110

107111
result := c.client.client.
@@ -130,14 +134,18 @@ func (c *dynamicResourceClient) Update(obj *unstructured.Unstructured, opts meta
130134
if err != nil {
131135
return nil, err
132136
}
137+
name := accessor.GetName()
138+
if len(name) == 0 {
139+
return nil, fmt.Errorf("name is required")
140+
}
133141
outBytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
134142
if err != nil {
135143
return nil, err
136144
}
137145

138146
result := c.client.client.
139147
Put().
140-
AbsPath(append(c.makeURLSegments(accessor.GetName()), subresources...)...).
148+
AbsPath(append(c.makeURLSegments(name), subresources...)...).
141149
Body(outBytes).
142150
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
143151
Do()
@@ -161,6 +169,10 @@ func (c *dynamicResourceClient) UpdateStatus(obj *unstructured.Unstructured, opt
161169
if err != nil {
162170
return nil, err
163171
}
172+
name := accessor.GetName()
173+
if len(name) == 0 {
174+
return nil, fmt.Errorf("name is required")
175+
}
164176

165177
outBytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
166178
if err != nil {
@@ -169,7 +181,7 @@ func (c *dynamicResourceClient) UpdateStatus(obj *unstructured.Unstructured, opt
169181

170182
result := c.client.client.
171183
Put().
172-
AbsPath(append(c.makeURLSegments(accessor.GetName()), "status")...).
184+
AbsPath(append(c.makeURLSegments(name), "status")...).
173185
Body(outBytes).
174186
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
175187
Do()
@@ -189,6 +201,9 @@ func (c *dynamicResourceClient) UpdateStatus(obj *unstructured.Unstructured, opt
189201
}
190202

191203
func (c *dynamicResourceClient) Delete(name string, opts *metav1.DeleteOptions, subresources ...string) error {
204+
if len(name) == 0 {
205+
return fmt.Errorf("name is required")
206+
}
192207
if opts == nil {
193208
opts = &metav1.DeleteOptions{}
194209
}
@@ -224,6 +239,9 @@ func (c *dynamicResourceClient) DeleteCollection(opts *metav1.DeleteOptions, lis
224239
}
225240

226241
func (c *dynamicResourceClient) Get(name string, opts metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error) {
242+
if len(name) == 0 {
243+
return nil, fmt.Errorf("name is required")
244+
}
227245
result := c.client.client.Get().AbsPath(append(c.makeURLSegments(name), subresources...)...).SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).Do()
228246
if err := result.Error(); err != nil {
229247
return nil, err
@@ -292,6 +310,9 @@ func (c *dynamicResourceClient) Watch(opts metav1.ListOptions) (watch.Interface,
292310
}
293311

294312
func (c *dynamicResourceClient) Patch(name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (*unstructured.Unstructured, error) {
313+
if len(name) == 0 {
314+
return nil, fmt.Errorf("name is required")
315+
}
295316
result := c.client.client.
296317
Patch(pt).
297318
AbsPath(append(c.makeURLSegments(name), subresources...)...).

0 commit comments

Comments
 (0)