Skip to content

Commit 1884e47

Browse files
authored
Fix crash when SizeLimit is empty on EmptyDirVolumeSource (#983)
1 parent 4f7a5ac commit 1884e47

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

kubernetes/structures_pod.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,10 @@ func flattenConfigMapVolumeSource(in *v1.ConfigMapVolumeSource) []interface{} {
412412

413413
func flattenEmptyDirVolumeSource(in *v1.EmptyDirVolumeSource) []interface{} {
414414
att := make(map[string]interface{})
415-
att["medium"] = in.Medium
416-
att["size_limit"] = in.SizeLimit.String()
415+
att["medium"] = string(in.Medium)
416+
if in.SizeLimit != nil {
417+
att["size_limit"] = in.SizeLimit.String()
418+
}
417419
return []interface{}{att}
418420
}
419421

kubernetes/structures_pod_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/google/go-cmp/cmp"
88
v1 "k8s.io/api/core/v1"
9+
"k8s.io/apimachinery/pkg/api/resource"
910
)
1011

1112
func TestFlattenTolerations(t *testing.T) {
@@ -298,6 +299,54 @@ func TestExpandSecretVolumeSource(t *testing.T) {
298299
}
299300
}
300301

302+
func TestFlattenEmptyDirVolumeSource(t *testing.T) {
303+
size, _ := resource.ParseQuantity("64Mi")
304+
305+
cases := []struct {
306+
Input *v1.EmptyDirVolumeSource
307+
ExpectedOutput []interface{}
308+
}{
309+
{
310+
&v1.EmptyDirVolumeSource{
311+
Medium: v1.StorageMediumMemory,
312+
},
313+
[]interface{}{
314+
map[string]interface{}{
315+
"medium": "Memory",
316+
},
317+
},
318+
},
319+
{
320+
&v1.EmptyDirVolumeSource{
321+
Medium: v1.StorageMediumMemory,
322+
SizeLimit: &size,
323+
},
324+
[]interface{}{
325+
map[string]interface{}{
326+
"medium": "Memory",
327+
"size_limit": "64Mi",
328+
},
329+
},
330+
},
331+
{
332+
&v1.EmptyDirVolumeSource{},
333+
[]interface{}{
334+
map[string]interface{}{
335+
"medium": "",
336+
},
337+
},
338+
},
339+
}
340+
341+
for _, tc := range cases {
342+
output := flattenEmptyDirVolumeSource(tc.Input)
343+
if !reflect.DeepEqual(output, tc.ExpectedOutput) {
344+
t.Fatalf("Unexpected output from flattener.\nExpected: %#v\nGiven: %#v",
345+
tc.ExpectedOutput, output)
346+
}
347+
}
348+
}
349+
301350
func TestFlattenConfigMapVolumeSource(t *testing.T) {
302351
cases := []struct {
303352
Input *v1.ConfigMapVolumeSource

0 commit comments

Comments
 (0)