Skip to content

Commit

Permalink
improve autoscaling_policy support in google_compute_node_group (#9044)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickElliot authored Sep 26, 2023
1 parent e59be4f commit e19357f
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 41 deletions.
25 changes: 7 additions & 18 deletions mmv1/products/compute/NodeGroup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ name: 'NodeGroup'
kind: 'compute#NodeGroup'
base_url: projects/{{project}}/zones/{{zone}}/nodeGroups
create_url: projects/{{project}}/zones/{{zone}}/nodeGroups?initialNodeCount=PRE_CREATE_REPLACE_ME
update_verb: :PATCH
update_mask: true
has_self_link: true
description: |
Represents a NodeGroup resource to manage a group of sole-tenant nodes.
immutable: true
references: !ruby/object:Api::Resource::ReferenceLinks
guides:
'Sole-Tenant Nodes': 'https://cloud.google.com/compute/docs/nodes/'
Expand All @@ -43,12 +44,6 @@ async: !ruby/object:Api::OpAsync
error: !ruby/object:Api::OpAsync::Error
path: 'error/errors'
message: 'message'
docs: !ruby/object:Provider::Terraform::Docs
warning: |
Due to limitations of the API, Terraform cannot update the
number of nodes in a node group and changes to node group size either
through Terraform config or through external changes will cause
Terraform to delete and recreate the node group.
examples:
- !ruby/object:Provider::Terraform::Examples
name: 'node_group_basic'
Expand Down Expand Up @@ -112,21 +107,13 @@ properties:
- !ruby/object:Api::Type::Integer
name: 'size'
description: |
The total number of nodes in the node group. One of `initial_size` or `size` must be specified.
immutable: true
send_empty_value: true
default_from_api: true
exactly_one_of:
- size
- initial_size
The total number of nodes in the node group.
output: true
- !ruby/object:Api::Type::Integer
name: 'initialSize'
description: |
The initial number of nodes in the node group. One of `initial_size` or `size` must be specified.
The initial number of nodes in the node group. One of `initial_size` or `autoscaling_policy` must be configured on resource creation.
url_param_only: true
exactly_one_of:
- size
- initial_size
- !ruby/object:Api::Type::String
name: 'maintenancePolicy'
description: |
Expand All @@ -147,6 +134,8 @@ properties:
description: |
If you use sole-tenant nodes for your workloads, you can use the node
group autoscaler to automatically manage the sizes of your node groups.
One of `initial_size` or `autoscaling_policy` must be configured on resource creation.
default_from_api: true
properties:
- !ruby/object:Api::Type::Enum
Expand Down
2 changes: 1 addition & 1 deletion mmv1/templates/terraform/examples/node_group_basic.tf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ resource "google_compute_node_group" "<%= ctx[:primary_resource_id] %>" {
zone = "us-central1-f"
description = "example google_compute_node_group for Terraform Google Provider"

size = 1
initial_size = 1
node_template = google_compute_node_template.soletenant-tmpl.id
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ resource "google_compute_node_group" "<%= ctx[:primary_resource_id] %>" {
zone = "us-central1-f"
description = "example google_compute_node_group for Terraform Google Provider"

size = 1
initial_size = 1
node_template = google_compute_node_template.soletenant-tmpl.id

share_settings {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
var sizeParam string
if v, ok := d.GetOkExists("size"); ok {
sizeParam = fmt.Sprintf("%v", v)
} else if v, ok := d.GetOkExists("initial_size"); ok {
if v, ok := d.GetOkExists("initial_size"); ok {
sizeParam = fmt.Sprintf("%v", v)
}else{
if _, ok := d.GetOkExists("autoscaling_policy"); ok{
sizeParam = fmt.Sprintf("%v", d.Get("autoscaling_policy.min_nodes"))
}else{
return errors.New("An initial_size or autoscaling_policy must be configured on node group creation.")
}
}

url = regexp.MustCompile("PRE_CREATE_REPLACE_ME").ReplaceAllLiteralString(url, sizeParam)
Original file line number Diff line number Diff line change
Expand Up @@ -5996,7 +5996,7 @@ resource "google_compute_node_group" "nodes" {
name = "%s"
zone = "us-central1-a"

size = 1
initial_size = 1
node_template = google_compute_node_template.nodetmpl.self_link
}
`, instance, nodeTemplate, nodeGroup)
Expand Down Expand Up @@ -6065,7 +6065,7 @@ resource "google_compute_node_group" "nodes" {
name = "%s"
zone = "us-central1-a"

size = 1
initial_size = 1
node_template = google_compute_node_template.nodetmpl.self_link
}
`, instance, nodeTemplate, nodeGroup)
Expand Down Expand Up @@ -6134,7 +6134,7 @@ resource "google_compute_node_group" "nodes" {
name = "%s"
zone = "us-central1-a"

size = 1
initial_size = 1
node_template = google_compute_node_template.nodetmpl.self_link
}
`, instance, nodeTemplate, nodeGroup)
Expand Down Expand Up @@ -6197,7 +6197,7 @@ resource "google_compute_node_group" "nodes" {
name = "%s"
zone = "us-central1-a"

size = 1
initial_size = 1
node_template = google_compute_node_template.nodetmpl.self_link
}
`, instance, nodeTemplate, nodeGroup)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"strings"
"time"

"regexp"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-provider-google/google/acctest"
)

func TestAccComputeNodeGroup_updateNodeTemplate(t *testing.T) {
func TestAccComputeNodeGroup_update(t *testing.T) {
t.Parallel()

groupName := fmt.Sprintf("group--%d", acctest.RandInt(t))
Expand All @@ -26,26 +28,47 @@ func TestAccComputeNodeGroup_updateNodeTemplate(t *testing.T) {
CheckDestroy: testAccCheckComputeNodeGroupDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeNodeGroup_updateNodeTemplate(groupName, tmplPrefix, "tmpl1"),
Config: testAccComputeNodeGroup_update(groupName, tmplPrefix, "tmpl1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeNodeGroupCreationTimeBefore(&timeCreated),
),
},
{
ResourceName: "google_compute_node_group.nodes",
ImportState: true,
ImportStateVerify: true,
ResourceName: "google_compute_node_group.nodes",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"initial_size"},
},
{
Config: testAccComputeNodeGroup_updateNodeTemplate(groupName, tmplPrefix, "tmpl2"),
Config: testAccComputeNodeGroup_update2(groupName, tmplPrefix, "tmpl2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeNodeGroupCreationTimeBefore(&timeCreated),
),
},
{
ResourceName: "google_compute_node_group.nodes",
ImportState: true,
ImportStateVerify: true,
ResourceName: "google_compute_node_group.nodes",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"initial_size"},
},
},
})
}

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

