Skip to content

Commit

Permalink
Add support for Compact Placement feature for GKE nodes (#5656) (#4043)
Browse files Browse the repository at this point in the history
* Add support for the Compact Placement node pool feature

* Update GKE API dependency

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Feb 10, 2022
1 parent bc1dba4 commit 3a69034
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/5656.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
container: Add support for GKE Compact Placement
```
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,7 @@ google.golang.org/genproto v0.0.0-20211206160659-862468c7d6e0/go.mod h1:5CzLGKJ6
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20211221195035-429b39de9b1c/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20211223182754-3ac035c7e7cb/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368 h1:Et6SkiuvnBn+SgrSYXs/BrUpGB4mbdwt4R3vaPIlicA=
google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20220111164026-67b88f271998 h1:g/x+MYjJYDEP3OBCYYmwIbt4x6k3gryb+ohyOR7PXfI=
google.golang.org/genproto v0.0.0-20220111164026-67b88f271998/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
Expand Down
32 changes: 32 additions & 0 deletions google-beta/resource_container_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ var schemaNodePool = map[string]*schema.Schema{
},
},

"placement_policy": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: `Specifies the node placement policy`,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
Description: `Type defines the type of placement policy`,
},
},
},
},

"max_pods_per_node": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -736,6 +753,13 @@ func expandNodePool(d *schema.ResourceData, prefix string) (*container.NodePool,
}
}

if v, ok := d.GetOk(prefix + "placement_policy"); ok {
placement_policy := v.([]interface{})[0].(map[string]interface{})
np.PlacementPolicy = &container.PlacementPolicy{
Type: placement_policy["type"].(string),
}
}

if v, ok := d.GetOk(prefix + "max_pods_per_node"); ok {
np.MaxPodsConstraint = &container.MaxPodsConstraint{
MaxPodsPerNode: int64(v.(int)),
Expand Down Expand Up @@ -831,6 +855,14 @@ func flattenNodePool(d *schema.ResourceData, config *Config, np *container.NodeP
}
}

if np.PlacementPolicy != nil {
nodePool["placement_policy"] = []map[string]interface{}{
{
"type": np.PlacementPolicy.Type,
},
}
}

if np.MaxPodsConstraint != nil {
nodePool["max_pods_per_node"] = np.MaxPodsConstraint.MaxPodsPerNode
}
Expand Down
47 changes: 47 additions & 0 deletions google-beta/resource_container_node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,53 @@ resource "google_container_node_pool" "np" {
`, cluster, np)
}

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

cluster := fmt.Sprintf("tf-test-cluster-%s", randString(t, 10))
np := fmt.Sprintf("tf-test-nodepool-%s", randString(t, 10))

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerNodePoolDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerNodePool_compactPlacement(cluster, np, "COMPACT"),
},
{
ResourceName: "google_container_cluster.cluster",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccContainerNodePool_compactPlacement(cluster, np, placementType string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "cluster" {
name = "%s"
location = "us-central1-a"
initial_node_count = 1
}
resource "google_container_node_pool" "np" {
name = "%s"
location = "us-central1-a"
cluster = google_container_cluster.cluster.name
initial_node_count = 2
node_config {
machine_type = "c2-standard-4"
}
placement_policy {
type = "%s"
}
}
`, cluster, np, placementType)
}

func testAccCheckContainerNodePoolDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
config := googleProviderConfig(t)
Expand Down
9 changes: 9 additions & 0 deletions website/docs/r/container_node_pool.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ cluster.
when fuzzy versions are used. See the `google_container_engine_versions` data source's
`version_prefix` field to approximate fuzzy versions in a Terraform-compatible way.

* `placement_policy` - (Optional, [Beta](https://terraform.io/docs/providers/google/provider_versions.html)) Specifies a custom placement policy for the
nodes.

<a name="nested_autoscaling"></a>The `autoscaling` block supports:

* `min_node_count` - (Required) Minimum number of nodes in the NodePool. Must be >=0 and
Expand All @@ -195,6 +198,12 @@ cluster.

`max_surge` and `max_unavailable` must not be negative and at least one of them must be greater than zero.

<a name="nested_placement_policy"></a>The `placement_policy` block supports:

* `type` - (Required) The type of the policy. Supports a single value: COMPACT.
Specifying COMPACT placement policy type places node pool's nodes in a closer
physical proximity in order to reduce network latency between nodes.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:
Expand Down

0 comments on commit 3a69034

Please sign in to comment.