Skip to content

Commit 8fba76f

Browse files
committed
skip guest accelerators if count is 0.
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.
1 parent 450a2c9 commit 8fba76f

3 files changed

+42
-4
lines changed

google/resource_compute_instance.go

+3
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,9 @@ func expandInstanceGuestAccelerators(d TerraformResourceData, config *Config) ([
11981198
guestAccelerators := make([]*computeBeta.AcceleratorConfig, len(accels))
11991199
for i, raw := range accels {
12001200
data := raw.(map[string]interface{})
1201+
if data["count"].(int) == 0 {
1202+
continue
1203+
}
12011204
at, err := ParseAcceleratorFieldValue(data["type"].(string), d, config)
12021205
if err != nil {
12031206
return nil, fmt.Errorf("cannot parse accelerator type: %v", err)

google/resource_compute_instance_template.go

+3
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,9 @@ func expandInstanceTemplateGuestAccelerators(d TerraformResourceData, config *Co
506506
guestAccelerators := make([]*computeBeta.AcceleratorConfig, len(accels))
507507
for i, raw := range accels {
508508
data := raw.(map[string]interface{})
509+
if data["count"].(int) == 0 {
510+
continue
511+
}
509512
guestAccelerators[i] = &computeBeta.AcceleratorConfig{
510513
AcceleratorCount: int64(data["count"].(int)),
511514
// We can't use ParseAcceleratorFieldValue here because an instance

google/resource_compute_instance_template_test.go

+36-4
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func TestAccComputeInstanceTemplate_guestAccelerator(t *testing.T) {
301301
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
302302
Steps: []resource.TestStep{
303303
resource.TestStep{
304-
Config: testAccComputeInstanceTemplate_guestAccelerator(acctest.RandString(10)),
304+
Config: testAccComputeInstanceTemplate_guestAccelerator(acctest.RandString(10), 1),
305305
Check: resource.ComposeTestCheckFunc(
306306
testAccCheckComputeInstanceTemplateExists("google_compute_instance_template.foobar", &instanceTemplate),
307307
testAccCheckComputeInstanceTemplateHasGuestAccelerator(&instanceTemplate, "nvidia-tesla-k80", 1),
@@ -312,6 +312,28 @@ func TestAccComputeInstanceTemplate_guestAccelerator(t *testing.T) {
312312

313313
}
314314

315+
func TestAccComputeInstanceTemplate_guestAcceleratorSkip(t *testing.T) {
316+
t.Parallel()
317+
318+
var instanceTemplate compute.InstanceTemplate
319+
320+
resource.Test(t, resource.TestCase{
321+
PreCheck: func() { testAccPreCheck(t) },
322+
Providers: testAccProviders,
323+
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
324+
Steps: []resource.TestStep{
325+
resource.TestStep{
326+
Config: testAccComputeInstanceTemplate_guestAccelerator(acctest.RandString(10), 0),
327+
Check: resource.ComposeTestCheckFunc(
328+
testAccCheckComputeInstanceTemplateExists("google_compute_instance_template.foobar", &instanceTemplate),
329+
testAccCheckComputeInstanceTemplateLacksGuestAccelerator(&instanceTemplate),
330+
),
331+
},
332+
},
333+
})
334+
335+
}
336+
315337
func TestAccComputeInstanceTemplate_minCpuPlatform(t *testing.T) {
316338
t.Parallel()
317339

@@ -604,6 +626,16 @@ func testAccCheckComputeInstanceTemplateHasGuestAccelerator(instanceTemplate *co
604626
}
605627
}
606628

629+
func testAccCheckComputeInstanceTemplateLacksGuestAccelerator(instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc {
630+
return func(s *terraform.State) error {
631+
if len(instanceTemplate.Properties.GuestAccelerators) > 0 {
632+
return fmt.Errorf("Expected no guest accelerators")
633+
}
634+
635+
return nil
636+
}
637+
}
638+
607639
func testAccCheckComputeInstanceTemplateHasMinCpuPlatform(instanceTemplate *compute.InstanceTemplate, minCpuPlatform string) resource.TestCheckFunc {
608640
return func(s *terraform.State) error {
609641
if instanceTemplate.Properties.MinCpuPlatform != minCpuPlatform {
@@ -1027,7 +1059,7 @@ resource "google_compute_instance_template" "foobar" {
10271059
}`, i, i, i)
10281060
}
10291061

1030-
func testAccComputeInstanceTemplate_guestAccelerator(i string) string {
1062+
func testAccComputeInstanceTemplate_guestAccelerator(i string, count int) string {
10311063
return fmt.Sprintf(`
10321064
resource "google_compute_instance_template" "foobar" {
10331065
name = "instance-test-%s"
@@ -1050,10 +1082,10 @@ resource "google_compute_instance_template" "foobar" {
10501082
}
10511083
10521084
guest_accelerator {
1053-
count = 1
1085+
count = %d
10541086
type = "nvidia-tesla-k80"
10551087
}
1056-
}`, i)
1088+
}`, i, count)
10571089
}
10581090

10591091
func testAccComputeInstanceTemplate_minCpuPlatform(i string) string {

0 commit comments

Comments
 (0)