Skip to content

Commit 211119c

Browse files
authored
Container warnings (#15118)
1 parent 137e0b9 commit 211119c

6 files changed

+261
-0
lines changed

mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,18 @@ func ComputeInstanceMinCpuPlatformEmptyOrAutomaticDiffSuppress(k, old, new strin
206206
return (old == "" && new == defaultVal) || (new == "" && old == defaultVal)
207207
}
208208

209+
func ValidateInstanceMetadata(i interface{}, k string) ([]string, []error) {
210+
metadata, ok := i.(map[string]interface{})
211+
if !ok {
212+
return nil, []error{fmt.Errorf("expected %q to be a map, got %T", k, i)}
213+
}
214+
var warnings []string
215+
if _, ok := metadata["gce-container-declaration"]; ok {
216+
warnings = append(warnings, "The option to deploy a container during VM creation using the container startup agent is deprecated. Use alternative services to run containers on your VMs. Learn more at https://cloud.google.com/compute/docs/containers/migrate-containers.")
217+
}
218+
return warnings, nil
219+
}
220+
209221
func ResourceComputeInstance() *schema.Resource {
210222
return &schema.Resource{
211223
Create: resourceComputeInstanceCreate,
@@ -995,6 +1007,7 @@ func ResourceComputeInstance() *schema.Resource {
9951007
Optional: true,
9961008
Elem: &schema.Schema{Type: schema.TypeString},
9971009
Description: `Metadata key/value pairs made available within the instance.`,
1010+
ValidateFunc: ValidateInstanceMetadata,
9981011
},
9991012

10001013
{{ if ne $.TargetVersionName `ga` -}}

mmv1/third_party/terraform/services/compute/resource_compute_instance_template.go.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ Google Cloud KMS. Only one of kms_key_self_link, rsa_encrypted_key and raw_key m
439439
Optional: true,
440440
ForceNew: true,
441441
Description: `Metadata key/value pairs to make available from within instances created from this template.`,
442+
ValidateFunc: ValidateInstanceMetadata,
442443
},
443444

444445
"metadata_startup_script": {

mmv1/third_party/terraform/services/compute/resource_compute_instance_template_test.go.tmpl

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,35 @@ func TestAccComputeInstanceTemplate_imageShorthand(t *testing.T) {
9999
})
100100
}
101101

102+
func TestAccComputeInstanceTemplate_metadataGceContainerDeclaration(t *testing.T) {
103+
t.Parallel()
104+
105+
var instanceTemplate compute.InstanceTemplate
106+
107+
acctest.VcrTest(t, resource.TestCase{
108+
PreCheck: func() { acctest.AccTestPreCheck(t) },
109+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
110+
CheckDestroy: testAccCheckComputeInstanceTemplateDestroyProducer(t),
111+
Steps: []resource.TestStep{
112+
{
113+
Config: testAccComputeInstanceTemplate_metadataGceContainerDeclaration(acctest.RandString(t, 10)),
114+
Check: resource.ComposeTestCheckFunc(
115+
testAccCheckComputeInstanceTemplateExists(
116+
t, "google_compute_instance_template.foobar", &instanceTemplate),
117+
testAccCheckComputeInstanceTemplateMetadata(&instanceTemplate, "foo", "bar"),
118+
testAccCheckComputeInstanceTemplateMetadata(&instanceTemplate, "gce-container-declaration", "spec:\n containers:\n - name: test\n image: gcr.io/google-containers/busybox\n"),
119+
),
120+
},
121+
{
122+
ResourceName: "google_compute_instance_template.foobar",
123+
ImportState: true,
124+
ImportStateVerify: true,
125+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels", "metadata.foo", "metadata.gce-container-declaration"},
126+
},
127+
},
128+
})
129+
}
130+
102131
func TestAccComputeInstanceTemplate_preemptible(t *testing.T) {
103132
t.Parallel()
104133

@@ -2759,6 +2788,50 @@ resource "google_compute_instance_template" "foobar" {
27592788
`, context)
27602789
}
27612790

2791+
func testAccComputeInstanceTemplate_metadataGceContainerDeclaration(suffix string) string {
2792+
return fmt.Sprintf(`
2793+
data "google_compute_image" "my_image" {
2794+
family = "debian-11"
2795+
project = "debian-cloud"
2796+
}
2797+
2798+
resource "google_compute_instance_template" "foobar" {
2799+
name = "tf-test-instance-template-%s"
2800+
machine_type = "e2-medium"
2801+
can_ip_forward = false
2802+
tags = ["foo", "bar"]
2803+
2804+
disk {
2805+
source_image = data.google_compute_image.my_image.self_link
2806+
auto_delete = true
2807+
boot = true
2808+
}
2809+
2810+
network_interface {
2811+
network = "default"
2812+
}
2813+
2814+
scheduling {
2815+
preemptible = false
2816+
automatic_restart = true
2817+
}
2818+
2819+
metadata = {
2820+
foo = "bar"
2821+
gce-container-declaration = "spec:\n containers:\n - name: test\n image: gcr.io/google-containers/busybox\n"
2822+
}
2823+
2824+
service_account {
2825+
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
2826+
}
2827+
2828+
labels = {
2829+
my_label = "foobar"
2830+
}
2831+
}
2832+
`, suffix)
2833+
}
2834+
27622835
func testAccComputeInstanceTemplate_preemptible(suffix string) string {
27632836
return fmt.Sprintf(`
27642837
data "google_compute_image" "my_image" {

mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.tmpl

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,48 @@ func TestDisksForceAttachDiffSuppress(t *testing.T) {
145145
}
146146
}
147147

148+
func TestValidateInstanceMetadata(t *testing.T) {
149+
cases := map[string]struct {
150+
Metadata map[string]interface{}
151+
ExpectWarning string
152+
}{
153+
"with gce-container-declaration": {
154+
Metadata: map[string]interface{}{
155+
"gce-container-declaration": "some-value",
156+
},
157+
ExpectWarning: "The option to deploy a container during VM creation using the container startup agent is deprecated. Use alternative services to run containers on your VMs. Learn more at https://cloud.google.com/compute/docs/containers/migrate-containers.",
158+
},
159+
"without gce-container-declaration": {
160+
Metadata: map[string]interface{}{
161+
"foo": "bar",
162+
},
163+
ExpectWarning: "",
164+
},
165+
"with empty metadata": {
166+
Metadata: map[string]interface{}{},
167+
ExpectWarning: "",
168+
},
169+
}
170+
171+
for tn, tc := range cases {
172+
warnings, errs := tpgcompute.ValidateInstanceMetadata(tc.Metadata, "metadata")
173+
if len(errs) > 0 {
174+
t.Errorf("%s: Unexpected errors: %v", tn, errs)
175+
}
176+
if tc.ExpectWarning == "" {
177+
if len(warnings) > 0 {
178+
t.Errorf("%s: Expected no warning, got: %v", tn, warnings)
179+
}
180+
} else {
181+
if len(warnings) == 0 {
182+
t.Errorf("%s: Expected warning %q, got none", tn, tc.ExpectWarning)
183+
} else if warnings[0] != tc.ExpectWarning {
184+
t.Errorf("%s: Expected warning %q, got %q", tn, tc.ExpectWarning, warnings[0])
185+
}
186+
}
187+
}
188+
}
189+
148190
func TestCheckForCommonAliasIp(t *testing.T) {
149191
type testCase struct {
150192
old, new []*compute.AliasIpRange
@@ -350,6 +392,31 @@ func TestAccComputeInstance_basic5(t *testing.T) {
350392
})
351393
}
352394

395+
func TestAccComputeInstance_metadataGceContainerDeclaration(t *testing.T) {
396+
t.Parallel()
397+
398+
var instance compute.Instance
399+
var instanceName = fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
400+
401+
acctest.VcrTest(t, resource.TestCase{
402+
PreCheck: func() { acctest.AccTestPreCheck(t) },
403+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
404+
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
405+
Steps: []resource.TestStep{
406+
{
407+
Config: testAccComputeInstance_metadataGceContainerDeclaration(instanceName),
408+
Check: resource.ComposeTestCheckFunc(
409+
testAccCheckComputeInstanceExists(
410+
t, "google_compute_instance.foobar", &instance),
411+
testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
412+
testAccCheckComputeInstanceMetadata(&instance, "gce-container-declaration", "spec:\n containers:\n - name: test\n image: gcr.io/google-containers/busybox\n"),
413+
),
414+
},
415+
computeInstanceImportStep("us-central1-a", instanceName, []string{"metadata.foo", "metadata.gce-container-declaration", "desired_status"}),
416+
},
417+
})
418+
}
419+
353420
func TestAccComputeInstance_resourceManagerTags(t *testing.T) {
354421
t.Parallel()
355422

@@ -6062,6 +6129,38 @@ resource "google_compute_instance" "foobar" {
60626129
`, instance)
60636130
}
60646131

6132+
func testAccComputeInstance_metadataGceContainerDeclaration(instance string) string {
6133+
return fmt.Sprintf(`
6134+
data "google_compute_image" "my_image" {
6135+
family = "debian-11"
6136+
project = "debian-cloud"
6137+
}
6138+
6139+
resource "google_compute_instance" "foobar" {
6140+
name = "%s"
6141+
machine_type = "e2-medium"
6142+
zone = "us-central1-a"
6143+
tags = ["foo", "bar"]
6144+
desired_status = "RUNNING"
6145+
6146+
boot_disk {
6147+
initialize_params {
6148+
image = data.google_compute_image.my_image.self_link
6149+
}
6150+
}
6151+
6152+
network_interface {
6153+
network = "default"
6154+
}
6155+
6156+
metadata = {
6157+
foo = "bar"
6158+
gce-container-declaration = "spec:\n containers:\n - name: test\n image: gcr.io/google-containers/busybox\n"
6159+
}
6160+
}
6161+
`, instance)
6162+
}
6163+
60656164
func testAccComputeInstance_machineType(instance string, machineType string) string {
60666165
return fmt.Sprintf(`
60676166
data "google_compute_image" "my_image" {

mmv1/third_party/terraform/services/compute/resource_compute_region_instance_template.go.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ Google Cloud KMS. Only one of kms_key_self_link, rsa_encrypted_key and raw_key m
411411
Optional: true,
412412
ForceNew: true,
413413
Description: `Metadata key/value pairs to make available from within instances created from this template.`,
414+
ValidateFunc: ValidateInstanceMetadata,
414415
},
415416

416417
{{ if ne $.TargetVersionName `ga` -}}

mmv1/third_party/terraform/services/compute/resource_compute_region_instance_template_test.go.tmpl

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,35 @@ func TestAccComputeRegionInstanceTemplate_imageShorthand(t *testing.T) {
9494
})
9595
}
9696

97+
func TestAccComputeRegionInstanceTemplate_metadataGceContainerDeclaration(t *testing.T) {
98+
t.Parallel()
99+
100+
var instanceTemplate compute.InstanceTemplate
101+
102+
acctest.VcrTest(t, resource.TestCase{
103+
PreCheck: func() { acctest.AccTestPreCheck(t) },
104+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
105+
CheckDestroy: testAccCheckComputeRegionInstanceTemplateDestroyProducer(t),
106+
Steps: []resource.TestStep{
107+
{
108+
Config: testAccComputeRegionInstanceTemplate_metadataGceContainerDeclaration(acctest.RandString(t, 10)),
109+
Check: resource.ComposeTestCheckFunc(
110+
testAccCheckComputeRegionInstanceTemplateExists(
111+
t, "google_compute_region_instance_template.foobar", &instanceTemplate),
112+
testAccCheckComputeRegionInstanceTemplateMetadata(&instanceTemplate, "foo", "bar"),
113+
testAccCheckComputeRegionInstanceTemplateMetadata(&instanceTemplate, "gce-container-declaration", "spec:\n containers:\n - name: test\n image: gcr.io/google-containers/busybox\n"),
114+
),
115+
},
116+
{
117+
ResourceName: "google_compute_region_instance_template.foobar",
118+
ImportState: true,
119+
ImportStateVerify: true,
120+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels", "metadata.foo", "metadata.gce-container-declaration"},
121+
},
122+
},
123+
})
124+
}
125+
97126
func TestAccComputeRegionInstanceTemplate_preemptible(t *testing.T) {
98127
t.Parallel()
99128

@@ -2299,6 +2328,51 @@ resource "google_compute_region_instance_template" "foobar" {
22992328
`, context)
23002329
}
23012330

2331+
func testAccComputeRegionInstanceTemplate_metadataGceContainerDeclaration(suffix string) string {
2332+
return fmt.Sprintf(`
2333+
data "google_compute_image" "my_image" {
2334+
family = "debian-11"
2335+
project = "debian-cloud"
2336+
}
2337+
2338+
resource "google_compute_region_instance_template" "foobar" {
2339+
name = "tf-test-instance-template-%s"
2340+
region = "us-central1"
2341+
machine_type = "e2-medium"
2342+
can_ip_forward = false
2343+
tags = ["foo", "bar"]
2344+
2345+
disk {
2346+
source_image = data.google_compute_image.my_image.self_link
2347+
auto_delete = true
2348+
boot = true
2349+
}
2350+
2351+
network_interface {
2352+
network = "default"
2353+
}
2354+
2355+
scheduling {
2356+
preemptible = false
2357+
automatic_restart = true
2358+
}
2359+
2360+
metadata = {
2361+
foo = "bar"
2362+
gce-container-declaration = "spec:\n containers:\n - name: test\n image: gcr.io/google-containers/busybox\n"
2363+
}
2364+
2365+
service_account {
2366+
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
2367+
}
2368+
2369+
labels = {
2370+
my_label = "foobar"
2371+
}
2372+
}
2373+
`, suffix)
2374+
}
2375+
23022376
func testAccComputeRegionInstanceTemplate_preemptible(suffix string) string {
23032377
return fmt.Sprintf(`
23042378
data "google_compute_image" "my_image" {

0 commit comments

Comments
 (0)