Skip to content

Commit

Permalink
skip guest accelerators if count is 0.
Browse files Browse the repository at this point in the history
Instances in instance groups in google will fail to provision, despite
requesting 0 GPUs. This came up for me when trying to provision
a similar instance group in all available regions, but only asking for
GPU's in those that support them by parameterizing the `count` and
setting it to 0.

This might be a violation of some terraform principles. For example,
testing locally with this change `terraform` did not recognize that
indeed my infra needed to be re-deployed (from it's pov, I assume it
believes this because inputs hadn't changed). Additionally, there may be
valid reasons for creating an instance template with 0 gpu's that can be
tuned upwards.
  • Loading branch information
jacobstr committed Dec 14, 2017
1 parent 450a2c9 commit 8fba76f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
3 changes: 3 additions & 0 deletions google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,9 @@ func expandInstanceGuestAccelerators(d TerraformResourceData, config *Config) ([
guestAccelerators := make([]*computeBeta.AcceleratorConfig, len(accels))
for i, raw := range accels {
data := raw.(map[string]interface{})
if data["count"].(int) == 0 {
continue
}
at, err := ParseAcceleratorFieldValue(data["type"].(string), d, config)
if err != nil {
return nil, fmt.Errorf("cannot parse accelerator type: %v", err)
Expand Down
3 changes: 3 additions & 0 deletions google/resource_compute_instance_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,9 @@ func expandInstanceTemplateGuestAccelerators(d TerraformResourceData, config *Co
guestAccelerators := make([]*computeBeta.AcceleratorConfig, len(accels))
for i, raw := range accels {
data := raw.(map[string]interface{})
if data["count"].(int) == 0 {
continue
}
guestAccelerators[i] = &computeBeta.AcceleratorConfig{
AcceleratorCount: int64(data["count"].(int)),
// We can't use ParseAcceleratorFieldValue here because an instance
Expand Down
40 changes: 36 additions & 4 deletions google/resource_compute_instance_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func TestAccComputeInstanceTemplate_guestAccelerator(t *testing.T) {
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstanceTemplate_guestAccelerator(acctest.RandString(10)),
Config: testAccComputeInstanceTemplate_guestAccelerator(acctest.RandString(10), 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceTemplateExists("google_compute_instance_template.foobar", &instanceTemplate),
testAccCheckComputeInstanceTemplateHasGuestAccelerator(&instanceTemplate, "nvidia-tesla-k80", 1),
Expand All @@ -312,6 +312,28 @@ func TestAccComputeInstanceTemplate_guestAccelerator(t *testing.T) {

}

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

var instanceTemplate compute.InstanceTemplate

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstanceTemplate_guestAccelerator(acctest.RandString(10), 0),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceTemplateExists("google_compute_instance_template.foobar", &instanceTemplate),
testAccCheckComputeInstanceTemplateLacksGuestAccelerator(&instanceTemplate),
),
},
},
})

}

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

Expand Down Expand Up @@ -604,6 +626,16 @@ func testAccCheckComputeInstanceTemplateHasGuestAccelerator(instanceTemplate *co
}
}

func testAccCheckComputeInstanceTemplateLacksGuestAccelerator(instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc {
return func(s *terraform.State) error {
if len(instanceTemplate.Properties.GuestAccelerators) > 0 {
return fmt.Errorf("Expected no guest accelerators")
}

return nil
}
}

func testAccCheckComputeInstanceTemplateHasMinCpuPlatform(instanceTemplate *compute.InstanceTemplate, minCpuPlatform string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if instanceTemplate.Properties.MinCpuPlatform != minCpuPlatform {
Expand Down Expand Up @@ -1027,7 +1059,7 @@ resource "google_compute_instance_template" "foobar" {
}`, i, i, i)
}

func testAccComputeInstanceTemplate_guestAccelerator(i string) string {
func testAccComputeInstanceTemplate_guestAccelerator(i string, count int) string {
return fmt.Sprintf(`
resource "google_compute_instance_template" "foobar" {
name = "instance-test-%s"
Expand All @@ -1050,10 +1082,10 @@ resource "google_compute_instance_template" "foobar" {
}
guest_accelerator {
count = 1
count = %d
type = "nvidia-tesla-k80"
}
}`, i)
}`, i, count)
}

func testAccComputeInstanceTemplate_minCpuPlatform(i string) string {
Expand Down

0 comments on commit 8fba76f

Please sign in to comment.