Skip to content

Commit 2cff006

Browse files
Container warnings (#15118) (#10725)
[upstream:211119cc09dfc4ac7a0e83baf674d5177e166fce] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent 2c921ae commit 2cff006

7 files changed

+276
-12
lines changed

.changelog/15118.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:deprecation
2+
compute: deprecated the option to deploy a container during VM creation using the container startup agent in `google_compute_instance`. Use alternative services to run containers on your VMs. Learn more at https://cloud.google.com/compute/docs/containers/migrate-containers.
3+
```

google-beta/services/compute/resource_compute_instance.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ func ComputeInstanceMinCpuPlatformEmptyOrAutomaticDiffSuppress(k, old, new strin
216216
return (old == "" && new == defaultVal) || (new == "" && old == defaultVal)
217217
}
218218

219+
func ValidateInstanceMetadata(i interface{}, k string) ([]string, []error) {
220+
metadata, ok := i.(map[string]interface{})
221+
if !ok {
222+
return nil, []error{fmt.Errorf("expected %q to be a map, got %T", k, i)}
223+
}
224+
var warnings []string
225+
if _, ok := metadata["gce-container-declaration"]; ok {
226+
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.")
227+
}
228+
return warnings, nil
229+
}
230+
219231
func ResourceComputeInstance() *schema.Resource {
220232
return &schema.Resource{
221233
Create: resourceComputeInstanceCreate,
@@ -995,10 +1007,11 @@ func ResourceComputeInstance() *schema.Resource {
9951007
},
9961008

9971009
"metadata": {
998-
Type: schema.TypeMap,
999-
Optional: true,
1000-
Elem: &schema.Schema{Type: schema.TypeString},
1001-
Description: `Metadata key/value pairs made available within the instance.`,
1010+
Type: schema.TypeMap,
1011+
Optional: true,
1012+
Elem: &schema.Schema{Type: schema.TypeString},
1013+
Description: `Metadata key/value pairs made available within the instance.`,
1014+
ValidateFunc: ValidateInstanceMetadata,
10021015
},
10031016

10041017
"partner_metadata": {

google-beta/services/compute/resource_compute_instance_template.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,10 +442,11 @@ Google Cloud KMS. Only one of kms_key_self_link, rsa_encrypted_key and raw_key m
442442
},
443443

444444
"metadata": {
445-
Type: schema.TypeMap,
446-
Optional: true,
447-
ForceNew: true,
448-
Description: `Metadata key/value pairs to make available from within instances created from this template.`,
445+
Type: schema.TypeMap,
446+
Optional: true,
447+
ForceNew: true,
448+
Description: `Metadata key/value pairs to make available from within instances created from this template.`,
449+
ValidateFunc: ValidateInstanceMetadata,
449450
},
450451

451452
"metadata_startup_script": {

google-beta/services/compute/resource_compute_instance_template_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,35 @@ func TestAccComputeInstanceTemplate_imageShorthand(t *testing.T) {
106106
})
107107
}
108108

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

@@ -2740,6 +2769,50 @@ resource "google_compute_instance_template" "foobar" {
27402769
`, context)
27412770
}
27422771

2772+
func testAccComputeInstanceTemplate_metadataGceContainerDeclaration(suffix string) string {
2773+
return fmt.Sprintf(`
2774+
data "google_compute_image" "my_image" {
2775+
family = "debian-11"
2776+
project = "debian-cloud"
2777+
}
2778+
2779+
resource "google_compute_instance_template" "foobar" {
2780+
name = "tf-test-instance-template-%s"
2781+
machine_type = "e2-medium"
2782+
can_ip_forward = false
2783+
tags = ["foo", "bar"]
2784+
2785+
disk {
2786+
source_image = data.google_compute_image.my_image.self_link
2787+
auto_delete = true
2788+
boot = true
2789+
}
2790+
2791+
network_interface {
2792+
network = "default"
2793+
}
2794+
2795+
scheduling {
2796+
preemptible = false
2797+
automatic_restart = true
2798+
}
2799+
2800+
metadata = {
2801+
foo = "bar"
2802+
gce-container-declaration = "spec:\n containers:\n - name: test\n image: gcr.io/google-containers/busybox\n"
2803+
}
2804+
2805+
service_account {
2806+
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
2807+
}
2808+
2809+
labels = {
2810+
my_label = "foobar"
2811+
}
2812+
}
2813+
`, suffix)
2814+
}
2815+
27432816
func testAccComputeInstanceTemplate_preemptible(suffix string) string {
27442817
return fmt.Sprintf(`
27452818
data "google_compute_image" "my_image" {

google-beta/services/compute/resource_compute_instance_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,48 @@ func TestDisksForceAttachDiffSuppress(t *testing.T) {
153153
}
154154
}
155155

156+
func TestValidateInstanceMetadata(t *testing.T) {
157+
cases := map[string]struct {
158+
Metadata map[string]interface{}
159+
ExpectWarning string
160+
}{
161+
"with gce-container-declaration": {
162+
Metadata: map[string]interface{}{
163+
"gce-container-declaration": "some-value",
164+
},
165+
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.",
166+
},
167+
"without gce-container-declaration": {
168+
Metadata: map[string]interface{}{
169+
"foo": "bar",
170+
},
171+
ExpectWarning: "",
172+
},
173+
"with empty metadata": {
174+
Metadata: map[string]interface{}{},
175+
ExpectWarning: "",
176+
},
177+
}
178+
179+
for tn, tc := range cases {
180+
warnings, errs := tpgcompute.ValidateInstanceMetadata(tc.Metadata, "metadata")
181+
if len(errs) > 0 {
182+
t.Errorf("%s: Unexpected errors: %v", tn, errs)
183+
}
184+
if tc.ExpectWarning == "" {
185+
if len(warnings) > 0 {
186+
t.Errorf("%s: Expected no warning, got: %v", tn, warnings)
187+
}
188+
} else {
189+
if len(warnings) == 0 {
190+
t.Errorf("%s: Expected warning %q, got none", tn, tc.ExpectWarning)
191+
} else if warnings[0] != tc.ExpectWarning {
192+
t.Errorf("%s: Expected warning %q, got %q", tn, tc.ExpectWarning, warnings[0])
193+
}
194+
}
195+
}
196+
}
197+
156198
func TestCheckForCommonAliasIp(t *testing.T) {
157199
type testCase struct {
158200
old, new []*compute.AliasIpRange
@@ -358,6 +400,31 @@ func TestAccComputeInstance_basic5(t *testing.T) {
358400
})
359401
}
360402

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

@@ -6046,6 +6113,38 @@ resource "google_compute_instance" "foobar" {
60466113
`, instance)
60476114
}
60486115

