Skip to content

Commit 6ea333c

Browse files
Apply new labels model to container cluster and edgenetwork resources (#11320) (#7932)
[upstream:75ce1f6be87e643cfb7f0db0c3a435d7b7b4a12e] Signed-off-by: Modular Magician <[email protected]>
1 parent 306a821 commit 6ea333c

12 files changed

+456
-49
lines changed

Diff for: .changelog/11320.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
```release-note:breaking-change
2+
container: three label-related fields are now in `google_container_cluster` resource. `resource_labels` field is non-authoritative and only manages the labels defined by the users on the resource through Terraform. The new output-only `terraform_labels` field merges the labels defined by the users on the resource through Terraform and the default labels configured on the provider. The new output-only `effective_labels` field lists all of labels present on the resource in GCP, including the labels configured through Terraform, the system, and other clients.
3+
```
4+
```release-note:breaking-change
5+
container: made three fields `resource_labels`, `terraform_labels`, and `effective_labels` be present in `google_container_cluster` datasources. All three fields will have all of labels present on the resource in GCP including the labels configured through Terraform, the system, and other clients, equivalent to `effective_labels` on the resource.
6+
```
7+
```release-note:breaking-change
8+
edgenetwork: three label-related fields are now in `google_edgenetwork_network ` and `google_edgenetwork_subnet` resources. `labels` field is non-authoritative and only manages the labels defined by the users on the resource through Terraform. The new output-only `terraform_labels` field merges the labels defined by the users on the resource through Terraform and the default labels configured on the provider. The new output-only `effective_labels` field lists all of labels present on the resource in GCP, including the labels configured through Terraform, the system, and other clients.
9+
```

Diff for: google-beta/services/container/data_source_google_container_cluster.go

+9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ func datasourceContainerClusterRead(d *schema.ResourceData, meta interface{}) er
4949
return err
5050
}
5151

52+
// Sets the "resource_labels" field and "terraform_labels" with the value of the field "effective_labels".
53+
effectiveLabels := d.Get("effective_labels")
54+
if err := d.Set("resource_labels", effectiveLabels); err != nil {
55+
return fmt.Errorf("Error setting labels in data source: %s", err)
56+
}
57+
if err := d.Set("terraform_labels", effectiveLabels); err != nil {
58+
return fmt.Errorf("Error setting terraform_labels in data source: %s", err)
59+
}
60+
5261
if d.Id() == "" {
5362
return fmt.Errorf("%s not found", id)
5463
}

