Skip to content

Commit 9d4a2a7

Browse files
Made google_container_cluster.user_managed_keys_config not settable and fixed diff due to server-set values (#12309)
[upstream:9a45aa9a44634ac33a3cd7a1024be660accd7647] Signed-off-by: Modular Magician <[email protected]>
1 parent c7adbd3 commit 9d4a2a7

File tree

3 files changed

+178
-14
lines changed

3 files changed

+178
-14
lines changed

.changelog/12309.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```release-note:bug
2+
container: fixed diff on `google_container_cluster.user_managed_keys_config` field for resources that had not set it. (patch release)
3+
```
4+
```release-note:bug
5+
container: marked `google_container_cluster.user_managed_keys_config` as immutable because it can't be updated in place. (patch release)
6+
```

google-beta/services/container/resource_container_cluster.go

+12-14
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,7 @@ func ResourceContainerCluster() *schema.Resource {
22222222
"user_managed_keys_config": {
22232223
Type: schema.TypeList,
22242224
Optional: true,
2225+
ForceNew: true,
22252226
MaxItems: 1,
22262227
Description: `The custom keys configuration of the cluster.`,
22272228
Elem: &schema.Resource{
@@ -4240,20 +4241,6 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
42404241
log.Printf("[INFO] GKE cluster %s fleet config has been updated", d.Id())
42414242
}
42424243

4243-
if d.HasChange("user_managed_keys_config") {
4244-
req := &container.UpdateClusterRequest{
4245-
Update: &container.ClusterUpdate{
4246-
UserManagedKeysConfig: expandUserManagedKeysConfig(d.Get("user_managed_keys_config")),
4247-
},
4248-
}
4249-
updateF := updateFunc(req, "updating GKE cluster user managed keys config.")
4250-
if err := transport_tpg.LockedCall(lockKey, updateF); err != nil {
4251-
return err
4252-
}
4253-
4254-
log.Printf("[INFO] GKE cluster %s user managed key config has been updated to %#v", d.Id(), req.Update.UserManagedKeysConfig)
4255-
}
4256-
42574244
if d.HasChange("enable_k8s_beta_apis") {
42584245
log.Print("[INFO] Enable Kubernetes Beta APIs")
42594246
if v, ok := d.GetOk("enable_k8s_beta_apis"); ok {
@@ -6608,11 +6595,22 @@ func flattenUserManagedKeysConfig(c *container.UserManagedKeysConfig) []map[stri
66086595
"control_plane_disk_encryption_key": c.ControlPlaneDiskEncryptionKey,
66096596
"gkeops_etcd_backup_encryption_key": c.GkeopsEtcdBackupEncryptionKey,
66106597
}
6598+
allEmpty := true
6599+
for _, v := range f {
6600+
if v.(string) != "" {
6601+
allEmpty = false
6602+
}
6603+
}
66116604
if len(c.ServiceAccountSigningKeys) != 0 {
66126605
f["service_account_signing_keys"] = schema.NewSet(schema.HashString, tpgresource.ConvertStringArrToInterface(c.ServiceAccountSigningKeys))
6606+
allEmpty = false
66136607
}
66146608
if len(c.ServiceAccountVerificationKeys) != 0 {
66156609
f["service_account_verification_keys"] = schema.NewSet(schema.HashString, tpgresource.ConvertStringArrToInterface(c.ServiceAccountVerificationKeys))
6610+
allEmpty = false
6611+
}
6612+
if allEmpty {
6613+
return nil
66166614
}
66176615
return []map[string]interface{}{f}
66186616
}

google-beta/services/container/resource_container_cluster_internal_test.go

+160
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ package container
55
import (
66
"testing"
77

8+
"github.com/google/go-cmp/cmp"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
11+
1012
container "google.golang.org/api/container/v1beta1"
1113
)
1214

@@ -290,3 +292,161 @@ func TestContainerCluster_NodeVersionCustomizeDiff(t *testing.T) {
290292
}
291293
}
292294
}
295+
296+
func TestContainerCluster_flattenUserManagedKeysConfig(t *testing.T) {
297+
t.Parallel()
298+
299+
cases := []struct {
300+
name string
301+
config *container.UserManagedKeysConfig
302+
want []map[string]interface{}
303+
}{
304+
{
305+
name: "nil",
306+
},
307+
{
308+
name: "empty",
309+
config: &container.UserManagedKeysConfig{},
310+
},
311+
{
312+
name: "cluster_ca",
313+
config: &container.UserManagedKeysConfig{
314+
ClusterCa: "value",
315+
},
316+
want: []map[string]interface{}{
317+
{
318+
"cluster_ca": "value",
319+
"etcd_api_ca": "",
320+
"etcd_peer_ca": "",
321+
"aggregation_ca": "",
322+
"control_plane_disk_encryption_key": "",
323+
"gkeops_etcd_backup_encryption_key": "",
324+
},
325+
},
326+
},
327+
{
328+
name: "etcd_api_ca",
329+
config: &container.UserManagedKeysConfig{
330+
EtcdApiCa: "value",
331+
},
332+
want: []map[string]interface{}{
333+
{
334+
"cluster_ca": "",
335+
"etcd_api_ca": "value",
336+
"etcd_peer_ca": "",
337+
"aggregation_ca": "",
338+
"control_plane_disk_encryption_key": "",
339+
"gkeops_etcd_backup_encryption_key": "",
340+
},
341+
},
342+
},
343+
{
344+
name: "etcd_peer_ca",
345+
config: &container.UserManagedKeysConfig{
346+
EtcdPeerCa: "value",
347+
},
348+
want: []map[string]interface{}{
349+
{
350+
"cluster_ca": "",
351+
"etcd_api_ca": "",
352+
"etcd_peer_ca": "value",
353+
"aggregation_ca": "",
354+
"control_plane_disk_encryption_key": "",
355+
"gkeops_etcd_backup_encryption_key": "",
356+
},
357+
},
358+
},
359+
{
360+
name: "aggregation_ca",
361+
config: &container.UserManagedKeysConfig{
362+
AggregationCa: "value",
363+
},
364+
want: []map[string]interface{}{
365+
{
366+
"cluster_ca": "",
367+
"etcd_api_ca": "",
368+
"etcd_peer_ca": "",
369+
"aggregation_ca": "value",
370+
"control_plane_disk_encryption_key": "",
371+
"gkeops_etcd_backup_encryption_key": "",
372+
},
373+
},
374+
},
375+
{
376+
name: "control_plane_disk_encryption_key",
377+
config: &container.UserManagedKeysConfig{
378+
ControlPlaneDiskEncryptionKey: "value",
379+
},
380+
want: []map[string]interface{}{
381+
{
382+
"cluster_ca": "",
383+
"etcd_api_ca": "",
384+
"etcd_peer_ca": "",
385+
"aggregation_ca": "",
386+
"control_plane_disk_encryption_key": "value",
387+
"gkeops_etcd_backup_encryption_key": "",
388+
},
389+
},
390+
},
391+
{
392+
name: "gkeops_etcd_backup_encryption_key",
393+
config: &container.UserManagedKeysConfig{
394+
GkeopsEtcdBackupEncryptionKey: "value",
395+
},
396+
want: []map[string]interface{}{
397+
{
398+
"cluster_ca": "",
399+
"etcd_api_ca": "",
400+
"etcd_peer_ca": "",
401+
"aggregation_ca": "",
402+
"control_plane_disk_encryption_key": "",
403+
"gkeops_etcd_backup_encryption_key": "value",
404+
},
405+
},
406+
},
407+
{
408+
name: "service_account_signing_keys",
409+
config: &container.UserManagedKeysConfig{
410+
ServiceAccountSigningKeys: []string{"value"},
411+
},
412+
want: []map[string]interface{}{
413+
{
414+
"cluster_ca": "",
415+
"etcd_api_ca": "",
416+
"etcd_peer_ca": "",
417+
"aggregation_ca": "",
418+
"control_plane_disk_encryption_key": "",
419+
"gkeops_etcd_backup_encryption_key": "",
420+
"service_account_signing_keys": schema.NewSet(schema.HashString, []interface{}{"value"}),
421+
},
422+
},
423+
},
424+
{
425+
name: "service_account_verification_keys",
426+
config: &container.UserManagedKeysConfig{
427+
ServiceAccountVerificationKeys: []string{"value"},
428+
},
429+
want: []map[string]interface{}{
430+
{
431+
"cluster_ca": "",
432+
"etcd_api_ca": "",
433+
"etcd_peer_ca": "",
434+
"aggregation_ca": "",
435+
"control_plane_disk_encryption_key": "",
436+
"gkeops_etcd_backup_encryption_key": "",
437+
"service_account_verification_keys": schema.NewSet(schema.HashString, []interface{}{"value"}),
438+
},
439+
},
440+
},
441+
}
442+
443+
for _, tc := range cases {
444+
t.Run(tc.name, func(t *testing.T) {
445+
t.Parallel()
446+
got := flattenUserManagedKeysConfig(tc.config)
447+
if diff := cmp.Diff(got, tc.want); diff != "" {
448+
t.Errorf("flattenUserManagedKeysConfig(%s) returned unexpected diff. +got, -want:\n%s", tc.name, diff)
449+
}
450+
})
451+
}
452+
}

0 commit comments

Comments
 (0)