Skip to content

Commit 3783c85

Browse files
Add support for policy_name field in Placement Policy (#8475) (#5994)
Signed-off-by: Modular Magician <[email protected]>
1 parent 2932b44 commit 3783c85

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

.changelog/8475.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
container: added `placement_policy.policy_name` field to `google_container_node_pool` resource
3+
```

google-beta/resource_container_cluster_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7802,3 +7802,63 @@ resource "google_container_cluster" "primary" {
78027802
min_master_version = 1.27
78037803
}`, name, enabled)
78047804
}
7805+
7806+
func TestAccContainerCluster_customPlacementPolicy(t *testing.T) {
7807+
t.Parallel()
7808+
7809+
cluster := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
7810+
np := fmt.Sprintf("tf-test-nodepool-%s", acctest.RandString(t, 10))
7811+
policy := fmt.Sprintf("tf-test-policy-%s", acctest.RandString(t, 10))
7812+
7813+
acctest.VcrTest(t, resource.TestCase{
7814+
PreCheck: func() { acctest.AccTestPreCheck(t) },
7815+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
7816+
CheckDestroy: testAccCheckContainerNodePoolDestroyProducer(t),
7817+
Steps: []resource.TestStep{
7818+
{
7819+
Config: testAccContainerCluster_customPlacementPolicy(cluster, np, policy),
7820+
Check: resource.ComposeTestCheckFunc(
7821+
resource.TestCheckResourceAttr("google_container_cluster.cluster", "node_pool.0.placement_policy.0.type", "COMPACT"),
7822+
resource.TestCheckResourceAttr("google_container_cluster.cluster", "node_pool.0.placement_policy.0.policy_name", policy),
7823+
resource.TestCheckResourceAttr("google_container_cluster.cluster", "node_pool.0.node_config.0.machine_type", "c2-standard-4"),
7824+
),
7825+
},
7826+
{
7827+
ResourceName: "google_container_cluster.cluster",
7828+
ImportState: true,
7829+
ImportStateVerify: true,
7830+
},
7831+
},
7832+
})
7833+
}
7834+
7835+
func testAccContainerCluster_customPlacementPolicy(cluster, np, policyName string) string {
7836+
return fmt.Sprintf(`
7837+
7838+
resource "google_compute_resource_policy" "policy" {
7839+
name = "%s"
7840+
region = "us-central1"
7841+
group_placement_policy {
7842+
collocation = "COLLOCATED"
7843+
}
7844+
}
7845+
7846+
resource "google_container_cluster" "cluster" {
7847+
name = "%s"
7848+
location = "us-central1-a"
7849+
7850+
node_pool {
7851+
name = "%s"
7852+
initial_node_count = 2
7853+
7854+
node_config {
7855+
machine_type = "c2-standard-4"
7856+
}
7857+
7858+
placement_policy {
7859+
type = "COMPACT"
7860+
policy_name = google_compute_resource_policy.policy.name
7861+
}
7862+
}
7863+
}`, policyName, cluster, np)
7864+
}

