Skip to content

Commit 61520b4

Browse files
gram-signalBBBmau
andauthored
Allow HugePages in emptyDir.medium. (#2395)
* Allow HugePages in emptyDir.medium. * Add acceptance test for HugePages-1Gi. * Reformat. * add changelog-entry --------- Co-authored-by: BBBmau <[email protected]>
1 parent 2566978 commit 61520b4

File tree

3 files changed

+100
-9
lines changed

3 files changed

+100
-9
lines changed

.changelog/2395.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
`kubernetes/kubernetes_deployment_v1`: Add support for `HugePages` in `emptyDir.medium`
3+
```

kubernetes/resource_kubernetes_deployment_v1_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,33 @@ func TestAccKubernetesDeploymentV1_with_empty_dir_volume(t *testing.T) {
820820
})
821821
}
822822

823+
func TestAccKubernetesDeploymentV1_with_empty_dir_huge_page(t *testing.T) {
824+
var conf appsv1.Deployment
825+
826+
imageName := busyboxImage
827+
deploymentName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
828+
resourceName := "kubernetes_deployment_v1.test"
829+
830+
resource.ParallelTest(t, resource.TestCase{
831+
PreCheck: func() { testAccPreCheck(t) },
832+
ProviderFactories: testAccProviderFactories,
833+
CheckDestroy: testAccCheckKubernetesDeploymentV1Destroy,
834+
Steps: []resource.TestStep{
835+
{
836+
Config: testAccKubernetesDeploymentV1ConfigWithEmptyDirHugePage(deploymentName, imageName),
837+
Check: resource.ComposeAggregateTestCheckFunc(
838+
testAccCheckKubernetesDeploymentV1Exists(resourceName, &conf),
839+
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.image", imageName),
840+
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.volume_mount.#", "1"),
841+
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.volume_mount.0.mount_path", "/cache"),
842+
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.volume_mount.0.name", "cache-volume"),
843+
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.volume.0.empty_dir.0.medium", "HugePages-1Gi"),
844+
),
845+
},
846+
},
847+
})
848+
}
849+
823850
func TestAccKubernetesDeploymentV1Update_basic(t *testing.T) {
824851
var conf1, conf2 appsv1.Deployment
825852
resourceName := "kubernetes_deployment_v1.test"
@@ -2608,6 +2635,59 @@ func testAccKubernetesDeploymentV1ConfigWithEmptyDirVolumes(deploymentName, imag
26082635
`, deploymentName, imageName)
26092636
}
26102637

2638+
func testAccKubernetesDeploymentV1ConfigWithEmptyDirHugePage(deploymentName, imageName string) string {
2639+
return fmt.Sprintf(`resource "kubernetes_deployment_v1" "test" {
2640+
metadata {
2641+
name = "%s"
2642+
2643+
labels = {
2644+
Test = "TfAcceptanceTest"
2645+
}
2646+
}
2647+
2648+
spec {
2649+
replicas = 0 # We request zero replicas, since the K8S backing this test may not have huge pages available.
2650+
selector {
2651+
match_labels = {
2652+
Test = "TfAcceptanceTest"
2653+
}
2654+
}
2655+
2656+
template {
2657+
metadata {
2658+
labels = {
2659+
Test = "TfAcceptanceTest"
2660+
}
2661+
}
2662+
2663+
spec {
2664+
container {
2665+
image = "%s"
2666+
name = "containername"
2667+
command = ["sleep", "300"]
2668+
2669+
volume_mount {
2670+
mount_path = "/cache"
2671+
name = "cache-volume"
2672+
}
2673+
}
2674+
2675+
volume {
2676+
name = "cache-volume"
2677+
2678+
empty_dir {
2679+
medium = "HugePages-1Gi"
2680+
}
2681+
}
2682+
2683+
termination_grace_period_seconds = 1
2684+
}
2685+
}
2686+
}
2687+
}
2688+
`, deploymentName, imageName)
2689+
}
2690+
26112691
func testAccKubernetesDeploymentV1ConfigWithEmptyDirVolumesModified(deploymentName, imageName string) string {
26122692
return fmt.Sprintf(`resource "kubernetes_deployment_v1" "test" {
26132693
metadata {

kubernetes/schema_pod_spec.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package kubernetes
55

66
import (
7+
"fmt"
8+
79
corev1 "k8s.io/api/core/v1"
810

911
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -707,6 +709,15 @@ func volumeSchema(isUpdatable bool) *schema.Resource {
707709
},
708710
}
709711

712+
validEmptyDirMediums := []string{
713+
string(corev1.StorageMediumDefault),
714+
string(corev1.StorageMediumMemory),
715+
string(corev1.StorageMediumHugePages),
716+
// This is possibly not an exhaustive list, but it does cover the
717+
// common cases from x86_64 architectures.
718+
string(corev1.StorageMediumHugePagesPrefix) + "2Mi",
719+
string(corev1.StorageMediumHugePagesPrefix) + "1Gi",
720+
}
710721
v["empty_dir"] = &schema.Schema{
711722
Type: schema.TypeList,
712723
Description: "EmptyDir represents a temporary directory that shares a pod's lifetime. More info: https://kubernetes.io/docs/concepts/storage/volumes#emptydir",
@@ -715,15 +726,12 @@ func volumeSchema(isUpdatable bool) *schema.Resource {
715726
Elem: &schema.Resource{
716727
Schema: map[string]*schema.Schema{
717728
"medium": {
718-
Type: schema.TypeString,
719-
Description: `What type of storage medium should back this directory. The default is "" which means to use the node's default medium. Must be an empty string (default) or Memory. More info: https://kubernetes.io/docs/concepts/storage/volumes#emptydir`,
720-
Optional: true,
721-
Default: "",
722-
ForceNew: !isUpdatable,
723-
ValidateFunc: validation.StringInSlice([]string{
724-
string(corev1.StorageMediumDefault),
725-
string(corev1.StorageMediumMemory),
726-
}, false),
729+
Type: schema.TypeString,
730+
Description: fmt.Sprintf(`What type of storage medium should back this directory. The default is "" which means to use the node's default medium. Must be one of %q. More info: https://kubernetes.io/docs/concepts/storage/volumes#emptydir`, validEmptyDirMediums),
731+
Optional: true,
732+
Default: "",
733+
ForceNew: !isUpdatable,
734+
ValidateFunc: validation.StringInSlice(validEmptyDirMediums, false),
727735
},
728736
"size_limit": {
729737
Type: schema.TypeString,

0 commit comments

Comments
 (0)