Skip to content

Commit

Permalink
Merge pull request #79 from itmecho/feature/empty-dir-size-limit
Browse files Browse the repository at this point in the history
Add missing empty_dir size_limit
  • Loading branch information
sl1pm4t authored Nov 13, 2018
2 parents 95b6c10 + c69a8b6 commit 3c07717
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
2 changes: 2 additions & 0 deletions kubernetes/resource_kubernetes_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ func TestAccKubernetesPod_with_empty_dir_volume(t *testing.T) {
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.volume_mount.0.mount_path", "/cache"),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.volume_mount.0.name", "cache-volume"),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.volume.0.empty_dir.0.medium", "Memory"),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.volume.0.empty_dir.0.size_limit", "1Gi"),
),
},
},
Expand Down Expand Up @@ -1044,6 +1045,7 @@ resource "kubernetes_pod" "test" {
volume {
name = "cache-volume"
empty_dir = {
size_limit = "1Gi"
medium = "Memory"
}
}
Expand Down
2 changes: 2 additions & 0 deletions kubernetes/resource_kubernetes_replication_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ func TestAccKubernetesReplicationController_with_empty_dir_volume(t *testing.T)
resource.TestCheckResourceAttr("kubernetes_replication_controller.test", "spec.0.template.0.container.0.volume_mount.0.mount_path", "/cache"),
resource.TestCheckResourceAttr("kubernetes_replication_controller.test", "spec.0.template.0.container.0.volume_mount.0.name", "cache-volume"),
resource.TestCheckResourceAttr("kubernetes_replication_controller.test", "spec.0.template.0.volume.0.empty_dir.0.medium", "Memory"),
resource.TestCheckResourceAttr("kubernetes_replication_controller.test", "spec.0.template.0.volume.0.empty_dir.0.size_limit", "1Gi"),
),
},
},
Expand Down Expand Up @@ -842,6 +843,7 @@ resource "kubernetes_replication_controller" "test" {
name = "cache-volume"
empty_dir = {
medium = "Memory"
size_limit = "1Gi"
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions kubernetes/schema_pod_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,12 @@ func volumeSchema() *schema.Resource {
Default: "",
ValidateFunc: validateAttributeValueIsIn([]string{"", "Memory"}),
},
"size_limit": {
Type: schema.TypeString,
Description: `Total amount of local storage required for this EmptyDir volume. The size limit is also applicable for memory medium. The maximum usage on memory medium EmptyDir would be the minimum value between the SizeLimit specified here and the sum of memory limits of all containers in a pod. The default is nil which means that the limit is undefined. More info: http://kubernetes.io/docs/user-guide/volumes#emptydir`,
Optional: true,
Default: "",
},
},
},
}
Expand Down
21 changes: 16 additions & 5 deletions kubernetes/structures_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/hashicorp/terraform/helper/schema"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)

// Flatteners
Expand Down Expand Up @@ -360,6 +361,7 @@ func flattenConfigMapVolumeSource(in *v1.ConfigMapVolumeSource) []interface{} {
func flattenEmptyDirVolumeSource(in *v1.EmptyDirVolumeSource) []interface{} {
att := make(map[string]interface{})
att["medium"] = in.Medium
att["size_limit"] = in.SizeLimit.String()
return []interface{}{att}
}

Expand Down Expand Up @@ -725,15 +727,20 @@ func expandGitRepoVolumeSource(l []interface{}) *v1.GitRepoVolumeSource {
return obj
}

func expandEmptyDirVolumeSource(l []interface{}) *v1.EmptyDirVolumeSource {
func expandEmptyDirVolumeSource(l []interface{}) (*v1.EmptyDirVolumeSource, error) {
if len(l) == 0 || l[0] == nil {
return &v1.EmptyDirVolumeSource{}
return &v1.EmptyDirVolumeSource{}, nil
}
in := l[0].(map[string]interface{})
v, err := resource.ParseQuantity(in["size_limit"].(string))
if err != nil {
return &v1.EmptyDirVolumeSource{}, err
}
obj := &v1.EmptyDirVolumeSource{
Medium: v1.StorageMedium(in["medium"].(string)),
Medium: v1.StorageMedium(in["medium"].(string)),
SizeLimit: &v,
}
return obj
return obj, nil
}

func expandPersistentVolumeClaimVolumeSource(l []interface{}) *v1.PersistentVolumeClaimVolumeSource {
Expand Down Expand Up @@ -786,7 +793,11 @@ func expandVolumes(volumes []interface{}) ([]v1.Volume, error) {
}

if value, ok := m["empty_dir"].([]interface{}); ok && len(value) > 0 {
vl[i].EmptyDir = expandEmptyDirVolumeSource(value)
var err error
vl[i].EmptyDir, err = expandEmptyDirVolumeSource(value)
if err != nil {
return vl, err
}
}
if value, ok := m["downward_api"].([]interface{}); ok && len(value) > 0 {
var err error
Expand Down

0 comments on commit 3c07717

Please sign in to comment.