@@ -149,7 +149,7 @@ func TestAccContainerCluster_misc(t *testing.T) {
149
149
ResourceName: "google_container_cluster.primary",
150
150
ImportState: true,
151
151
ImportStateVerify: true,
152
- ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection"},
152
+ ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels" },
153
153
},
154
154
{
155
155
Config: testAccContainerCluster_misc_update(clusterName, networkName, subnetworkName),
@@ -158,7 +158,7 @@ func TestAccContainerCluster_misc(t *testing.T) {
158
158
ResourceName: "google_container_cluster.primary",
159
159
ImportState: true,
160
160
ImportStateVerify: true,
161
- ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection"},
161
+ ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels" },
162
162
},
163
163
},
164
164
})
@@ -11080,3 +11080,158 @@ resource "google_container_cluster" "primary" {
11080
11080
}
11081
11081
`, secretID, clusterName, networkName, subnetworkName)
11082
11082
}
11083
+
11084
+ func TestAccContainerCluster_withProviderDefaultLabels(t *testing.T) {
11085
+ // The test failed if VCR testing is enabled, because the cached provider config is used.
11086
+ // With the cached provider config, any changes in the provider default labels will not be applied.
11087
+ acctest.SkipIfVcr(t)
11088
+ t.Parallel()
11089
+
11090
+ clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
11091
+ networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
11092
+ subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
11093
+
11094
+ acctest.VcrTest(t, resource.TestCase{
11095
+ PreCheck: func() { acctest.AccTestPreCheck(t) },
11096
+ ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
11097
+ CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
11098
+ Steps: []resource.TestStep{
11099
+ {
11100
+ Config: testAccContainerCluster_withProviderDefaultLabels(clusterName, networkName, subnetworkName),
11101
+ Check: resource.ComposeTestCheckFunc(
11102
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "1"),
11103
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.created-by", "terraform"),
11104
+
11105
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "2"),
11106
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "default_value1"),
11107
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.created-by", "terraform"),
11108
+
11109
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "2"),
11110
+ ),
11111
+ },
11112
+ {
11113
+ ResourceName: "google_container_cluster.primary",
11114
+ ImportState: true,
11115
+ ImportStateVerify: true,
11116
+ ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11117
+ },
11118
+ {
11119
+ Config: testAccContainerCluster_resourceLabelsOverridesProviderDefaultLabels(clusterName, networkName, subnetworkName),
11120
+ Check: resource.ComposeTestCheckFunc(
11121
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "2"),
11122
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.created-by", "terraform"),
11123
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "value1"),
11124
+
11125
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "2"),
11126
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "value1"),
11127
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.created-by", "terraform"),
11128
+
11129
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "2"),
11130
+ ),
11131
+ },
11132
+ {
11133
+ ResourceName: "google_container_cluster.primary",
11134
+ ImportState: true,
11135
+ ImportStateVerify: true,
11136
+ ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11137
+ },
11138
+ {
11139
+ Config: testAccContainerCluster_moveResourceLabelToProviderDefaultLabels(clusterName, networkName, subnetworkName),
11140
+ Check: resource.ComposeTestCheckFunc(
11141
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "0"),
11142
+
11143
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "2"),
11144
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "default_value1"),
11145
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.created-by", "terraform"),
11146
+
11147
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "2"),
11148
+ ),
11149
+ },
11150
+ {
11151
+ ResourceName: "google_container_cluster.primary",
11152
+ ImportState: true,
11153
+ ImportStateVerify: true,
11154
+ ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11155
+ },
11156
+ {
11157
+ Config: testAccContainerCluster_basic(clusterName, networkName, subnetworkName),
11158
+ Check: resource.ComposeTestCheckFunc(
11159
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "0"),
11160
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "0"),
11161
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "0"),
11162
+ ),
11163
+ },
11164
+ {
11165
+ ResourceName: "google_container_cluster.primary",
11166
+ ImportState: true,
11167
+ ImportStateVerify: true,
11168
+ ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11169
+ },
11170
+ },
11171
+ })
11172
+ }
11173
+
11174
+ func testAccContainerCluster_withProviderDefaultLabels(name, networkName, subnetworkName string) string {
11175
+ return fmt.Sprintf(`
11176
+ provider "google" {
11177
+ default_labels = {
11178
+ default_key1 = "default_value1"
11179
+ }
11180
+ }
11181
+
11182
+ resource "google_container_cluster" "primary" {
11183
+ name = "%s"
11184
+ location = "us-central1-a"
11185
+ initial_node_count = 1
11186
+ deletion_protection = false
11187
+ network = "%s"
11188
+ subnetwork = "%s"
11189
+ resource_labels = {
11190
+ created-by = "terraform"
11191
+ }
11192
+ }
11193
+ `, name, networkName, subnetworkName)
11194
+ }
11195
+
11196
+ func testAccContainerCluster_resourceLabelsOverridesProviderDefaultLabels(name, networkName, subnetworkName string) string {
11197
+ return fmt.Sprintf(`
11198
+ provider "google" {
11199
+ default_labels = {
11200
+ default_key1 = "default_value1"
11201
+ }
11202
+ }
11203
+
11204
+ resource "google_container_cluster" "primary" {
11205
+ name = "%s"
11206
+ location = "us-central1-a"
11207
+ initial_node_count = 1
11208
+ deletion_protection = false
11209
+ network = "%s"
11210
+ subnetwork = "%s"
11211
+ resource_labels = {
11212
+ created-by = "terraform"
11213
+ default_key1 = "value1"
11214
+ }
11215
+ }
11216
+ `, name, networkName, subnetworkName)
11217
+ }
11218
+
11219
+ func testAccContainerCluster_moveResourceLabelToProviderDefaultLabels(name, networkName, subnetworkName string) string {
11220
+ return fmt.Sprintf(`
11221
+ provider "google" {
11222
+ default_labels = {
11223
+ default_key1 = "default_value1"
11224
+ created-by = "terraform"
11225
+ }
11226
+ }
11227
+
11228
+ resource "google_container_cluster" "primary" {
11229
+ name = "%s"
11230
+ location = "us-central1-a"
11231
+ initial_node_count = 1
11232
+ deletion_protection = false
11233
+ network = "%s"
11234
+ subnetwork = "%s"
11235
+ }
11236
+ `, name, networkName, subnetworkName)
11237
+ }
0 commit comments