Skip to content

Commit

Permalink
feat(container_node_pool): support cgroup mode (#8997)
Browse files Browse the repository at this point in the history
Signed-off-by: Tsubasa Nagasawa <[email protected]>
[upstream:8fa653aed5646ee55483612fa4d1a0706eb326bb]
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician committed Oct 3, 2023
1 parent 758925d commit aa317a0
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .changelog/8997.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
container: added `node_config.linux_node_config.cgroup_mode` field to `google_container_node_pool`
```
43 changes: 37 additions & 6 deletions google-beta/services/container/node_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,18 @@ func schemaNodeConfig() *schema.Schema {
Schema: map[string]*schema.Schema{
"sysctls": {
Type: schema.TypeMap,
Required: true,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The Linux kernel parameters to be applied to the nodes and all pods running on the nodes.`,
},
"cgroup_mode": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"CGROUP_MODE_UNSPECIFIED", "CGROUP_MODE_V1", "CGROUP_MODE_V2"}, false),
Description: `cgroupMode specifies the cgroup mode to be used on the node.`,
DiffSuppressFunc: tpgresource.EmptyOrDefaultStringSuppress("CGROUP_MODE_UNSPECIFIED"),
},
},
},
},
Expand Down Expand Up @@ -1011,17 +1019,39 @@ func expandLinuxNodeConfig(v interface{}) *container.LinuxNodeConfig {
return &container.LinuxNodeConfig{}
}
cfg := ls[0].(map[string]interface{})

linuxNodeConfig := &container.LinuxNodeConfig{}
sysctls := expandSysctls(cfg)
if sysctls != nil {
linuxNodeConfig.Sysctls = sysctls
}
cgroupMode := expandCgroupMode(cfg)
if len(cgroupMode) != 0 {
linuxNodeConfig.CgroupMode = cgroupMode
}

return linuxNodeConfig
}

func expandSysctls(cfg map[string]interface{}) map[string]string {
sysCfgRaw, ok := cfg["sysctls"]
if !ok {
return nil
}
m := make(map[string]string)
sysctls := make(map[string]string)
for k, v := range sysCfgRaw.(map[string]interface{}) {
m[k] = v.(string)
sysctls[k] = v.(string)
}
return &container.LinuxNodeConfig{
Sysctls: m,
return sysctls
}

func expandCgroupMode(cfg map[string]interface{}) string {
cgroupMode, ok := cfg["cgroup_mode"]
if !ok {
return ""
}

return cgroupMode.(string)
}

func expandSoleTenantConfig(v interface{}) *container.SoleTenantConfig {
Expand Down Expand Up @@ -1397,7 +1427,8 @@ func flattenLinuxNodeConfig(c *container.LinuxNodeConfig) []map[string]interface
result := []map[string]interface{}{}
if c != nil {
result = append(result, map[string]interface{}{
"sysctls": c.Sysctls,
"sysctls": c.Sysctls,
"cgroup_mode": c.CgroupMode,
})
}
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,38 @@ func TestAccContainerNodePool_withLinuxNodeConfig(t *testing.T) {
})
}

func TestAccContainerNodePool_withCgroupMode(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))

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

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

Expand Down Expand Up @@ -2622,6 +2654,39 @@ resource "google_container_node_pool" "with_linux_node_config" {
`, cluster, np, linuxNodeConfig)
}

func testAccContainerNodePool_withCgroupMode(cluster, np string, mode string) 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
}
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"
linux_node_config {
cgroup_mode = "%s"
}
oauth_scopes = [
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring",
]
}
}
`, cluster, np, mode)
}

func testAccContainerNodePool_withNetworkConfig(cluster, np, network string) string {
return fmt.Sprintf(`
resource "google_compute_network" "container_network" {
Expand Down
34 changes: 19 additions & 15 deletions website/docs/r/container_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -920,19 +920,7 @@ kubelet_config {
}
```

* `linux_node_config` - (Optional)
Linux node configuration, currently supported attributes can be found [here](https://cloud.google.com/sdk/gcloud/reference/beta/container/node-pools/create#--system-config-from-file).
Note that validations happen all server side. All attributes are optional.
Structure is [documented below](#nested_linux_node_config).

```hcl
linux_node_config {
sysctls = {
"net.core.netdev_max_backlog" = "10000"
"net.core.rmem_max" = "10000"
}
}
```
* `linux_node_config` - (Optional) Parameters that can be configured on Linux nodes. Structure is [documented below](#nested_linux_node_config).

* `node_group` - (Optional) Setting this field will assign instances of this pool to run on the specified node group. This is useful for running workloads on [sole tenant nodes](https://cloud.google.com/compute/docs/nodes/sole-tenant-nodes).

Expand Down Expand Up @@ -1238,9 +1226,25 @@ not specifying the `kubelet_config` block should be the equivalent of specifying

<a name="nested_linux_node_config"></a>The `linux_node_config` block supports:

* `sysctls` - (Required) The Linux kernel parameters to be applied to the nodes
* `sysctls` - (Optional) The Linux kernel parameters to be applied to the nodes
and all pods running on the nodes. Specified as a map from the key, such as
`net.core.wmem_max`, to a string value.
`net.core.wmem_max`, to a string value. Currently supported attributes can be found [here](https://cloud.google.com/sdk/gcloud/reference/beta/container/node-pools/create#--system-config-from-file).
Note that validations happen all server side. All attributes are optional.

```hcl
linux_node_config {
sysctls = {
"net.core.netdev_max_backlog" = "10000"
"net.core.rmem_max" = "10000"
}
}
```

* `cgroup_mode` - (Optional) Possible cgroup modes that can be used.
Accepted values are:
* `CGROUP_MODE_UNSPECIFIED`: CGROUP_MODE_UNSPECIFIED is when unspecified cgroup configuration is used. The default for the GKE node OS image will be used.
* `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.

<a name="nested_vertical_pod_autoscaling"></a>The `vertical_pod_autoscaling` block supports:

Expand Down

0 comments on commit aa317a0

Please sign in to comment.