Diff for: google-beta/services/container/data_source_google_container_cluster_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ resource "google_container_cluster" "kubes" {
9898
deletion_protection = false
9999
network = "%s"
100100
subnetwork = "%s"
101-
101+
resource_labels = {
102+
created-by = "terraform"
103+
}
102104
}
103105
104106
data "google_container_cluster" "kubes" {

Diff for: google-beta/services/container/resource_container_cluster.go

+29-7
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ func ResourceContainerCluster() *schema.Resource {
209209
containerClusterSurgeSettingsCustomizeDiff,
210210
containerClusterEnableK8sBetaApisCustomizeDiff,
211211
containerClusterNodeVersionCustomizeDiff,
212+
tpgresource.SetDiffForLabelsWithCustomizedName("resource_labels"),
212213
),
213214

214215
Timeouts: &schema.ResourceTimeout{
@@ -1779,10 +1780,25 @@ func ResourceContainerCluster() *schema.Resource {
17791780
},
17801781

17811782
"resource_labels": {
1783+
Type: schema.TypeMap,
1784+
Optional: true,
1785+
Elem: &schema.Schema{Type: schema.TypeString},
1786+
Description: `The GCE resource labels (a map of key/value pairs) to be applied to the cluster.
1787+
1788+
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
1789+
Please refer to the field 'effective_labels' for all of the labels present on the resource.`,
1790+
},
1791+
"terraform_labels": {
17821792
Type: schema.TypeMap,
1783-
Optional: true,
1793+
Computed: true,
1794+
Description: `The combination of labels configured directly on the resource and default labels configured on the provider.`,
1795+
Elem: &schema.Schema{Type: schema.TypeString},
1796+
},
1797+
"effective_labels": {
1798+
Type: schema.TypeMap,
1799+
Computed: true,
1800+
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
17841801
Elem: &schema.Schema{Type: schema.TypeString},
1785-
Description: `The GCE resource labels (a map of key/value pairs) to be applied to the cluster.`,
17861802
},
17871803

17881804
"label_fingerprint": {
@@ -2331,7 +2347,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
23312347
MasterAuth: expandMasterAuth(d.Get("master_auth")),
23322348
NotificationConfig: expandNotificationConfig(d.Get("notification_config")),
23332349
ConfidentialNodes: expandConfidentialNodes(d.Get("confidential_nodes")),
2334-
ResourceLabels: tpgresource.ExpandStringMap(d, "resource_labels"),
2350+
ResourceLabels: tpgresource.ExpandStringMap(d, "effective_labels"),
23352351
NodePoolAutoConfig: expandNodePoolAutoConfig(d.Get("node_pool_auto_config")),
23362352
ProtectConfig: expandProtectConfig(d.Get("protect_config")),
23372353
CostManagementConfig: expandCostManagementConfig(d.Get("cost_management_config")),
@@ -2939,8 +2955,14 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
29392955
return err
29402956
}
29412957

2942-
if err := d.Set("resource_labels", cluster.ResourceLabels); err != nil {
2943-
return fmt.Errorf("Error setting resource_labels: %s", err)
2958+
if err := tpgresource.SetLabels(cluster.ResourceLabels, d, "resource_labels"); err != nil {
2959+
return fmt.Errorf("Error setting labels: %s", err)
2960+
}
2961+
if err := tpgresource.SetLabels(cluster.ResourceLabels, d, "terraform_labels"); err != nil {
2962+
return fmt.Errorf("Error setting terraform_labels: %s", err)
2963+
}
2964+
if err := d.Set("effective_labels", cluster.ResourceLabels); err != nil {
2965+
return fmt.Errorf("Error setting effective_labels: %s", err)
29442966
}
29452967
if err := d.Set("label_fingerprint", cluster.LabelFingerprint); err != nil {
29462968
return fmt.Errorf("Error setting label_fingerprint: %s", err)
@@ -4022,8 +4044,8 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
40224044
log.Printf("[INFO] GKE cluster %s monitoring config has been updated", d.Id())
40234045
}
40244046

4025-
if d.HasChange("resource_labels") {
4026-
resourceLabels := d.Get("resource_labels").(map[string]interface{})
4047+
if d.HasChange("effective_labels") {
4048+
resourceLabels := d.Get("effective_labels").(map[string]interface{})
40274049
labelFingerprint := d.Get("label_fingerprint").(string)
40284050
req := &container.SetLabelsRequest{
40294051
ResourceLabels: tpgresource.ConvertStringMap(resourceLabels),

Diff for: google-beta/services/container/resource_container_cluster_test.go

+157-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func TestAccContainerCluster_misc(t *testing.T) {
150150
ResourceName: "google_container_cluster.primary",
151151
ImportState: true,
152152
ImportStateVerify: true,
153-
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection"},
153+
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
154154
},
155155
{
156156
Config: testAccContainerCluster_misc_update(clusterName, networkName, subnetworkName),
@@ -159,7 +159,7 @@ func TestAccContainerCluster_misc(t *testing.T) {
159159
ResourceName: "google_container_cluster.primary",
160160
ImportState: true,
161161
ImportStateVerify: true,
162-
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection"},
162+
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
163163
},
164164
},
165165
})
@@ -11007,3 +11007,158 @@ resource "google_container_cluster" "primary" {
1100711007
}
1100811008
`, secretID, clusterName, networkName, subnetworkName)
1100911009
}
11010+
11011+
func TestAccContainerCluster_withProviderDefaultLabels(t *testing.T) {
11012+
// The test failed if VCR testing is enabled, because the cached provider config is used.
11013+
// With the cached provider config, any changes in the provider default labels will not be applied.
11014+
acctest.SkipIfVcr(t)
11015+
t.Parallel()
11016+
11017+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
11018+
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
11019+
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
11020+
11021+
acctest.VcrTest(t, resource.TestCase{
11022+
PreCheck: func() { acctest.AccTestPreCheck(t) },
11023+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
11024+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
11025+
Steps: []resource.TestStep{
11026+
{
11027+
Config: testAccContainerCluster_withProviderDefaultLabels(clusterName, networkName, subnetworkName),
11028+
Check: resource.ComposeTestCheckFunc(
11029+
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "1"),
11030+
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.created-by", "terraform"),
11031+
11032+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "2"),
11033+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "default_value1"),
11034+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.created-by", "terraform"),
11035+
11036+
resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "2"),
11037+
),
11038+
},
11039+
{
11040+
ResourceName: "google_container_cluster.primary",
11041+
ImportState: true,
11042+
ImportStateVerify: true,
11043+
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11044+
},
11045+
{
11046+
Config: testAccContainerCluster_resourceLabelsOverridesProviderDefaultLabels(clusterName, networkName, subnetworkName),
11047+
Check: resource.ComposeTestCheckFunc(
11048+
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "2"),
11049+
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.created-by", "terraform"),
11050+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "value1"),
11051+
11052+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "2"),
11053+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "value1"),
11054+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.created-by", "terraform"),
11055+
11056+
resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "2"),
11057+
),
11058+
},
11059+
{
11060+
ResourceName: "google_container_cluster.primary",
11061+
ImportState: true,
11062+
ImportStateVerify: true,
11063+
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11064+
},
11065+
{
11066+
Config: testAccContainerCluster_moveResourceLabelToProviderDefaultLabels(clusterName, networkName, subnetworkName),
11067+
Check: resource.ComposeTestCheckFunc(
11068+
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "0"),
11069+
11070+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "2"),
11071+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "default_value1"),
11072+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.created-by", "terraform"),
11073+
11074+
resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "2"),
11075+
),
11076+
},
11077+
{
11078+
ResourceName: "google_container_cluster.primary",
11079+
ImportState: true,
11080+
ImportStateVerify: true,
11081+
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11082+
},
11083+
{
11084+
Config: testAccContainerCluster_basic(clusterName, networkName, subnetworkName),
11085+
Check: resource.ComposeTestCheckFunc(
11086+
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "0"),
11087+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "0"),
11088+
resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "0"),
11089+
),
11090+
},
11091+
{
11092+
ResourceName: "google_container_cluster.primary",
11093+
ImportState: true,
11094+
ImportStateVerify: true,
11095+
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11096+
},
11097+
},
11098+
})
11099+
}
11100+
11101+
func testAccContainerCluster_withProviderDefaultLabels(name, networkName, subnetworkName string) string {
11102+
return fmt.Sprintf(`
11103+
provider "google" {
11104+
default_labels = {
11105+
default_key1 = "default_value1"
11106+
}
11107+
}
11108+
11109+
resource "google_container_cluster" "primary" {
11110+
name = "%s"
11111+
location = "us-central1-a"
11112+
initial_node_count = 1
11113+
deletion_protection = false
11114+
network = "%s"
11115+
subnetwork = "%s"
11116+
resource_labels = {
11117+
created-by = "terraform"
11118+
}
11119+
}
11120+
`, name, networkName, subnetworkName)
11121+
}
11122+
11123+
func testAccContainerCluster_resourceLabelsOverridesProviderDefaultLabels(name, networkName, subnetworkName string) string {
11124+
return fmt.Sprintf(`
11125+
provider "google" {
11126+
default_labels = {
11127+
default_key1 = "default_value1"
11128+
}
11129+
}
11130+
11131+
resource "google_container_cluster" "primary" {
11132+
name = "%s"
11133+
location = "us-central1-a"
11134+
initial_node_count = 1
11135+
deletion_protection = false
11136+
network = "%s"
11137+
subnetwork = "%s"
11138+
resource_labels = {
11139+
created-by = "terraform"
11140+
default_key1 = "value1"
11141+
}
11142+
}
11143+
`, name, networkName, subnetworkName)
11144+
}
11145+
11146+
func testAccContainerCluster_moveResourceLabelToProviderDefaultLabels(name, networkName, subnetworkName string) string {
11147+
return fmt.Sprintf(`
11148+
provider "google" {
11149+
default_labels = {
11150+
default_key1 = "default_value1"
11151+
created-by = "terraform"
11152+
}
11153+
}
11154+
11155+
resource "google_container_cluster" "primary" {
11156+
name = "%s"
11157+
location = "us-central1-a"
11158+
initial_node_count = 1
11159+
deletion_protection = false
11160+
network = "%s"
11161+
subnetwork = "%s"
11162+
}
11163+
`, name, networkName, subnetworkName)
11164+
}

0 commit comments

Comments
 (0)