groupName := fmt.Sprintf("group--%d", acctest.RandInt(t))
tmplPrefix := fmt.Sprintf("tmpl--%d", acctest.RandInt(t))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeNodeGroupDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeNodeGroup_fail(groupName, tmplPrefix, "tmpl1"),
ExpectError: regexp.MustCompile("An initial_size or autoscaling_policy must be configured on node group creation."),
},
},
})
Expand Down Expand Up @@ -86,7 +109,7 @@ func testAccCheckComputeNodeGroupCreationTimeBefore(prevTimeCreated *time.Time)
}
}

func testAccComputeNodeGroup_updateNodeTemplate(groupName, tmplPrefix, tmplToUse string) string {
func testAccComputeNodeGroup_update(groupName, tmplPrefix, tmplToUse string) string {
return fmt.Sprintf(`
resource "google_compute_node_template" "tmpl1" {
name = "%s-first"
Expand All @@ -105,8 +128,58 @@ resource "google_compute_node_group" "nodes" {
zone = "us-central1-a"
description = "example google_compute_node_group for Terraform Google Provider"

size = 0
initial_size = 1
node_template = google_compute_node_template.%s.self_link
}

`, tmplPrefix, tmplPrefix, groupName, tmplToUse)
}

func testAccComputeNodeGroup_update2(groupName, tmplPrefix, tmplToUse string) string {
return fmt.Sprintf(`
resource "google_compute_node_template" "tmpl1" {
name = "%s-first"
region = "us-central1"
node_type = "n1-node-96-624"
}

resource "google_compute_node_template" "tmpl2" {
name = "%s-second"
region = "us-central1"
node_type = "n1-node-96-624"
}

resource "google_compute_node_group" "nodes" {
name = "%s"
zone = "us-central1-a"
description = "example google_compute_node_group for Terraform Google Provider"

autoscaling_policy {
mode = "ONLY_SCALE_OUT"
min_nodes = 1
max_nodes = 10
}
node_template = google_compute_node_template.%s.self_link
}

`, tmplPrefix, tmplPrefix, groupName, tmplToUse)
}

func testAccComputeNodeGroup_fail(groupName, tmplPrefix, tmplToUse string) string {
return fmt.Sprintf(`
resource "google_compute_node_template" "tmpl1" {
name = "%s-first"
region = "us-central1"
node_type = "n1-node-96-624"
}

resource "google_compute_node_group" "nodes" {
name = "%s"
zone = "us-central1-a"
description = "example google_compute_node_group for Terraform Google Provider"

node_template = google_compute_node_template.%s.self_link
}

`, tmplPrefix, groupName, tmplToUse)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7963,7 +7963,7 @@ resource "google_compute_node_group" "group" {
zone = "us-central1-f"
description = "example google_compute_node_group for Terraform Google Provider"

size = 1
initial_size = 1
node_template = google_compute_node_template.soletenant-tmpl.id
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3246,7 +3246,7 @@ resource "google_compute_node_template" "soletenant-tmpl" {
resource "google_compute_node_group" "nodes" {
name = "tf-test-soletenant-group"
zone = "us-central1-a"
size = 1
initial_size = 1
node_template = google_compute_node_template.soletenant-tmpl.id
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@ resource "google_compute_node_group" "nodes" {
name = "test-nodegroup-%s"
zone = "us-central1-f"

size = 3
initial_size = 3
node_template = google_compute_node_template.nodetmpl.self_link
}

Expand Down

0 comments on commit e19357f

Please sign in to comment.