Skip to content

Commit e45a120

Browse files
Adding the namespace_labels field to the GKE Hub Scope resource (#9972) (#17421)
* initial commit for scope-level namespace labels * Add validation exceptions for the field of the GKEHub Scope and Namespace resources to be of type * Undoing unnecessary changes * Fixing the type of Scope namespace_labels field [upstream:3e13564ac5d3ac814949b399e0fd56058445a717] Signed-off-by: Modular Magician <[email protected]>
1 parent 8e717c4 commit e45a120

6 files changed

+100
-0
lines changed

.changelog/9972.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
gkehub2: added `namespace_labels` field to `google_gke_hub_scope` resource
3+
```

google/services/gkehub2/iam_gke_hub_scope_generated_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ func testAccGKEHub2ScopeIamMember_basicGenerated(context map[string]interface{})
130130
return acctest.Nprintf(`
131131
resource "google_gke_hub_scope" "scope" {
132132
scope_id = "tf-test-my-scope%{random_suffix}"
133+
namespace_labels = {
134+
keyb = "valueb"
135+
keya = "valuea"
136+
keyc = "valuec"
137+
}
133138
labels = {
134139
keyb = "valueb"
135140
keya = "valuea"
@@ -150,6 +155,11 @@ func testAccGKEHub2ScopeIamPolicy_basicGenerated(context map[string]interface{})
150155
return acctest.Nprintf(`
151156
resource "google_gke_hub_scope" "scope" {
152157
scope_id = "tf-test-my-scope%{random_suffix}"
158+
namespace_labels = {
159+
keyb = "valueb"
160+
keya = "valuea"
161+
keyc = "valuec"
162+
}
153163
labels = {
154164
keyb = "valueb"
155165
keya = "valuea"
@@ -184,6 +194,11 @@ func testAccGKEHub2ScopeIamPolicy_emptyBinding(context map[string]interface{}) s
184194
return acctest.Nprintf(`
185195
resource "google_gke_hub_scope" "scope" {
186196
scope_id = "tf-test-my-scope%{random_suffix}"
197+
namespace_labels = {
198+
keyb = "valueb"
199+
keya = "valuea"
200+
keyc = "valuec"
201+
}
187202
labels = {
188203
keyb = "valueb"
189204
keya = "valuea"
@@ -206,6 +221,11 @@ func testAccGKEHub2ScopeIamBinding_basicGenerated(context map[string]interface{}
206221
return acctest.Nprintf(`
207222
resource "google_gke_hub_scope" "scope" {
208223
scope_id = "tf-test-my-scope%{random_suffix}"
224+
namespace_labels = {
225+
keyb = "valueb"
226+
keya = "valuea"
227+
keyc = "valuec"
228+
}
209229
labels = {
210230
keyb = "valueb"
211231
keya = "valuea"
@@ -226,6 +246,11 @@ func testAccGKEHub2ScopeIamBinding_updateGenerated(context map[string]interface{
226246
return acctest.Nprintf(`
227247
resource "google_gke_hub_scope" "scope" {
228248
scope_id = "tf-test-my-scope%{random_suffix}"
249+
namespace_labels = {
250+
keyb = "valueb"
251+
keya = "valuea"
252+
keyc = "valuec"
253+
}
229254
labels = {
230255
keyb = "valueb"
231256
keya = "valuea"

google/services/gkehub2/resource_gke_hub_scope.go

+44
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ func ResourceGKEHub2Scope() *schema.Resource {
7070
Please refer to the field 'effective_labels' for all of the labels present on the resource.`,
7171
Elem: &schema.Schema{Type: schema.TypeString},
7272
},
73+
"namespace_labels": {
74+
Type: schema.TypeMap,
75+
Optional: true,
76+
Description: `Scope-level cluster namespace labels. For the member clusters bound
77+
to the Scope, these labels are applied to each namespace under the
78+
Scope. Scope-level labels take precedence over Namespace-level
79+
labels ('namespace_labels' in the Fleet Namespace resource) if they
80+
share a key. Keys and values must be Kubernetes-conformant.`,
81+
Elem: &schema.Schema{Type: schema.TypeString},
82+
},
7383
"create_time": {
7484
Type: schema.TypeString,
7585
Computed: true,
@@ -141,6 +151,12 @@ func resourceGKEHub2ScopeCreate(d *schema.ResourceData, meta interface{}) error
141151
}
142152

143153
obj := make(map[string]interface{})
154+
namespaceLabelsProp, err := expandGKEHub2ScopeNamespaceLabels(d.Get("namespace_labels"), d, config)
155+
if err != nil {
156+
return err
157+
} else if v, ok := d.GetOkExists("namespace_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(namespaceLabelsProp)) && (ok || !reflect.DeepEqual(v, namespaceLabelsProp)) {
158+
obj["namespaceLabels"] = namespaceLabelsProp
159+
}
144160
labelsProp, err := expandGKEHub2ScopeEffectiveLabels(d.Get("effective_labels"), d, config)
145161
if err != nil {
146162
return err
@@ -274,6 +290,9 @@ func resourceGKEHub2ScopeRead(d *schema.ResourceData, meta interface{}) error {
274290
if err := d.Set("state", flattenGKEHub2ScopeState(res["state"], d, config)); err != nil {
275291
return fmt.Errorf("Error reading Scope: %s", err)
276292
}
293+
if err := d.Set("namespace_labels", flattenGKEHub2ScopeNamespaceLabels(res["namespaceLabels"], d, config)); err != nil {
294+
return fmt.Errorf("Error reading Scope: %s", err)
295+
}
277296
if err := d.Set("labels", flattenGKEHub2ScopeLabels(res["labels"], d, config)); err != nil {
278297
return fmt.Errorf("Error reading Scope: %s", err)
279298
}
@@ -303,6 +322,12 @@ func resourceGKEHub2ScopeUpdate(d *schema.ResourceData, meta interface{}) error
303322
billingProject = project
304323

305324
obj := make(map[string]interface{})
325+
namespaceLabelsProp, err := expandGKEHub2ScopeNamespaceLabels(d.Get("namespace_labels"), d, config)
326+
if err != nil {
327+
return err
328+
} else if v, ok := d.GetOkExists("namespace_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, namespaceLabelsProp)) {
329+
obj["namespaceLabels"] = namespaceLabelsProp
330+
}
306331
labelsProp, err := expandGKEHub2ScopeEffectiveLabels(d.Get("effective_labels"), d, config)
307332
if err != nil {
308333
return err
@@ -318,6 +343,10 @@ func resourceGKEHub2ScopeUpdate(d *schema.ResourceData, meta interface{}) error
318343
log.Printf("[DEBUG] Updating Scope %q: %#v", d.Id(), obj)
319344
updateMask := []string{}
320345

346+
if d.HasChange("namespace_labels") {
347+
updateMask = append(updateMask, "namespaceLabels")
348+
}
349+
321350
if d.HasChange("effective_labels") {
322351
updateMask = append(updateMask, "labels")
323352
}
@@ -473,6 +502,10 @@ func flattenGKEHub2ScopeStateCode(v interface{}, d *schema.ResourceData, config
473502
return v
474503
}
475504

505+
func flattenGKEHub2ScopeNamespaceLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
506+
return v
507+
}
508+
476509
func flattenGKEHub2ScopeLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
477510
if v == nil {
478511
return v
@@ -507,6 +540,17 @@ func flattenGKEHub2ScopeEffectiveLabels(v interface{}, d *schema.ResourceData, c
507540
return v
508541
}
509542

543+
func expandGKEHub2ScopeNamespaceLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
544+
if v == nil {
545+
return map[string]string{}, nil
546+
}
547+
m := make(map[string]string)
548+
for k, val := range v.(map[string]interface{}) {
549+
m[k] = val.(string)
550+
}
551+
return m, nil
552+
}
553+
510554
func expandGKEHub2ScopeEffectiveLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
511555
if v == nil {
512556
return map[string]string{}, nil

google/services/gkehub2/resource_gke_hub_scope_generated_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ func testAccGKEHub2Scope_gkehubScopeBasicExample(context map[string]interface{})
6161
return acctest.Nprintf(`
6262
resource "google_gke_hub_scope" "scope" {
6363
scope_id = "tf-test-my-scope%{random_suffix}"
64+
namespace_labels = {
65+
keyb = "valueb"
66+
keya = "valuea"
67+
keyc = "valuec"
68+
}
6469
labels = {
6570
keyb = "valueb"
6671
keya = "valuea"

google/services/gkehub2/resource_gke_hub_scope_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ func testAccGKEHub2Scope_gkehubScopeBasicExample_basic(context map[string]interf
4949
return acctest.Nprintf(`
5050
resource "google_gke_hub_scope" "scope" {
5151
scope_id = "tf-test-scope%{random_suffix}"
52+
namespace_labels = {
53+
keyb = "valueb"
54+
keya = "valuea"
55+
keyc = "valuec"
56+
}
5257
labels = {
5358
keyb = "valueb"
5459
keya = "valuea"
@@ -62,6 +67,11 @@ func testAccGKEHub2Scope_gkehubScopeBasicExample_update(context map[string]inter
6267
return acctest.Nprintf(`
6368
resource "google_gke_hub_scope" "scope" {
6469
scope_id = "tf-test-scope%{random_suffix}"
70+
namespace_labels = {
71+
updated_keyb = "updated_valueb"
72+
updated_keya = "updated_valuea"
73+
updated_keyc = "updated_valuec"
74+
}
6575
labels = {
6676
updated_keyb = "updated_valueb"
6777
updated_keya = "updated_valuea"

website/docs/r/gke_hub_scope.html.markdown

+13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ To get more information about Scope, see:
3434
```hcl
3535
resource "google_gke_hub_scope" "scope" {
3636
scope_id = "my-scope"
37+
namespace_labels = {
38+
keyb = "valueb"
39+
keya = "valuea"
40+
keyc = "valuec"
41+
}
3742
labels = {
3843
keyb = "valueb"
3944
keya = "valuea"
@@ -55,6 +60,14 @@ The following arguments are supported:
5560
- - -
5661

5762

63+
* `namespace_labels` -
64+
(Optional)
65+
Scope-level cluster namespace labels. For the member clusters bound
66+
to the Scope, these labels are applied to each namespace under the
67+
Scope. Scope-level labels take precedence over Namespace-level
68+
labels (`namespace_labels` in the Fleet Namespace resource) if they
69+
share a key. Keys and values must be Kubernetes-conformant.
70+
5871
* `labels` -
5972
(Optional)
6073
Labels for this Scope.

0 commit comments

Comments
 (0)