6116+
func testAccComputeInstance_metadataGceContainerDeclaration(instance string) string {
6117+
return fmt.Sprintf(`
6118+
data "google_compute_image" "my_image" {
6119+
family = "debian-11"
6120+
project = "debian-cloud"
6121+
}
6122+
6123+
resource "google_compute_instance" "foobar" {
6124+
name = "%s"
6125+
machine_type = "e2-medium"
6126+
zone = "us-central1-a"
6127+
tags = ["foo", "bar"]
6128+
desired_status = "RUNNING"
6129+
6130+
boot_disk {
6131+
initialize_params {
6132+
image = data.google_compute_image.my_image.self_link
6133+
}
6134+
}
6135+
6136+
network_interface {
6137+
network = "default"
6138+
}
6139+
6140+
metadata = {
6141+
foo = "bar"
6142+
gce-container-declaration = "spec:\n containers:\n - name: test\n image: gcr.io/google-containers/busybox\n"
6143+
}
6144+
}
6145+
`, instance)
6146+
}
6147+
60496148
func testAccComputeInstance_machineType(instance string, machineType string) string {
60506149
return fmt.Sprintf(`
60516150
data "google_compute_image" "my_image" {

google-beta/services/compute/resource_compute_region_instance_template.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,11 @@ Google Cloud KMS. Only one of kms_key_self_link, rsa_encrypted_key and raw_key m
416416
},
417417

418418
"metadata": {
419-
Type: schema.TypeMap,
420-
Optional: true,
421-
ForceNew: true,
422-
Description: `Metadata key/value pairs to make available from within instances created from this template.`,
419+
Type: schema.TypeMap,
420+
Optional: true,
421+
ForceNew: true,
422+
Description: `Metadata key/value pairs to make available from within instances created from this template.`,
423+
ValidateFunc: ValidateInstanceMetadata,
423424
},
424425

425426
"partner_metadata": {

google-beta/services/compute/resource_compute_region_instance_template_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,35 @@ func TestAccComputeRegionInstanceTemplate_imageShorthand(t *testing.T) {
103103
})
104104
}
105105

106+
func TestAccComputeRegionInstanceTemplate_metadataGceContainerDeclaration(t *testing.T) {
107+
t.Parallel()
108+
109+
var instanceTemplate compute.InstanceTemplate
110+
111+
acctest.VcrTest(t, resource.TestCase{
112+
PreCheck: func() { acctest.AccTestPreCheck(t) },
113+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
114+
CheckDestroy: testAccCheckComputeRegionInstanceTemplateDestroyProducer(t),
115+
Steps: []resource.TestStep{
116+
{
117+
Config: testAccComputeRegionInstanceTemplate_metadataGceContainerDeclaration(acctest.RandString(t, 10)),
118+
Check: resource.ComposeTestCheckFunc(
119+
testAccCheckComputeRegionInstanceTemplateExists(
120+
t, "google_compute_region_instance_template.foobar", &instanceTemplate),
121+
testAccCheckComputeRegionInstanceTemplateMetadata(&instanceTemplate, "foo", "bar"),
122+
testAccCheckComputeRegionInstanceTemplateMetadata(&instanceTemplate, "gce-container-declaration", "spec:\n containers:\n - name: test\n image: gcr.io/google-containers/busybox\n"),
123+
),
124+
},
125+
{
126+
ResourceName: "google_compute_region_instance_template.foobar",
127+
ImportState: true,
128+
ImportStateVerify: true,
129+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels", "metadata.foo", "metadata.gce-container-declaration"},
130+
},
131+
},
132+
})
133+
}
134+
106135
func TestAccComputeRegionInstanceTemplate_preemptible(t *testing.T) {
107136
t.Parallel()
108137

@@ -2291,6 +2320,51 @@ resource "google_compute_region_instance_template" "foobar" {
22912320
`, context)
22922321
}
22932322

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

0 commit comments

Comments
 (0)