From 9dc1198b8a7e8828179c2c7393e2fb9d9d3e2148 Mon Sep 17 00:00:00 2001 From: Maria Date: Wed, 2 Aug 2023 00:22:24 +0200 Subject: [PATCH] Add support for policy_name field in Placement Policy (#8475) --- .../resource_container_node_pool.go.erb | 8 +++ .../resource_container_cluster_test.go.erb | 61 ++++++++++++++++++ .../resource_container_node_pool_test.go.erb | 62 +++++++++++++++++++ .../docs/r/container_node_pool.html.markdown | 4 ++ 4 files changed, 135 insertions(+) diff --git a/mmv1/third_party/terraform/services/container/resource_container_node_pool.go.erb b/mmv1/third_party/terraform/services/container/resource_container_node_pool.go.erb index bc5b7fa0115f..9d73c6ccb416 100644 --- a/mmv1/third_party/terraform/services/container/resource_container_node_pool.go.erb +++ b/mmv1/third_party/terraform/services/container/resource_container_node_pool.go.erb @@ -191,6 +191,12 @@ var schemaNodePool = map[string]*schema.Schema{ Required: true, Description: `Type defines the type of placement policy`, }, + "policy_name": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + 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.`, + }, <% unless version == 'ga' -%> "tpu_topology": { Type: schema.TypeString, @@ -937,6 +943,7 @@ func expandNodePool(d *schema.ResourceData, prefix string) (*container.NodePool, placement_policy := v.([]interface{})[0].(map[string]interface{}) np.PlacementPolicy = &container.PlacementPolicy{ Type: placement_policy["type"].(string), + PolicyName: placement_policy["policy_name"].(string), <% unless version == 'ga' -%> TpuTopology: placement_policy["tpu_topology"].(string), <% end -%> @@ -1131,6 +1138,7 @@ func flattenNodePool(d *schema.ResourceData, config *transport_tpg.Config, np *c nodePool["placement_policy"] = []map[string]interface{}{ { "type": np.PlacementPolicy.Type, + "policy_name": np.PlacementPolicy.PolicyName, <% unless version == 'ga' -%> "tpu_topology": np.PlacementPolicy.TpuTopology, <% end -%> diff --git a/mmv1/third_party/terraform/tests/resource_container_cluster_test.go.erb b/mmv1/third_party/terraform/tests/resource_container_cluster_test.go.erb index cd510fa9f630..2553a49d3876 100644 --- a/mmv1/third_party/terraform/tests/resource_container_cluster_test.go.erb +++ b/mmv1/third_party/terraform/tests/resource_container_cluster_test.go.erb @@ -7855,3 +7855,64 @@ resource "google_container_cluster" "primary" { min_master_version = 1.27 }`, name, enabled) } + + +func TestAccContainerCluster_customPlacementPolicy(t *testing.T) { + t.Parallel() + + cluster := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10)) + np := fmt.Sprintf("tf-test-nodepool-%s", acctest.RandString(t, 10)) + policy := fmt.Sprintf("tf-test-policy-%s", acctest.RandString(t, 10)) + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckContainerNodePoolDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccContainerCluster_customPlacementPolicy(cluster, np, policy), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("google_container_cluster.cluster", "node_pool.0.placement_policy.0.type", "COMPACT"), + resource.TestCheckResourceAttr("google_container_cluster.cluster", "node_pool.0.placement_policy.0.policy_name", policy), + resource.TestCheckResourceAttr("google_container_cluster.cluster", "node_pool.0.node_config.0.machine_type", "c2-standard-4"), + ), + }, + { + ResourceName: "google_container_cluster.cluster", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccContainerCluster_customPlacementPolicy(cluster, np, policyName string) string { + return fmt.Sprintf(` + +resource "google_compute_resource_policy" "policy" { + name = "%s" + region = "us-central1" + group_placement_policy { + collocation = "COLLOCATED" + } +} + +resource "google_container_cluster" "cluster" { + name = "%s" + location = "us-central1-a" + + node_pool { + name = "%s" + initial_node_count = 2 + + node_config { + machine_type = "c2-standard-4" + } + + placement_policy { + type = "COMPACT" + policy_name = google_compute_resource_policy.policy.name + } + } +}`, policyName, cluster, np) +} diff --git a/mmv1/third_party/terraform/tests/resource_container_node_pool_test.go.erb b/mmv1/third_party/terraform/tests/resource_container_node_pool_test.go.erb index 60b17e8266c8..5b6fe484e440 100644 --- a/mmv1/third_party/terraform/tests/resource_container_node_pool_test.go.erb +++ b/mmv1/third_party/terraform/tests/resource_container_node_pool_test.go.erb @@ -1558,6 +1558,68 @@ resource "google_container_node_pool" "np" { `, cluster, np, placementType) } +func TestAccContainerNodePool_customPlacementPolicy(t *testing.T) { + t.Parallel() + + cluster := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10)) + np := fmt.Sprintf("tf-test-nodepool-%s", acctest.RandString(t, 10)) + policy := fmt.Sprintf("tf-test-policy-%s", acctest.RandString(t, 10)) + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckContainerNodePoolDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccContainerNodePool_customPlacementPolicy(cluster, np, policy), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("google_container_node_pool.np", "node_config.0.machine_type", "c2-standard-4"), + resource.TestCheckResourceAttr("google_container_node_pool.np", "placement_policy.0.policy_name", policy), + resource.TestCheckResourceAttr("google_container_node_pool.np", "placement_policy.0.type", "COMPACT"), + ), + }, + { + ResourceName: "google_container_node_pool.np", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccContainerNodePool_customPlacementPolicy(cluster, np, policyName string) string { + return fmt.Sprintf(` +resource "google_container_cluster" "cluster" { + name = "%s" + location = "us-central1-a" + initial_node_count = 1 +} + +resource "google_compute_resource_policy" "policy" { + name = "%s" + region = "us-central1" + group_placement_policy { + collocation = "COLLOCATED" + } +} + +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 = "COMPACT" + policy_name = google_compute_resource_policy.policy.name + } +} +`, cluster, policyName, np) +} + func TestAccContainerNodePool_threadsPerCore(t *testing.T) { t.Parallel() diff --git a/mmv1/third_party/terraform/website/docs/r/container_node_pool.html.markdown b/mmv1/third_party/terraform/website/docs/r/container_node_pool.html.markdown index 8f7d75246505..4a8f19b26cd3 100644 --- a/mmv1/third_party/terraform/website/docs/r/container_node_pool.html.markdown +++ b/mmv1/third_party/terraform/website/docs/r/container_node_pool.html.markdown @@ -262,6 +262,10 @@ cluster. Specifying COMPACT placement policy type places node pool's nodes in a closer physical proximity in order to reduce network latency between nodes. +* `policy_name` - (Optional) 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. + * `tpu_topology` - (Optional, Beta) The [TPU placement topology](https://cloud.google.com/tpu/docs/types-topologies#tpu_topologies) for pod slice node pool. ## Attributes Reference