Skip to content

Commit eed0cef

Browse files
Add support for WorkloadALTSConfig in google_container_cluster (Beta) (#9638) (#6762)
* Add support for WorkloadALTSConfig in google_container_cluster * Fix issues * Make enable_alts within workload_alts_config required and force-send in JSON * Update documentation * Make acceptance test network & subnet names unique * Remove extra test config * Fix spacing [upstream:fffe4b1616a1095d5d95c51f0519a5484c49c216] Signed-off-by: Modular Magician <[email protected]>
1 parent cd21e6a commit eed0cef

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

.changelog/9638.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
container: added `workload_alts_config` field to `google_container_cluster` resource (beta)
3+
```

google-beta/services/container/resource_container_cluster.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,22 @@ func ResourceContainerCluster() *schema.Resource {
20412041
},
20422042
},
20432043
},
2044+
"workload_alts_config": {
2045+
Type: schema.TypeList,
2046+
Optional: true,
2047+
Computed: true,
2048+
MaxItems: 1,
2049+
Description: `Configuration for direct-path (via ALTS) with workload identity.`,
2050+
Elem: &schema.Resource{
2051+
Schema: map[string]*schema.Schema{
2052+
"enable_alts": {
2053+
Type: schema.TypeBool,
2054+
Required: true,
2055+
Description: `Whether the alts handshaker should be enabled or not for direct-path. Requires Workload Identity (workloadPool must be non-empty).`,
2056+
},
2057+
},
2058+
},
2059+
},
20442060
},
20452061
}
20462062
}
@@ -2364,6 +2380,10 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
23642380
cluster.AddonsConfig.GcePersistentDiskCsiDriverConfig.Enabled = true
23652381
}
23662382

2383+
if v, ok := d.GetOk("workload_alts_config"); ok {
2384+
cluster.WorkloadAltsConfig = expandWorkloadAltsConfig(v)
2385+
}
2386+
23672387
req := &container.CreateClusterRequest{
23682388
Cluster: cluster,
23692389
}
@@ -2834,6 +2854,10 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
28342854
return err
28352855
}
28362856

2857+
if err := d.Set("workload_alts_config", flattenWorkloadAltsConfig(cluster.WorkloadAltsConfig)); err != nil {
2858+
return err
2859+
}
2860+
28372861
return nil
28382862
}
28392863

@@ -4129,7 +4153,20 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
41294153

41304154
log.Printf("[INFO] GKE cluster %s Protect Config has been updated to %#v", d.Id(), req.Update.DesiredProtectConfig)
41314155
}
4156+
if d.HasChange("workload_alts_config") {
4157+
req := &container.UpdateClusterRequest{
4158+
Update: &container.ClusterUpdate{
4159+
DesiredWorkloadAltsConfig: expandWorkloadAltsConfig(d.Get("workload_alts_config")),
4160+
},
4161+
}
4162+
4163+
updateF := updateFunc(req, "updating GKE cluster WorkloadALTSConfig")
4164+
if err := transport_tpg.LockedCall(lockKey, updateF); err != nil {
4165+
return err
4166+
}
41324167

4168+
log.Printf("[INFO] GKE cluster %s's WorkloadALTSConfig has been updated", d.Id())
4169+
}
41334170
return resourceContainerClusterRead(d, meta)
41344171
}
41354172

@@ -5318,6 +5355,19 @@ func expandNodePoolAutoConfigNetworkTags(configured interface{}) *container.Netw
53185355
return nt
53195356
}
53205357

5358+
func expandWorkloadAltsConfig(configured interface{}) *container.WorkloadALTSConfig {
5359+
l := configured.([]interface{})
5360+
if len(l) == 0 || l[0] == nil {
5361+
return nil
5362+
}
5363+
5364+
config := l[0].(map[string]interface{})
5365+
return &container.WorkloadALTSConfig{
5366+
EnableAlts: config["enable_alts"].(bool),
5367+
ForceSendFields: []string{"EnableAlts"},
5368+
}
5369+
}
5370+
53215371
func flattenNotificationConfig(c *container.NotificationConfig) []map[string]interface{} {
53225372
if c == nil {
53235373
return nil
@@ -6067,6 +6117,17 @@ func flattenNodePoolAutoConfigNetworkTags(c *container.NetworkTags) []map[string
60676117
return []map[string]interface{}{result}
60686118
}
60696119

6120+
func flattenWorkloadAltsConfig(c *container.WorkloadALTSConfig) []map[string]interface{} {
6121+
if c == nil {
6122+
return nil
6123+
}
6124+
return []map[string]interface{}{
6125+
{
6126+
"enable_alts": c.EnableAlts,
6127+
},
6128+
}
6129+
}
6130+
60706131
func resourceContainerClusterStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
60716132
config := meta.(*transport_tpg.Config)
60726133

google-beta/services/container/resource_container_cluster_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4283,6 +4283,44 @@ func TestAccContainerCluster_withFleetConfig(t *testing.T) {
42834283
})
42844284
}
42854285

4286+
func TestAccContainerCluster_withWorkloadALTSConfig(t *testing.T) {
4287+
t.Parallel()
4288+
4289+
networkName := "gke-cluster-alts"
4290+
subnetworkName := "gke-cluster-alts"
4291+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
4292+
pid := envvar.GetTestProjectFromEnv()
4293+
acctest.VcrTest(t, resource.TestCase{
4294+
PreCheck: func() { acctest.AccTestPreCheck(t) },
4295+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
4296+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
4297+
Steps: []resource.TestStep{
4298+
{
4299+
Config: testAccContainerCluster_withWorkloadALTSConfig(pid, networkName, subnetworkName, clusterName, true),
4300+
},
4301+
{
4302+
ResourceName: "google_container_cluster.with_workload_alts_config",
4303+
ImportState: true,
4304+
ImportStateVerify: true,
4305+
ImportStateVerifyIgnore: []string{"deletion_protection"},
4306+
Check: resource.TestCheckResourceAttr(
4307+
"google_container_cluster.with_workload_alts_config", "workload_alts_config.enable_alts", "true"),
4308+
},
4309+
{
4310+
Config: testAccContainerCluster_withWorkloadALTSConfig(pid, networkName, subnetworkName, clusterName, false),
4311+
},
4312+
{
4313+
ResourceName: "google_container_cluster.with_workload_alts_config",
4314+
ImportState: true,
4315+
ImportStateVerify: true,
4316+
ImportStateVerifyIgnore: []string{"deletion_protection"},
4317+
Check: resource.TestCheckResourceAttr(
4318+
"google_container_cluster.with_workload_alts_config", "workload_alts_config.enable_alts", "false"),
4319+
},
4320+
},
4321+
})
4322+
}
4323+
42864324
func testAccContainerCluster_withFleetConfig(name, projectID string) string {
42874325
return fmt.Sprintf(`
42884326
resource "google_container_cluster" "primary" {
@@ -9206,3 +9244,40 @@ resource "google_container_cluster" "without_confidential_boot_disk" {
92069244
}
92079245
`, clusterName, npName)
92089246
}
9247+
9248+
func testAccContainerCluster_withWorkloadALTSConfig(projectID, name, networkName, subnetworkName string, enable bool) string {
9249+
return fmt.Sprintf(`
9250+
data "google_project" "project" {
9251+
provider = google-beta
9252+
project_id = "%s"
9253+
}
9254+
resource "google_compute_network" "network" {
9255+
provider = google-beta
9256+
name = "%s"
9257+
auto_create_subnetworks = false
9258+
enable_ula_internal_ipv6 = true
9259+
}
9260+
resource "google_compute_subnetwork" "subnet" {
9261+
provider = google-beta
9262+
name = "%s"
9263+
network = google_compute_network.network.id
9264+
ip_cidr_range = "9.12.22.0/24"
9265+
region = "us-central1"
9266+
}
9267+
resource "google_container_cluster" "with_workload_alts_config" {
9268+
provider = google-beta
9269+
name = "%s"
9270+
location = "us-central1-a"
9271+
initial_node_count = 1
9272+
network = google_compute_network.network.name
9273+
subnetwork = google_compute_subnetwork.subnet.name
9274+
workload_alts_config {
9275+
enable_alts = %v
9276+
}
9277+
workload_identity_config {
9278+
workload_pool = "${data.google_project.project.project_id}.svc.id.goog"
9279+
}
9280+
deletion_protection = false
9281+
}
9282+
`, projectID, networkName, subnetworkName, name, enable)
9283+
}

website/docs/r/container_cluster.html.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ Enable/Disable Security Posture API features for the cluster. Structure is [docu
381381
* `fleet` - (Optional)
382382
Fleet configuration for the cluster. Structure is [documented below](#nested_fleet).
383383

384+
* `workload_alts_config` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
385+
Configuration for [direct-path (via ALTS) with workload identity.](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#workloadaltsconfig). Structure is [documented below](#nested_workload_alts_config).
386+
384387
<a name="nested_default_snat_status"></a>The `default_snat_status` block supports
385388

386389
* `disabled` - (Required) Whether the cluster disables default in-node sNAT rules. In-node sNAT rules will be disabled when defaultSnatStatus is disabled.When disabled is set to false, default IP masquerade rules will be applied to the nodes to prevent sNAT on cluster internal traffic
@@ -1295,6 +1298,9 @@ linux_node_config {
12951298

12961299
* `project` - (Optional) The name of the Fleet host project where this cluster will be registered.
12971300

1301+
<a name="nested_workload_alts_config"></a>The `workload_alts_config` block supports:
1302+
1303+
* `enable_alts` - (Required) Whether the alts handshaker should be enabled or not for direct-path. Requires Workload Identity ([workloadPool]((#nested_workload_identity_config)) must be non-empty).
12981304

12991305
## Attributes Reference
13001306

0 commit comments

Comments
 (0)