From f55952ee46cab4c86acb30f7493e48c55043282b Mon Sep 17 00:00:00 2001 From: Gaurav Ghildiyal Date: Wed, 27 Aug 2025 23:25:07 +0000 Subject: [PATCH 1/2] feat: Add IPAlias GCE Attribute to published resourceslices --- pkg/cloudprovider/cloud.go | 11 ++++++----- pkg/cloudprovider/gce/gce.go | 10 ++++++++-- pkg/inventory/cloud_test.go | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/pkg/cloudprovider/cloud.go b/pkg/cloudprovider/cloud.go index 68185410..6eeb2727 100644 --- a/pkg/cloudprovider/cloud.go +++ b/pkg/cloudprovider/cloud.go @@ -26,11 +26,12 @@ type CloudInstance struct { } type NetworkInterface struct { - IPv4 string `json:"ip,omitempty"` - IPv6 []string `json:"ipv6,omitempty"` - Mac string `json:"mac,omitempty"` - MTU int `json:"mtu,omitempty"` - Network string `json:"network,omitempty"` + IPv4 string `json:"ip,omitempty"` + IPv6 []string `json:"ipv6,omitempty"` + Mac string `json:"mac,omitempty"` + MTU int `json:"mtu,omitempty"` + Network string `json:"network,omitempty"` + IPAliases []string `json:"ipAliases,omitempty"` } // CloudProvider represents the type of cloud provider. diff --git a/pkg/cloudprovider/gce/gce.go b/pkg/cloudprovider/gce/gce.go index 4a52f284..ecefc237 100644 --- a/pkg/cloudprovider/gce/gce.go +++ b/pkg/cloudprovider/gce/gce.go @@ -49,6 +49,7 @@ const ( AttrGCEHost = GCEAttrPrefix + "/" + "host" AttrGCENetworkName = GCEAttrPrefix + "/" + "networkName" AttrGCENetworkProjectNumber = GCEAttrPrefix + "/" + "networkProjectNumber" + AttrGCEIPAliases = GCEAttrPrefix + "/" + "ipAliases" ) var ( @@ -150,6 +151,11 @@ func GetGCEAttributes(mac string, instance *cloudprovider.CloudInstance) map[res } } if interfaceForMacFound { + if len(interfaceForMac.IPAliases) > 0 { + ipAliases := strings.Join(interfaceForMac.IPAliases, ",") + attributes[AttrGCEIPAliases] = resourceapi.DeviceAttribute{StringValue: &ipAliases} + } + var projectNumber int64 var name string // Use custom parsing because the network path is @@ -159,8 +165,8 @@ func GetGCEAttributes(mac string, instance *cloudprovider.CloudInstance) map[res klog.Warningf("Error parsing network %q : %v", interfaceForMac.Network, err) return nil } - attributes["gce.dra.net/networkName"] = resourceapi.DeviceAttribute{StringValue: &name} - attributes["gce.dra.net/networkProjectNumber"] = resourceapi.DeviceAttribute{IntValue: &projectNumber} + attributes[AttrGCENetworkName] = resourceapi.DeviceAttribute{StringValue: &name} + attributes[AttrGCENetworkProjectNumber] = resourceapi.DeviceAttribute{IntValue: &projectNumber} } else { klog.V(4).Infof("No cloud metadata found for device with mac %q; it is possible this device has no associated cloud provider metadata", mac) } diff --git a/pkg/inventory/cloud_test.go b/pkg/inventory/cloud_test.go index d70629af..eef056d5 100644 --- a/pkg/inventory/cloud_test.go +++ b/pkg/inventory/cloud_test.go @@ -132,6 +132,25 @@ func TestGetProviderAttributes(t *testing.T) { }, want: nil, }, + { + name: "GCE provider, MAC found, with IP aliases", + mac: "00:11:22:33:44:55", + instance: &cloudprovider.CloudInstance{ + Provider: cloudprovider.CloudProviderGCE, + Interfaces: []cloudprovider.NetworkInterface{ + { + Mac: "00:11:22:33:44:55", + Network: "projects/12345/networks/test-network", + IPAliases: []string{"10.0.0.1/24", "10.0.0.2/24"}, + }, + }, + }, + want: map[resourceapi.QualifiedName]resourceapi.DeviceAttribute{ + "gce.dra.net/networkName": {StringValue: ptr.To("test-network")}, + "gce.dra.net/networkProjectNumber": {IntValue: ptr.To(int64(12345))}, + "gce.dra.net/ipAliases": {StringValue: ptr.To("10.0.0.1/24,10.0.0.2/24")}, + }, + }, } for _, tt := range tests { From 48bf0c0505794f08b401fd310b4b060d079535f0 Mon Sep 17 00:00:00 2001 From: Gaurav Ghildiyal Date: Wed, 27 Aug 2025 23:31:45 +0000 Subject: [PATCH 2/2] chore: Use constants for GCE Attributes --- pkg/cloudprovider/gce/gce.go | 8 ++++---- pkg/inventory/cloud_test.go | 27 ++++++++++++++------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/pkg/cloudprovider/gce/gce.go b/pkg/cloudprovider/gce/gce.go index ecefc237..b742381e 100644 --- a/pkg/cloudprovider/gce/gce.go +++ b/pkg/cloudprovider/gce/gce.go @@ -45,7 +45,7 @@ const ( GCEAttrPrefix = "gce.dra.net" AttrGCEBlock = GCEAttrPrefix + "/" + "block" - AttrGCESubblock = GCEAttrPrefix + "/" + "subblock" + AttrGCESubBlock = GCEAttrPrefix + "/" + "subBlock" AttrGCEHost = GCEAttrPrefix + "/" + "host" AttrGCENetworkName = GCEAttrPrefix + "/" + "networkName" AttrGCENetworkProjectNumber = GCEAttrPrefix + "/" + "networkProjectNumber" @@ -132,9 +132,9 @@ func GetGCEAttributes(mac string, instance *cloudprovider.CloudInstance) map[res topologyParts := strings.SplitN(strings.TrimPrefix(instance.Topology, "/"), "/", 3) // topology may not be always available if len(topologyParts) == 3 { - attributes["gce.dra.net/block"] = resourceapi.DeviceAttribute{StringValue: &topologyParts[0]} - attributes["gce.dra.net/subblock"] = resourceapi.DeviceAttribute{StringValue: &topologyParts[1]} - attributes["gce.dra.net/host"] = resourceapi.DeviceAttribute{StringValue: &topologyParts[2]} + attributes[AttrGCEBlock] = resourceapi.DeviceAttribute{StringValue: &topologyParts[0]} + attributes[AttrGCESubBlock] = resourceapi.DeviceAttribute{StringValue: &topologyParts[1]} + attributes[AttrGCEHost] = resourceapi.DeviceAttribute{StringValue: &topologyParts[2]} } else { klog.Warningf("Error parsing host topology %q; it may be unsupported for the VM", instance.Topology) } diff --git a/pkg/inventory/cloud_test.go b/pkg/inventory/cloud_test.go index eef056d5..0a8a0f5c 100644 --- a/pkg/inventory/cloud_test.go +++ b/pkg/inventory/cloud_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/google/dranet/pkg/cloudprovider" + "github.com/google/dranet/pkg/cloudprovider/gce" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" resourceapi "k8s.io/api/resource/v1" @@ -70,9 +71,9 @@ func TestGetProviderAttributes(t *testing.T) { Topology: "/block/subblock/host", }, want: map[resourceapi.QualifiedName]resourceapi.DeviceAttribute{ - "gce.dra.net/block": {StringValue: ptr.To("block")}, - "gce.dra.net/subblock": {StringValue: ptr.To("subblock")}, - "gce.dra.net/host": {StringValue: ptr.To("host")}, + gce.AttrGCEBlock: {StringValue: ptr.To("block")}, + gce.AttrGCESubBlock: {StringValue: ptr.To("subblock")}, + gce.AttrGCEHost: {StringValue: ptr.To("host")}, }, }, { @@ -87,11 +88,11 @@ func TestGetProviderAttributes(t *testing.T) { Topology: "/block/subblock/host", }, want: map[resourceapi.QualifiedName]resourceapi.DeviceAttribute{ - "gce.dra.net/networkName": {StringValue: ptr.To("test-network")}, - "gce.dra.net/networkProjectNumber": {IntValue: ptr.To(int64(12345))}, - "gce.dra.net/block": {StringValue: ptr.To("block")}, - "gce.dra.net/subblock": {StringValue: ptr.To("subblock")}, - "gce.dra.net/host": {StringValue: ptr.To("host")}, + gce.AttrGCENetworkName: {StringValue: ptr.To("test-network")}, + gce.AttrGCENetworkProjectNumber: {IntValue: ptr.To(int64(12345))}, + gce.AttrGCEBlock: {StringValue: ptr.To("block")}, + gce.AttrGCESubBlock: {StringValue: ptr.To("subblock")}, + gce.AttrGCEHost: {StringValue: ptr.To("host")}, }, }, { @@ -117,8 +118,8 @@ func TestGetProviderAttributes(t *testing.T) { Topology: "/block/subblock", }, want: map[resourceapi.QualifiedName]resourceapi.DeviceAttribute{ - "gce.dra.net/networkName": {StringValue: ptr.To("test-network")}, - "gce.dra.net/networkProjectNumber": {IntValue: ptr.To(int64(12345))}, + gce.AttrGCENetworkName: {StringValue: ptr.To("test-network")}, + gce.AttrGCENetworkProjectNumber: {IntValue: ptr.To(int64(12345))}, }, }, { @@ -146,9 +147,9 @@ func TestGetProviderAttributes(t *testing.T) { }, }, want: map[resourceapi.QualifiedName]resourceapi.DeviceAttribute{ - "gce.dra.net/networkName": {StringValue: ptr.To("test-network")}, - "gce.dra.net/networkProjectNumber": {IntValue: ptr.To(int64(12345))}, - "gce.dra.net/ipAliases": {StringValue: ptr.To("10.0.0.1/24,10.0.0.2/24")}, + gce.AttrGCENetworkName: {StringValue: ptr.To("test-network")}, + gce.AttrGCENetworkProjectNumber: {IntValue: ptr.To(int64(12345))}, + gce.AttrGCEIPAliases: {StringValue: ptr.To("10.0.0.1/24,10.0.0.2/24")}, }, }, }