Skip to content

Commit

Permalink
Allow specifying node pool cluster as a long-form id (#4842) (#3314)
Browse files Browse the repository at this point in the history
This allows users to specify an id-based relationship between the two, which will ensure the node pool is automatically re-created when the cluster is re-created.

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Jun 4, 2021
1 parent 267d96c commit 4c07af1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .changelog/4842.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
container: Allowed specifying a cluster id field for `google_container_node_pool.cluster` to ensure that a node pool is recreated if the associated cluster is recreated.
```
17 changes: 16 additions & 1 deletion google-beta/resource_container_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package google
import (
"fmt"
"log"
"regexp"
"strings"
"time"

Expand All @@ -13,6 +14,8 @@ import (
containerBeta "google.golang.org/api/container/v1beta1"
)

var clusterIdRegex = regexp.MustCompile("projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/clusters/(?P<name>[^/]+)")

func resourceContainerNodePool() *schema.Resource {
return &schema.Resource{
Create: resourceContainerNodePoolCreate,
Expand Down Expand Up @@ -242,6 +245,18 @@ func (nodePoolInformation *NodePoolInformation) lockKey() string {
}

func extractNodePoolInformation(d *schema.ResourceData, config *Config) (*NodePoolInformation, error) {
cluster := d.Get("cluster").(string)

if fieldValues := clusterIdRegex.FindStringSubmatch(cluster); fieldValues != nil {
log.Printf("[DEBUG] matching parent cluster %s to regex %s", cluster, clusterIdRegex.String())
return &NodePoolInformation{
project: fieldValues[1],
location: fieldValues[2],
cluster: fieldValues[3],
}, nil
}
log.Printf("[DEBUG] parent cluster %s does not match regex %s", cluster, clusterIdRegex.String())

project, err := getProject(d, config)
if err != nil {
return nil, err
Expand All @@ -255,7 +270,7 @@ func extractNodePoolInformation(d *schema.ResourceData, config *Config) (*NodePo
return &NodePoolInformation{
project: project,
location: location,
cluster: d.Get("cluster").(string),
cluster: cluster,
}, nil
}

Expand Down
43 changes: 43 additions & 0 deletions google-beta/resource_container_node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ func TestAccContainerNodePool_basic(t *testing.T) {
})
}

func TestAccContainerNodePool_basicWithClusterId(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_basicWithClusterId(cluster, np),
},
{
ResourceName: "google_container_node_pool.np",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"cluster"},
},
},
})
}

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

Expand Down Expand Up @@ -940,6 +964,25 @@ resource "google_container_node_pool" "np" {
`, cluster, np)
}

func testAccContainerNodePool_basicWithClusterId(cluster, np string) string {
return fmt.Sprintf(`
provider "google" {
user_project_override = true
}
resource "google_container_cluster" "cluster" {
name = "%s"
location = "us-central1-a"
initial_node_count = 3
}
resource "google_container_node_pool" "np" {
name = "%s"
cluster = google_container_cluster.cluster.id
initial_node_count = 2
}
`, cluster, np)
}

func testAccContainerNodePool_nodeLocations(cluster, np, network string) string {
return fmt.Sprintf(`
resource "google_compute_network" "container_network" {
Expand Down
8 changes: 3 additions & 5 deletions website/docs/r/container_node_pool.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ resource "google_container_cluster" "primary" {
resource "google_container_node_pool" "primary_preemptible_nodes" {
name = "my-node-pool"
location = "us-central1"
cluster = google_container_cluster.primary.name
cluster = google_container_cluster.primary.id
node_count = 1
node_config {
Expand All @@ -64,8 +63,7 @@ resource "google_service_account" "default" {
resource "google_container_node_pool" "np" {
name = "my-node-pool"
location = "us-central1-a"
cluster = google_container_cluster.primary.name
cluster = google_container_cluster.primary.id
node_config {
machine_type = "e2-medium"
# Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
Expand Down Expand Up @@ -105,7 +103,7 @@ resource "google_container_cluster" "primary" {

## Argument Reference

* `cluster` - (Required) The cluster to create the node pool for. Cluster must be present in `location` provided for zonal clusters.
* `cluster` - (Required) The cluster to create the node pool for. Cluster must be present in `location` provided for zonal clusters. May be specified in the format `projects/{{project}}/locations/{{location}}/clusters/{{cluster}}` or as just the name of the cluster.

- - -

Expand Down

0 comments on commit 4c07af1

Please sign in to comment.