Skip to content

Commit

Permalink
container - add hugepages config (GoogleCloudPlatform#11541)
Browse files Browse the repository at this point in the history
Signed-off-by: drfaust92 <[email protected]>
Co-authored-by: Will Yardley <[email protected]>
  • Loading branch information
2 people authored and abd-goog committed Sep 23, 2024
1 parent 48e4304 commit e9b2135
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 2 deletions.
66 changes: 64 additions & 2 deletions mmv1/third_party/terraform/services/container/node_config.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,26 @@ func schemaNodeConfig() *schema.Schema {
Description: `cgroupMode specifies the cgroup mode to be used on the node.`,
DiffSuppressFunc: tpgresource.EmptyOrDefaultStringSuppress("CGROUP_MODE_UNSPECIFIED"),
},
"hugepages_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: `Amounts for 2M and 1G hugepages.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"hugepage_size_2m": {
Type: schema.TypeInt,
Optional: true,
Description: `Amount of 2M hugepages.`,
},
"hugepage_size_1g": {
Type: schema.TypeInt,
Optional: true,
Description: `Amount of 1G hugepages.`,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -1246,6 +1266,10 @@ func expandLinuxNodeConfig(v interface{}) *container.LinuxNodeConfig {
linuxNodeConfig.CgroupMode = cgroupMode
}

if v, ok := cfg["hugepages_config"]; ok {
linuxNodeConfig.Hugepages = expandHugepagesConfig(v)
}

return linuxNodeConfig
}

Expand All @@ -1270,6 +1294,32 @@ func expandCgroupMode(cfg map[string]interface{}) string {
return cgroupMode.(string)
}

func expandHugepagesConfig(v interface{}) *container.HugepagesConfig {
if v == nil {
return nil
}
ls := v.([]interface{})
if len(ls) == 0 {
return nil
}
if ls[0] == nil {
return &container.HugepagesConfig{}
}
cfg := ls[0].(map[string]interface{})

hugepagesConfig := &container.HugepagesConfig{}

if v, ok := cfg["hugepage_size_2m"]; ok {
hugepagesConfig.HugepageSize2m = int64(v.(int))
}

if v, ok := cfg["hugepage_size_1g"]; ok {
hugepagesConfig.HugepageSize1g = int64(v.(int))
}

return hugepagesConfig
}

func expandContainerdConfig(v interface{}) *container.ContainerdConfig {
if v == nil {
return nil
Expand Down Expand Up @@ -1800,8 +1850,20 @@ func flattenLinuxNodeConfig(c *container.LinuxNodeConfig) []map[string]interface
result := []map[string]interface{}{}
if c != nil {
result = append(result, map[string]interface{}{
"sysctls": c.Sysctls,
"cgroup_mode": c.CgroupMode,
"sysctls": c.Sysctls,
"cgroup_mode": c.CgroupMode,
"hugepages_config": flattenHugepagesConfig(c.Hugepages),
})
}
return result
}

func flattenHugepagesConfig(c *container.HugepagesConfig) []map[string]interface{} {
result := []map[string]interface{}{}
if c != nil {
result = append(result, map[string]interface{}{
"hugepage_size_2m": c.HugepageSize2m,
"hugepage_size_1g": c.HugepageSize1g,
})
}
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,40 @@ func TestAccContainerNodePool_withCgroupMode(t *testing.T) {
})
}

func TestAccContainerNodePool_withHugepageConfig(t *testing.T) {
t.Parallel()

cluster := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
np := fmt.Sprintf("tf-test-np-%s", acctest.RandString(t, 10))
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerNodePool_withHugepageConfig(cluster, np, networkName, subnetworkName, 1),
},
{
ResourceName: "google_container_node_pool.np",
ImportState: true,
ImportStateVerify: true,
},
// Perform an update.
{
Config: testAccContainerNodePool_withHugepageConfig(cluster, np, networkName, subnetworkName, 2),
},
{
ResourceName: "google_container_node_pool.np",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccContainerNodePool_withNetworkConfig(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -3391,6 +3425,46 @@ resource "google_container_node_pool" "with_tier1_net" {
`, network, cluster, np, np, np, np, netTier)
}


func testAccContainerNodePool_withHugepageConfig(cluster, np, networkName, subnetworkName string, hugepage int) string {
return fmt.Sprintf(`
data "google_container_engine_versions" "central1a" {
location = "us-central1-a"
}

resource "google_container_cluster" "cluster" {
name = "%s"
location = "us-central1-a"
initial_node_count = 1
min_master_version = data.google_container_engine_versions.central1a.latest_master_version
deletion_protection = false
network = "%s"
subnetwork = "%s"
}

resource "google_container_node_pool" "np" {
name = "%s"
location = "us-central1-a"
cluster = google_container_cluster.cluster.name
initial_node_count = 1
node_config {
image_type = "COS_CONTAINERD"
machine_type = "c2d-standard-2" # This is required for hugepage_size_1g https://cloud.google.com/kubernetes-engine/docs/how-to/node-system-config#huge-page-options
linux_node_config {
hugepages_config {
hugepage_size_2m = %d
hugepage_size_1g = %d
}
}
oauth_scopes = [
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring",
]
}
}
`, cluster, networkName, subnetworkName, np, hugepage, hugepage)
}

func testAccContainerNodePool_withMultiNicNetworkConfig(cluster, np, network string) string {
return fmt.Sprintf(`
resource "google_compute_network" "container_network" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,14 @@ linux_node_config {
* `CGROUP_MODE_V1`: CGROUP_MODE_V1 specifies to use cgroupv1 for the cgroup configuration on the node image.
* `CGROUP_MODE_V2`: CGROUP_MODE_V2 specifies to use cgroupv2 for the cgroup configuration on the node image.

* `hugepages_config` - (Optional) Amounts for 2M and 1G hugepages. Structure is [documented below](#nested_hugepages_config).

<a name="nested_hugepages_config"></a>The `hugepages_config` block supports:

* `hugepage_size_2m` - (Optional) Amount of 2M hugepages.

* `hugepage_size_1g` - (Optional) Amount of 1G hugepages.

<a name="nested_containerd_config"></a>The `containerd_config` block supports:

* `private_registry_access_config` (Optional) - Configuration for private container registries. There are two fields in this config:
Expand Down

0 comments on commit e9b2135

Please sign in to comment.