Skip to content

Commit c528a1e

Browse files
Initial implementation of tpu_config in container cluster (#6130) (#4390)
Co-authored-by: Shuya Ma <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Shuya Ma <[email protected]>
1 parent 618af44 commit c528a1e

File tree

3 files changed

+136
-4
lines changed

3 files changed

+136
-4
lines changed

.changelog/6130.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 `tpu_config` to `google_container_cluster` (beta only)
3+
```

google-beta/resource_container_cluster.go

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,11 +521,43 @@ func resourceContainerCluster() *schema.Resource {
521521
},
522522

523523
"enable_tpu": {
524-
Type: schema.TypeBool,
524+
Type: schema.TypeBool,
525+
Optional: true,
526+
ForceNew: true,
527+
Description: `Whether to enable Cloud TPU resources in this cluster.`,
528+
ConflictsWith: []string{"tpu_config"},
529+
Computed: true,
530+
// TODO: deprecate when tpu_config is correctly returned by the API
531+
// Deprecated: "Deprecated in favor of tpu_config",
532+
},
533+
534+
"tpu_config": {
535+
Type: schema.TypeList,
525536
Optional: true,
526-
ForceNew: true,
527-
Description: `Whether to enable Cloud TPU resources in this cluster.`,
528-
Default: false,
537+
Computed: true,
538+
MaxItems: 1,
539+
Description: `TPU configuration for the cluster.`,
540+
Elem: &schema.Resource{
541+
Schema: map[string]*schema.Schema{
542+
"enabled": {
543+
Type: schema.TypeBool,
544+
Required: true,
545+
ForceNew: true,
546+
Description: `Whether Cloud TPU integration is enabled or not`,
547+
},
548+
"ipv4_cidr_block": {
549+
Type: schema.TypeString,
550+
Computed: true,
551+
Description: `IPv4 CIDR block reserved for Cloud TPU in the VPC.`,
552+
},
553+
"use_service_networking": {
554+
Type: schema.TypeBool,
555+
Optional: true,
556+
ForceNew: true,
557+
Description: `Whether to use service networking for Cloud TPU or not`,
558+
},
559+
},
560+
},
529561
},
530562

531563
"enable_legacy_abac": {
@@ -1606,6 +1638,10 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
16061638
cluster.IdentityServiceConfig = expandIdentityServiceConfig(v)
16071639
}
16081640

1641+
if v, ok := d.GetOk("tpu_config"); ok {
1642+
cluster.TpuConfig = expandContainerClusterTpuConfig(v)
1643+
}
1644+
16091645
if v, ok := d.GetOk("resource_usage_export_config"); ok {
16101646
cluster.ResourceUsageExportConfig = expandResourceUsageExportConfig(v)
16111647
}
@@ -3633,6 +3669,19 @@ func expandMonitoringConfig(configured interface{}) *container.MonitoringConfig
36333669
return mc
36343670
}
36353671

3672+
func expandContainerClusterTpuConfig(configured interface{}) *container.TpuConfig {
3673+
l := configured.([]interface{})
3674+
if len(l) == 0 || l[0] == nil {
3675+
return nil
3676+
}
3677+
3678+
config := l[0].(map[string]interface{})
3679+
return &container.TpuConfig{
3680+
Enabled: config["enabled"].(bool),
3681+
UseServiceNetworking: config["use_service_networking"].(bool),
3682+
}
3683+
}
3684+
36363685
func flattenNotificationConfig(c *container.NotificationConfig) []map[string]interface{} {
36373686
if c == nil {
36383687
return nil

google-beta/resource_container_cluster_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,6 +2527,30 @@ func TestAccContainerCluster_withDNSConfig(t *testing.T) {
25272527
})
25282528
}
25292529

2530+
func TestAccContainerCluster_withTPUConfig(t *testing.T) {
2531+
t.Parallel()
2532+
2533+
clusterName := fmt.Sprintf("tf-test-cluster-%s", randString(t, 10))
2534+
containerNetName := fmt.Sprintf("tf-test-container-net-%s", randString(t, 10))
2535+
vcrTest(t, resource.TestCase{
2536+
PreCheck: func() { testAccPreCheck(t) },
2537+
Providers: testAccProviders,
2538+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
2539+
Steps: []resource.TestStep{
2540+
{
2541+
Config: testAccContainerCluster_withTPUConfig(containerNetName, clusterName),
2542+
},
2543+
{
2544+
ResourceName: "google_container_cluster.with_tpu_config",
2545+
ImportState: true,
2546+
ImportStateVerify: true,
2547+
// TODO: remove when tpu_config can be read from the API
2548+
ImportStateVerifyIgnore: []string{"tpu_config"},
2549+
},
2550+
},
2551+
})
2552+
}
2553+
25302554
func testAccContainerCluster_masterAuthorizedNetworksDisabled(t *testing.T, resource_name string) resource.TestCheckFunc {
25312555
return func(s *terraform.State) error {
25322556
rs, ok := s.RootModule().Resources[resource_name]
@@ -5271,3 +5295,59 @@ resource "google_container_cluster" "primary" {
52715295
}
52725296
`, name, name, name)
52735297
}
5298+
5299+
func testAccContainerCluster_withTPUConfig(network, cluster string) string {
5300+
return fmt.Sprintf(`
5301+
resource "google_compute_network" "container_network" {
5302+
name = "%s"
5303+
auto_create_subnetworks = false
5304+
}
5305+
5306+
resource "google_compute_subnetwork" "container_subnetwork" {
5307+
name = google_compute_network.container_network.name
5308+
network = google_compute_network.container_network.name
5309+
ip_cidr_range = "10.0.36.0/24"
5310+
region = "us-central1"
5311+
private_ip_google_access = true
5312+
5313+
secondary_ip_range {
5314+
range_name = "pod"
5315+
ip_cidr_range = "10.0.0.0/19"
5316+
}
5317+
5318+
secondary_ip_range {
5319+
range_name = "svc"
5320+
ip_cidr_range = "10.0.32.0/22"
5321+
}
5322+
}
5323+
5324+
resource "google_container_cluster" "with_tpu_config" {
5325+
name = "%s"
5326+
location = "us-central1-a"
5327+
initial_node_count = 1
5328+
5329+
5330+
tpu_config {
5331+
enabled = true
5332+
use_service_networking = true
5333+
}
5334+
5335+
network = google_compute_network.container_network.name
5336+
subnetwork = google_compute_subnetwork.container_subnetwork.name
5337+
networking_mode = "VPC_NATIVE"
5338+
5339+
private_cluster_config {
5340+
enable_private_endpoint = true
5341+
enable_private_nodes = true
5342+
master_ipv4_cidr_block = "10.42.0.0/28"
5343+
}
5344+
master_authorized_networks_config {
5345+
}
5346+
5347+
ip_allocation_policy {
5348+
cluster_secondary_range_name = google_compute_subnetwork.container_subnetwork.secondary_ip_range[0].range_name
5349+
services_secondary_range_name = google_compute_subnetwork.container_subnetwork.secondary_ip_range[1].range_name
5350+
}
5351+
}
5352+
`, network, cluster)
5353+
}

0 commit comments

Comments
 (0)