Skip to content

Commit a10fc6e

Browse files
Add NetworkConfigs and NetworkConfig.QueueCount to TPU v2 VM (#12481) (#8865)
[upstream:b2265ae629f0e13d9ec36acebd8b8c6b1a429f03] Signed-off-by: Modular Magician <[email protected]>
1 parent f4220c8 commit a10fc6e

File tree

4 files changed

+264
-12
lines changed

4 files changed

+264
-12
lines changed

.changelog/12481.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
tpuv2: added `network_configs` and `network_config.queue_count` fields to `google_tpu_v2_vm` resource
3+
```

google-beta/services/tpuv2/resource_tpu_v2_vm.go

+221-6
Original file line numberDiff line numberDiff line change
@@ -235,22 +235,73 @@ false, the specified subnetwork or network should have Private Google Access ena
235235
Optional: true,
236236
ForceNew: true,
237237
Description: `The name of the network for the TPU node. It must be a preexisting Google Compute Engine
238-
network. If both network and subnetwork are specified, the given subnetwork must belong
239-
to the given network. If network is not specified, it will be looked up from the
240-
subnetwork if one is provided, or otherwise use "default".`,
238+
network. If none is provided, "default" will be used.`,
239+
},
240+
"queue_count": {
241+
Type: schema.TypeInt,
242+
Optional: true,
243+
ForceNew: true,
244+
Description: `Specifies networking queue count for TPU VM instance's network interface.`,
245+
},
246+
"subnetwork": {
247+
Type: schema.TypeString,
248+
Computed: true,
249+
Optional: true,
250+
ForceNew: true,
251+
Description: `The name of the subnetwork for the TPU node. It must be a preexisting Google Compute
252+
Engine subnetwork. If none is provided, "default" will be used.`,
253+
},
254+
},
255+
},
256+
ConflictsWith: []string{"network_configs"},
257+
},
258+
"network_configs": {
259+
Type: schema.TypeList,
260+
Optional: true,
261+
ForceNew: true,
262+
Description: `Repeated network configurations for the TPU node. This field is used to specify multiple
263+
network configs for the TPU node.`,
264+
Elem: &schema.Resource{
265+
Schema: map[string]*schema.Schema{
266+
"can_ip_forward": {
267+
Type: schema.TypeBool,
268+
Optional: true,
269+
ForceNew: true,
270+
Description: `Allows the TPU node to send and receive packets with non-matching destination or source
271+
IPs. This is required if you plan to use the TPU workers to forward routes.`,
272+
},
273+
"enable_external_ips": {
274+
Type: schema.TypeBool,
275+
Optional: true,
276+
ForceNew: true,
277+
Description: `Indicates that external IP addresses would be associated with the TPU workers. If set to
278+
false, the specified subnetwork or network should have Private Google Access enabled.`,
279+
},
280+
"network": {
281+
Type: schema.TypeString,
282+
Computed: true,
283+
Optional: true,
284+
ForceNew: true,
285+
Description: `The name of the network for the TPU node. It must be a preexisting Google Compute Engine
286+
network. If none is provided, "default" will be used.`,
287+
},
288+
"queue_count": {
289+
Type: schema.TypeInt,
290+
Optional: true,
291+
ForceNew: true,
292+
Description: `Specifies networking queue count for TPU VM instance's network interface.`,
241293
},
242294
"subnetwork": {
243295
Type: schema.TypeString,
244296
Computed: true,
245297
Optional: true,
246298
ForceNew: true,
247299
Description: `The name of the subnetwork for the TPU node. It must be a preexisting Google Compute
248-
Engine subnetwork. If both network and subnetwork are specified, the given subnetwork
249-
must belong to the given network. If subnetwork is not specified, the subnetwork with the
250-
same name as the network will be used.`,
300+
Engine subnetwork. If none is provided, "default" will be used.`,
251301
},
252302
},
253303
},
304+
ConflictsWith: []string{"network_config"},
254305
},
255306
"scheduling_config": {
256307
Type: schema.TypeList,
@@ -500,6 +551,12 @@ func resourceTpuV2VmCreate(d *schema.ResourceData, meta interface{}) error {
500551
} else if v, ok := d.GetOkExists("network_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(networkConfigProp)) && (ok || !reflect.DeepEqual(v, networkConfigProp)) {
501552
obj["networkConfig"] = networkConfigProp
502553
}
554+
networkConfigsProp, err := expandTpuV2VmNetworkConfigs(d.Get("network_configs"), d, config)
555+
if err != nil {
556+
return err
557+
} else if v, ok := d.GetOkExists("network_configs"); !tpgresource.IsEmptyValue(reflect.ValueOf(networkConfigsProp)) && (ok || !reflect.DeepEqual(v, networkConfigsProp)) {
558+
obj["networkConfigs"] = networkConfigsProp
559+
}
503560
serviceAccountProp, err := expandTpuV2VmServiceAccount(d.Get("service_account"), d, config)
504561
if err != nil {
505562
return err
@@ -679,6 +736,9 @@ func resourceTpuV2VmRead(d *schema.ResourceData, meta interface{}) error {
679736
if err := d.Set("network_config", flattenTpuV2VmNetworkConfig(res["networkConfig"], d, config)); err != nil {
680737
return fmt.Errorf("Error reading Vm: %s", err)
681738
}
739+
if err := d.Set("network_configs", flattenTpuV2VmNetworkConfigs(res["networkConfigs"], d, config)); err != nil {
740+
return fmt.Errorf("Error reading Vm: %s", err)
741+
}
682742
if err := d.Set("service_account", flattenTpuV2VmServiceAccount(res["serviceAccount"], d, config)); err != nil {
683743
return fmt.Errorf("Error reading Vm: %s", err)
684744
}
@@ -972,6 +1032,8 @@ func flattenTpuV2VmNetworkConfig(v interface{}, d *schema.ResourceData, config *
9721032
flattenTpuV2VmNetworkConfigEnableExternalIps(original["enableExternalIps"], d, config)
9731033
transformed["can_ip_forward"] =
9741034
flattenTpuV2VmNetworkConfigCanIpForward(original["canIpForward"], d, config)
1035+
transformed["queue_count"] =
1036+
flattenTpuV2VmNetworkConfigQueueCount(original["queueCount"], d, config)
9751037
return []interface{}{transformed}
9761038
}
9771039
func flattenTpuV2VmNetworkConfigNetwork(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
@@ -990,6 +1052,78 @@ func flattenTpuV2VmNetworkConfigCanIpForward(v interface{}, d *schema.ResourceDa
9901052
return v
9911053
}
9921054

1055+
func flattenTpuV2VmNetworkConfigQueueCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1056+
// Handles the string fixed64 format
1057+
if strVal, ok := v.(string); ok {
1058+
if intVal, err := tpgresource.StringToFixed64(strVal); err == nil {
1059+
return intVal
1060+
}
1061+
}
1062+
1063+
// number values are represented as float64
1064+
if floatVal, ok := v.(float64); ok {
1065+
intVal := int(floatVal)
1066+
return intVal
1067+
}
1068+
1069+
return v // let terraform core handle it otherwise
1070+
}
1071+
1072+
func flattenTpuV2VmNetworkConfigs(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1073+
if v == nil {
1074+
return v
1075+
}
1076+
l := v.([]interface{})
1077+
transformed := make([]interface{}, 0, len(l))
1078+
for _, raw := range l {
1079+
original := raw.(map[string]interface{})
1080+
if len(original) < 1 {
1081+
// Do not include empty json objects coming back from the api
1082+
continue
1083+
}
1084+
transformed = append(transformed, map[string]interface{}{
1085+
"network": flattenTpuV2VmNetworkConfigsNetwork(original["network"], d, config),
1086+
"subnetwork": flattenTpuV2VmNetworkConfigsSubnetwork(original["subnetwork"], d, config),
1087+
"enable_external_ips": flattenTpuV2VmNetworkConfigsEnableExternalIps(original["enableExternalIps"], d, config),
1088+
"can_ip_forward": flattenTpuV2VmNetworkConfigsCanIpForward(original["canIpForward"], d, config),
1089+
"queue_count": flattenTpuV2VmNetworkConfigsQueueCount(original["queueCount"], d, config),
1090+
})
1091+
}
1092+
return transformed
1093+
}
1094+
func flattenTpuV2VmNetworkConfigsNetwork(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1095+
return v
1096+
}
1097+
1098+
func flattenTpuV2VmNetworkConfigsSubnetwork(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1099+
return v
1100+
}
1101+
1102+
func flattenTpuV2VmNetworkConfigsEnableExternalIps(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1103+
return v
1104+
}
1105+
1106+
func flattenTpuV2VmNetworkConfigsCanIpForward(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1107+
return v
1108+
}
1109+
1110+
func flattenTpuV2VmNetworkConfigsQueueCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1111+
// Handles the string fixed64 format
1112+
if strVal, ok := v.(string); ok {
1113+
if intVal, err := tpgresource.StringToFixed64(strVal); err == nil {
1114+
return intVal
1115+
}
1116+
}
1117+
1118+
// number values are represented as float64
1119+
if floatVal, ok := v.(float64); ok {
1120+
intVal := int(floatVal)
1121+
return intVal
1122+
}
1123+
1124+
return v // let terraform core handle it otherwise
1125+
}
1126+
9931127
func flattenTpuV2VmServiceAccount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
9941128
if v == nil {
9951129
return nil
@@ -1321,6 +1455,13 @@ func expandTpuV2VmNetworkConfig(v interface{}, d tpgresource.TerraformResourceDa
13211455
transformed["canIpForward"] = transformedCanIpForward
13221456
}
13231457

1458+
transformedQueueCount, err := expandTpuV2VmNetworkConfigQueueCount(original["queue_count"], d, config)
1459+
if err != nil {
1460+
return nil, err
1461+
} else if val := reflect.ValueOf(transformedQueueCount); val.IsValid() && !tpgresource.IsEmptyValue(val) {
1462+
transformed["queueCount"] = transformedQueueCount
1463+
}
1464+
13241465
return transformed, nil
13251466
}
13261467

@@ -1340,6 +1481,80 @@ func expandTpuV2VmNetworkConfigCanIpForward(v interface{}, d tpgresource.Terrafo
13401481
return v, nil
13411482
}
13421483

1484+
func expandTpuV2VmNetworkConfigQueueCount(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1485+
return v, nil
1486+
}
1487+
1488+
func expandTpuV2VmNetworkConfigs(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1489+
l := v.([]interface{})
1490+
req := make([]interface{}, 0, len(l))
1491+
for _, raw := range l {
1492+
if raw == nil {
1493+
continue
1494+
}
1495+
original := raw.(map[string]interface{})
1496+
transformed := make(map[string]interface{})
1497+
1498+
transformedNetwork, err := expandTpuV2VmNetworkConfigsNetwork(original["network"], d, config)
1499+
if err != nil {
1500+
return nil, err
1501+
} else if val := reflect.ValueOf(transformedNetwork); val.IsValid() && !tpgresource.IsEmptyValue(val) {
1502+
transformed["network"] = transformedNetwork
1503+
}
1504+
1505+
transformedSubnetwork, err := expandTpuV2VmNetworkConfigsSubnetwork(original["subnetwork"], d, config)
1506+
if err != nil {
1507+
return nil, err
1508+
} else if val := reflect.ValueOf(transformedSubnetwork); val.IsValid() && !tpgresource.IsEmptyValue(val) {
1509+
transformed["subnetwork"] = transformedSubnetwork
1510+
}
1511+
1512+
transformedEnableExternalIps, err := expandTpuV2VmNetworkConfigsEnableExternalIps(original["enable_external_ips"], d, config)
1513+
if err != nil {
1514+
return nil, err
1515+
} else {
1516+
transformed["enableExternalIps"] = transformedEnableExternalIps
1517+
}
1518+
1519+
transformedCanIpForward, err := expandTpuV2VmNetworkConfigsCanIpForward(original["can_ip_forward"], d, config)
1520+
if err != nil {
1521+
return nil, err
1522+
} else {
1523+
transformed["canIpForward"] = transformedCanIpForward
1524+
}
1525+
1526+
transformedQueueCount, err := expandTpuV2VmNetworkConfigsQueueCount(original["queue_count"], d, config)
1527+
if err != nil {
1528+
return nil, err
1529+
} else if val := reflect.ValueOf(transformedQueueCount); val.IsValid() && !tpgresource.IsEmptyValue(val) {
1530+
transformed["queueCount"] = transformedQueueCount
1531+
}
1532+
1533+
req = append(req, transformed)
1534+
}
1535+
return req, nil
1536+
}
1537+
1538+
func expandTpuV2VmNetworkConfigsNetwork(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1539+
return v, nil
1540+
}
1541+
1542+
func expandTpuV2VmNetworkConfigsSubnetwork(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1543+
return v, nil
1544+
}
1545+
1546+
func expandTpuV2VmNetworkConfigsEnableExternalIps(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1547+
return v, nil
1548+
}
1549+
1550+
func expandTpuV2VmNetworkConfigsCanIpForward(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1551+
return v, nil
1552+
}
1553+
1554+
func expandTpuV2VmNetworkConfigsQueueCount(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1555+
return v, nil
1556+
}
1557+
13431558
func expandTpuV2VmServiceAccount(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
13441559
l := v.([]interface{})
13451560
if len(l) == 0 || l[0] == nil {

google-beta/services/tpuv2/resource_tpu_v2_vm_generated_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ resource "google_tpu_v2_vm" "tpu" {
134134
enable_external_ips = true
135135
network = google_compute_network.network.id
136136
subnetwork = google_compute_subnetwork.subnet.id
137+
queue_count = 32
137138
}
138139
139140
scheduling_config {

website/docs/r/tpu_v2_vm.html.markdown

+39-6
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ resource "google_tpu_v2_vm" "tpu" {
9090
enable_external_ips = true
9191
network = google_compute_network.network.id
9292
subnetwork = google_compute_subnetwork.subnet.id
93+
queue_count = 32
9394
}
9495
9596
scheduling_config {
@@ -206,6 +207,12 @@ The following arguments are supported:
206207
Network configurations for the TPU node.
207208
Structure is [documented below](#nested_network_config).
208209

210+
* `network_configs` -
211+
(Optional)
212+
Repeated network configurations for the TPU node. This field is used to specify multiple
213+
network configs for the TPU node.
214+
Structure is [documented below](#nested_network_configs).
215+
209216
* `service_account` -
210217
(Optional)
211218
The Google Cloud Platform Service Account to be used by the TPU node VMs. If None is
@@ -260,16 +267,12 @@ The following arguments are supported:
260267
* `network` -
261268
(Optional)
262269
The name of the network for the TPU node. It must be a preexisting Google Compute Engine
263-
network. If both network and subnetwork are specified, the given subnetwork must belong
264-
to the given network. If network is not specified, it will be looked up from the
265-
subnetwork if one is provided, or otherwise use "default".
270+
network. If none is provided, "default" will be used.
266271

267272
* `subnetwork` -
268273
(Optional)
269274
The name of the subnetwork for the TPU node. It must be a preexisting Google Compute
270-
Engine subnetwork. If both network and subnetwork are specified, the given subnetwork
271-
must belong to the given network. If subnetwork is not specified, the subnetwork with the
272-
same name as the network will be used.
275+
Engine subnetwork. If none is provided, "default" will be used.
273276

274277
* `enable_external_ips` -
275278
(Optional)
@@ -281,6 +284,36 @@ The following arguments are supported:
281284
Allows the TPU node to send and receive packets with non-matching destination or source
282285
IPs. This is required if you plan to use the TPU workers to forward routes.
283286

287+
* `queue_count` -
288+
(Optional)
289+
Specifies networking queue count for TPU VM instance's network interface.
290+
291+
<a name="nested_network_configs"></a>The `network_configs` block supports:
292+
293+
* `network` -
294+
(Optional)
295+
The name of the network for the TPU node. It must be a preexisting Google Compute Engine
296+
network. If none is provided, "default" will be used.
297+
298+
* `subnetwork` -
299+
(Optional)
300+
The name of the subnetwork for the TPU node. It must be a preexisting Google Compute
301+
Engine subnetwork. If none is provided, "default" will be used.
302+
303+
* `enable_external_ips` -
304+
(Optional)
305+
Indicates that external IP addresses would be associated with the TPU workers. If set to
306+
false, the specified subnetwork or network should have Private Google Access enabled.
307+
308+
* `can_ip_forward` -
309+
(Optional)
310+
Allows the TPU node to send and receive packets with non-matching destination or source
311+
IPs. This is required if you plan to use the TPU workers to forward routes.
312+
313+
* `queue_count` -
314+
(Optional)
315+
Specifies networking queue count for TPU VM instance's network interface.
316+
284317
<a name="nested_service_account"></a>The `service_account` block supports:
285318

286319
* `email` -

0 commit comments

Comments
 (0)