google-beta/resource_container_node_pool_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,68 @@ resource "google_container_node_pool" "np" {
15471547
`, cluster, np, placementType)
15481548
}
15491549

1550+
func TestAccContainerNodePool_customPlacementPolicy(t *testing.T) {
1551+
t.Parallel()
1552+
1553+
cluster := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
1554+
np := fmt.Sprintf("tf-test-nodepool-%s", acctest.RandString(t, 10))
1555+
policy := fmt.Sprintf("tf-test-policy-%s", acctest.RandString(t, 10))
1556+
1557+
acctest.VcrTest(t, resource.TestCase{
1558+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1559+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1560+
CheckDestroy: testAccCheckContainerNodePoolDestroyProducer(t),
1561+
Steps: []resource.TestStep{
1562+
{
1563+
Config: testAccContainerNodePool_customPlacementPolicy(cluster, np, policy),
1564+
Check: resource.ComposeTestCheckFunc(
1565+
resource.TestCheckResourceAttr("google_container_node_pool.np", "node_config.0.machine_type", "c2-standard-4"),
1566+
resource.TestCheckResourceAttr("google_container_node_pool.np", "placement_policy.0.policy_name", policy),
1567+
resource.TestCheckResourceAttr("google_container_node_pool.np", "placement_policy.0.type", "COMPACT"),
1568+
),
1569+
},
1570+
{
1571+
ResourceName: "google_container_node_pool.np",
1572+
ImportState: true,
1573+
ImportStateVerify: true,
1574+
},
1575+
},
1576+
})
1577+
}
1578+
1579+
func testAccContainerNodePool_customPlacementPolicy(cluster, np, policyName string) string {
1580+
return fmt.Sprintf(`
1581+
resource "google_container_cluster" "cluster" {
1582+
name = "%s"
1583+
location = "us-central1-a"
1584+
initial_node_count = 1
1585+
}
1586+
1587+
resource "google_compute_resource_policy" "policy" {
1588+
name = "%s"
1589+
region = "us-central1"
1590+
group_placement_policy {
1591+
collocation = "COLLOCATED"
1592+
}
1593+
}
1594+
1595+
resource "google_container_node_pool" "np" {
1596+
name = "%s"
1597+
location = "us-central1-a"
1598+
cluster = google_container_cluster.cluster.name
1599+
initial_node_count = 2
1600+
1601+
node_config {
1602+
machine_type = "c2-standard-4"
1603+
}
1604+
placement_policy {
1605+
type = "COMPACT"
1606+
policy_name = google_compute_resource_policy.policy.name
1607+
}
1608+
}
1609+
`, cluster, policyName, np)
1610+
}
1611+
15501612
func TestAccContainerNodePool_threadsPerCore(t *testing.T) {
15511613
t.Parallel()
15521614

google-beta/services/container/resource_container_node_pool.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ var schemaNodePool = map[string]*schema.Schema{
188188
Required: true,
189189
Description: `Type defines the type of placement policy`,
190190
},
191+
"policy_name": {
192+
Type: schema.TypeString,
193+
Optional: true,
194+
ForceNew: true,
195+
Description: `If set, refers to the name of a custom resource policy supplied by the user. The resource policy must be in the same project and region as the node pool. If not found, InvalidArgument error is returned.`,
196+
},
191197
"tpu_topology": {
192198
Type: schema.TypeString,
193199
Optional: true,
@@ -929,6 +935,7 @@ func expandNodePool(d *schema.ResourceData, prefix string) (*container.NodePool,
929935
placement_policy := v.([]interface{})[0].(map[string]interface{})
930936
np.PlacementPolicy = &container.PlacementPolicy{
931937
Type: placement_policy["type"].(string),
938+
PolicyName: placement_policy["policy_name"].(string),
932939
TpuTopology: placement_policy["tpu_topology"].(string),
933940
}
934941
}
@@ -1121,6 +1128,7 @@ func flattenNodePool(d *schema.ResourceData, config *transport_tpg.Config, np *c
11211128
nodePool["placement_policy"] = []map[string]interface{}{
11221129
{
11231130
"type": np.PlacementPolicy.Type,
1131+
"policy_name": np.PlacementPolicy.PolicyName,
11241132
"tpu_topology": np.PlacementPolicy.TpuTopology,
11251133
},
11261134
}

website/docs/r/container_node_pool.html.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ cluster.
262262
Specifying COMPACT placement policy type places node pool's nodes in a closer
263263
physical proximity in order to reduce network latency between nodes.
264264

265+
* `policy_name` - (Optional) If set, refers to the name of a custom resource policy supplied by the user.
266+
The resource policy must be in the same project and region as the node pool.
267+
If not found, InvalidArgument error is returned.
268+
265269
* `tpu_topology` - (Optional, Beta) The [TPU placement topology](https://cloud.google.com/tpu/docs/types-topologies#tpu_topologies) for pod slice node pool.
266270

267271
## Attributes Reference

0 commit comments

Comments
 (0)