Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.

Commit e741bb8

Browse files
committed
feat: add topology info to DRANet on GCP
1 parent 1627d9e commit e741bb8

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

pkg/cloudprovider/cloud.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type CloudInstance struct {
2222
Provider CloudProvider
2323
AcceleratorProtocol string
2424
Interfaces []NetworkInterface
25+
Topology string
2526
}
2627

2728
type NetworkInterface struct {

pkg/cloudprovider/gce/gce.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23+
"strings"
2324
"time"
2425

2526
"cloud.google.com/go/compute/metadata"
@@ -94,6 +95,12 @@ func GetInstance(ctx context.Context) (*cloudprovider.CloudInstance, error) {
9495
klog.Infof("could not get network interfaces on GCE ... retrying: %v", err)
9596
return false, nil
9697
}
98+
gceTopologyAttributes, err := metadata.GetWithContext(ctx, "instance/attributes/physical_host")
99+
if err != nil {
100+
klog.Infof("could not get physical host on GCE ... retrying: %v", err)
101+
return false, nil
102+
}
103+
instance.Topology = gceTopologyAttributes
97104
return true, nil
98105
})
99106
if err != nil {
@@ -103,7 +110,7 @@ func GetInstance(ctx context.Context) (*cloudprovider.CloudInstance, error) {
103110
}
104111

105112
// GetGCEAttributes fetches all attributes related to the provided GCP network.
106-
func GetGCEAttributes(network string) map[resourceapi.QualifiedName]resourceapi.DeviceAttribute {
113+
func GetGCEAttributes(network, topology string) map[resourceapi.QualifiedName]resourceapi.DeviceAttribute {
107114
attributes := make(map[resourceapi.QualifiedName]resourceapi.DeviceAttribute)
108115
var projectNumber int64
109116
var name string
@@ -114,7 +121,17 @@ func GetGCEAttributes(network string) map[resourceapi.QualifiedName]resourceapi.
114121
klog.Warningf("Error parsing network %q : %v", network, err)
115122
return nil
116123
}
124+
topologyParts := strings.SplitN(strings.TrimPrefix(topology, "/"), "/", 3)
125+
// topology may not be always available
126+
if len(topologyParts) == 3 {
127+
attributes["gce.dra.net/block"] = resourceapi.DeviceAttribute{StringValue: &topologyParts[0]}
128+
attributes["gce.dra.net/subblock"] = resourceapi.DeviceAttribute{StringValue: &topologyParts[1]}
129+
attributes["gce.dra.net/host"] = resourceapi.DeviceAttribute{StringValue: &topologyParts[2]}
130+
} else {
131+
klog.Warningf("Error parsing host topology, may be unsupported on VM %q : %v", topology, err)
132+
}
117133
attributes["gce.dra.net/networkName"] = resourceapi.DeviceAttribute{StringValue: &name}
118134
attributes["gce.dra.net/networkProjectNumber"] = resourceapi.DeviceAttribute{IntValue: &projectNumber}
135+
klog.Info(attributes)
119136
return attributes
120137
}

pkg/inventory/cloud.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func getProviderAttributes(mac string, instance *cloudprovider.CloudInstance) ma
5959
}
6060
for _, cloudInterface := range instance.Interfaces {
6161
if cloudInterface.Mac == mac {
62-
return gce.GetGCEAttributes(cloudInterface.Network)
62+
return gce.GetGCEAttributes(cloudInterface.Network, instance.Topology)
6363
}
6464
}
6565
klog.Warningf("no matching cloud interface found for mac %s", mac)

pkg/inventory/cloud_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ limitations under the License.
1717
package inventory
1818

1919
import (
20-
"github.com/google/go-cmp/cmp"
2120
"testing"
2221

2322
"github.com/google/dranet/pkg/cloudprovider"
23+
"github.com/google/go-cmp/cmp"
2424
resourceapi "k8s.io/api/resource/v1beta1"
2525
"k8s.io/utils/ptr"
2626
)
@@ -67,10 +67,14 @@ func TestGetProviderAttributes(t *testing.T) {
6767
{Mac: "00:11:22:33:44:55", Network: "projects/12345/networks/test-network"},
6868
{Mac: "AA:BB:CC:DD:EE:FF", Network: "projects/67890/networks/other-network"},
6969
},
70+
Topology: "/block/subblock/host",
7071
},
7172
want: map[resourceapi.QualifiedName]resourceapi.DeviceAttribute{
7273
"gce.dra.net/networkName": {StringValue: ptr.To("test-network")},
7374
"gce.dra.net/networkProjectNumber": {IntValue: ptr.To(int64(12345))},
75+
"gce.dra.net/block": {StringValue: ptr.To("block")},
76+
"gce.dra.net/subblock": {StringValue: ptr.To("subblock")},
77+
"gce.dra.net/host": {StringValue: ptr.To("host")},
7478
},
7579
},
7680
{
@@ -84,6 +88,22 @@ func TestGetProviderAttributes(t *testing.T) {
8488
},
8589
want: nil, // gce.GetGCEAttributes returns nil for invalid network string
8690
},
91+
{
92+
name: "GCE provider, MAC found, valid network, invalid topology",
93+
mac: "00:11:22:33:44:55",
94+
instance: &cloudprovider.CloudInstance{
95+
Provider: cloudprovider.CloudProviderGCE,
96+
Interfaces: []cloudprovider.NetworkInterface{
97+
{Mac: "00:11:22:33:44:55", Network: "projects/12345/networks/test-network"},
98+
{Mac: "AA:BB:CC:DD:EE:FF", Network: "projects/67890/networks/other-network"},
99+
},
100+
Topology: "/block/subblock",
101+
},
102+
want: map[resourceapi.QualifiedName]resourceapi.DeviceAttribute{
103+
"gce.dra.net/networkName": {StringValue: ptr.To("test-network")},
104+
"gce.dra.net/networkProjectNumber": {IntValue: ptr.To(int64(12345))},
105+
},
106+
},
87107
{
88108
name: "Unsupported provider, MAC found",
89109
mac: "00:11:22:33:44:55",

0 commit comments

Comments
 (0)