Skip to content

Commit d6cc202

Browse files
committed
azure: Remove unused GetVpcsAndSubnets function
The refactor in cilium#41555 to do targeted subnet discovery replaced all usages of `GetVpcsAndSubnets` with `GetSubnetsByIDs`. That function is now just dead code. This PR removes the unused `GetVpcsAndSubnets` function along with related supporting code: the `listAllVPCs` and `parseSubnet` functions that were only called from `GetVpcsAndSubnets` and mocks for all those. Signed-off-by: Hadrien Patte <hadrien.patte@datadoghq.com>
1 parent 46b1025 commit d6cc202

7 files changed

Lines changed: 15 additions & 152 deletions

File tree

pkg/azure/api/api.go

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ const (
3838
virtualMachineScaleSetsList = "VirtualMachineScaleSets.List"
3939
virtualMachineScaleSetVMsGet = "VirtualMachineScaleSetVMs.Get"
4040
virtualMachineScaleSetVMsUpdate = "VirtualMachineScaleSetVMs.Update"
41-
virtualNetworksListAll = "VirtualNetworks.ListAll"
4241
subnetsGet = "Subnets.Get"
4342

4443
interfacesListVirtualMachineScaleSetNetworkInterfaces = "Interfaces.ListVirtualMachineScaleSetNetworkInterfaces"
@@ -52,7 +51,6 @@ type Client struct {
5251
resourceGroup string
5352
interfaces *armnetwork.InterfacesClient
5453
publicIPPrefixes *armnetwork.PublicIPPrefixesClient
55-
virtualNetworks *armnetwork.VirtualNetworksClient
5654
virtualMachines *armcompute.VirtualMachinesClient
5755
subnets *armnetwork.SubnetsClient
5856
virtualMachineScaleSetVMs *armcompute.VirtualMachineScaleSetVMsClient
@@ -132,11 +130,6 @@ func NewClient(logger *slog.Logger, cloudName, subscriptionID, resourceGroup, us
132130
return nil, err
133131
}
134132

135-
virtualNetworksClient, err := armnetwork.NewVirtualNetworksClient(subscriptionID, credential, armClientOptions)
136-
if err != nil {
137-
return nil, err
138-
}
139-
140133
virtualMachinesClient, err := armcompute.NewVirtualMachinesClient(subscriptionID, credential, armClientOptions)
141134
if err != nil {
142135
return nil, err
@@ -168,7 +161,6 @@ func NewClient(logger *slog.Logger, cloudName, subscriptionID, resourceGroup, us
168161
resourceGroup: resourceGroup,
169162
interfaces: interfacesClient,
170163
publicIPPrefixes: publicIPPrefixesClient,
171-
virtualNetworks: virtualNetworksClient,
172164
virtualMachines: virtualMachinesClient,
173165
subnets: subnetsClient,
174166
virtualMachineScaleSetVMs: virtualMachineScaleSetVMsClient,
@@ -446,96 +438,13 @@ func (c *Client) ParseInterfacesIntoInstance(networkInterfaces []*armnetwork.Int
446438
return &instance
447439
}
448440

449-
// listAllVPCs lists all VPCs
450-
func (c *Client) listAllVPCs(ctx context.Context) (vpcs []*armnetwork.VirtualNetwork, err error) {
451-
c.limiter.Limit(ctx, virtualNetworksListAll)
452-
sinceStart := spanstat.Start()
453-
454-
// Note: lists all VPCs, not just those in c.resourcegroup
455-
pager := c.virtualNetworks.NewListAllPager(nil)
456-
457-
defer func() {
458-
c.metricsAPI.ObserveAPICall(virtualNetworksListAll, deriveStatus(err), sinceStart.Seconds())
459-
}()
460-
461-
for pager.More() {
462-
nextResult, err := pager.NextPage(ctx)
463-
if err != nil {
464-
return nil, err
465-
}
466-
vpcs = append(vpcs, nextResult.Value...)
467-
}
468-
469-
return vpcs, nil
470-
}
471-
472-
func parseSubnet(subnet *armnetwork.Subnet) (s *ipamTypes.Subnet) {
473-
s = &ipamTypes.Subnet{ID: *subnet.ID}
474-
if subnet.Name != nil {
475-
s.Name = *subnet.Name
476-
}
477-
478-
if subnet.Properties.AddressPrefix != nil {
479-
cidr, err := netip.ParsePrefix(*subnet.Properties.AddressPrefix)
480-
if err != nil {
481-
return nil
482-
}
483-
s.CIDR = cidr
484-
if subnet.Properties.IPConfigurations != nil {
485-
s.AvailableAddresses = availableIPs(cidr) - len(subnet.Properties.IPConfigurations)
486-
} else {
487-
// Azure currently returns nil for subnet IPConfigs if the subnet has a large number of existing IPConfigs.
488-
// API / SDK is supposed to return a IpConfigurationsNextLink which can be used to make an additional
489-
// call to get all IPConfigs. This field however seems to be missing from the API spec.
490-
// Since we cannot fall back to other subnets anyway, assume all IPs are available.
491-
// TODO: Update this once azure-sdk-for-go supports ipConfigurationsNextLink
492-
s.AvailableAddresses = availableIPs(cidr)
493-
}
494-
}
495-
496-
return
497-
}
498-
499441
// availableIPs returns the number of IPs available in a CIDR
500442
func availableIPs(p netip.Prefix) int {
501443
ones := p.Bits()
502444
bits := p.Addr().BitLen()
503445
return 1 << (bits - ones)
504446
}
505447

506-
// GetVpcsAndSubnets retrieves and returns all Vpcs
507-
func (c *Client) GetVpcsAndSubnets(ctx context.Context) (ipamTypes.VirtualNetworkMap, ipamTypes.SubnetMap, error) {
508-
vpcs := ipamTypes.VirtualNetworkMap{}
509-
subnets := ipamTypes.SubnetMap{}
510-
511-
vpcList, err := c.listAllVPCs(ctx)
512-
if err != nil {
513-
return nil, nil, err
514-
}
515-
516-
for _, v := range vpcList {
517-
if v.ID == nil {
518-
continue
519-
}
520-
521-
vpc := &ipamTypes.VirtualNetwork{ID: *v.ID}
522-
vpcs[vpc.ID] = vpc
523-
524-
if v.Properties.Subnets != nil {
525-
for _, subnet := range v.Properties.Subnets {
526-
if subnet.ID == nil {
527-
continue
528-
}
529-
if s := parseSubnet(subnet); s != nil {
530-
subnets[*subnet.ID] = s
531-
}
532-
}
533-
}
534-
}
535-
536-
return vpcs, subnets, nil
537-
}
538-
539448
// parseSubnetID extracts resource group, virtual network, and subnet names from an Azure subnet ID.
540449
// Expected format: /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{vnetName}/subnets/{subnetName}
541450
// Uses arm.ParseResourceID from the Azure SDK for robust parsing.

pkg/azure/api/mock/mock.go

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const (
2727
AllOperations Operation = iota
2828
ListVMNetworkInterfaces
2929
ListAllNetworkInterfaces
30-
GetVpcsAndSubnets
3130
GetSubnetsByIDs
3231
AssignPrivateIpAddressesVMSS
3332
MaxOperation
@@ -42,27 +41,21 @@ type API struct {
4241
mutex lock.RWMutex
4342
subnets map[string]*subnet
4443
instances *ipamTypes.InstanceMap
45-
vnets map[string]*ipamTypes.VirtualNetwork
4644
errors map[Operation]error
4745
delaySim *helpers.DelaySimulator
4846
limiter *rate.Limiter
4947
}
5048

51-
func NewAPI(subnets []*ipamTypes.Subnet, vnets []*ipamTypes.VirtualNetwork) *API {
49+
func NewAPI(subnets []*ipamTypes.Subnet) *API {
5250
api := &API{
5351
instances: ipamTypes.NewInstanceMap(),
5452
subnets: map[string]*subnet{},
55-
vnets: map[string]*ipamTypes.VirtualNetwork{},
5653
errors: map[Operation]error{},
5754
delaySim: helpers.NewDelaySimulator(),
5855
}
5956

6057
api.UpdateSubnets(subnets)
6158

62-
for _, v := range vnets {
63-
api.vnets[v.ID] = v
64-
}
65-
6659
return api
6760
}
6861

@@ -129,33 +122,6 @@ func (a *API) rateLimit() {
129122
}
130123
}
131124

132-
func (a *API) GetVpcsAndSubnets(ctx context.Context) (ipamTypes.VirtualNetworkMap, ipamTypes.SubnetMap, error) {
133-
a.rateLimit()
134-
a.delaySim.Delay(GetVpcsAndSubnets)
135-
136-
a.mutex.RLock()
137-
defer a.mutex.RUnlock()
138-
139-
if err, ok := a.errors[GetVpcsAndSubnets]; ok {
140-
return nil, nil, err
141-
}
142-
143-
vnets := ipamTypes.VirtualNetworkMap{}
144-
subnets := ipamTypes.SubnetMap{}
145-
146-
for _, s := range a.subnets {
147-
sd := s.subnet.DeepCopy()
148-
sd.AvailableAddresses = s.allocator.Free()
149-
subnets[sd.ID] = sd
150-
}
151-
152-
for _, v := range a.vnets {
153-
vnets[v.ID] = v.DeepCopy()
154-
}
155-
156-
return vnets, subnets, nil
157-
}
158-
159125
func (a *API) GetSubnetsByIDs(ctx context.Context, nodeSubnetIDs []string) (ipamTypes.SubnetMap, error) {
160126
a.rateLimit()
161127
a.delaySim.Delay(GetSubnetsByIDs)

pkg/azure/api/mock/mock_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,16 @@ import (
2020
func TestMock(t *testing.T) {
2121
cidr := netip.MustParsePrefix("10.0.0.0/16")
2222
subnet := &ipamTypes.Subnet{ID: "s-1", CIDR: cidr, AvailableAddresses: 65534}
23-
api := NewAPI([]*ipamTypes.Subnet{subnet}, []*ipamTypes.VirtualNetwork{{ID: "v-1"}})
23+
api := NewAPI([]*ipamTypes.Subnet{subnet})
2424
require.NotNil(t, api)
2525

2626
nics, err := api.ListAllNetworkInterfaces(t.Context())
2727
require.NoError(t, err)
2828
instances := api.ParseInterfacesIntoInstanceMap(nics, ipamTypes.SubnetMap{})
2929
require.Equal(t, 0, instances.NumInstances())
3030

31-
vnets, subnets, err := api.GetVpcsAndSubnets(t.Context())
31+
subnets, err := api.GetSubnetsByIDs(t.Context(), []string{"s-1"})
3232
require.NoError(t, err)
33-
require.Len(t, vnets, 1)
34-
require.Equal(t, &ipamTypes.VirtualNetwork{ID: "v-1"}, vnets["v-1"])
3533
require.Len(t, subnets, 1)
3634
require.Equal(t, subnet, subnets["s-1"])
3735

@@ -83,7 +81,7 @@ func TestMock(t *testing.T) {
8381
}
8482

8583
func TestSetMockError(t *testing.T) {
86-
api := NewAPI([]*ipamTypes.Subnet{}, []*ipamTypes.VirtualNetwork{})
84+
api := NewAPI([]*ipamTypes.Subnet{})
8785
require.NotNil(t, api)
8886

8987
mockError := errors.New("error")
@@ -92,8 +90,8 @@ func TestSetMockError(t *testing.T) {
9290
_, err := api.ListAllNetworkInterfaces(t.Context())
9391
require.ErrorIs(t, err, mockError)
9492

95-
api.SetMockError(GetVpcsAndSubnets, mockError)
96-
_, _, err = api.GetVpcsAndSubnets(t.Context())
93+
api.SetMockError(GetSubnetsByIDs, mockError)
94+
_, err = api.GetSubnetsByIDs(t.Context(), nil)
9795
require.ErrorIs(t, err, mockError)
9896

9997
api.SetMockError(AssignPrivateIpAddressesVMSS, mockError)
@@ -104,7 +102,7 @@ func TestSetMockError(t *testing.T) {
104102
func TestSetLimiter(t *testing.T) {
105103
cidr := netip.MustParsePrefix("10.0.0.0/16")
106104
subnet := &ipamTypes.Subnet{ID: "s-1", CIDR: cidr, AvailableAddresses: 100}
107-
api := NewAPI([]*ipamTypes.Subnet{subnet}, []*ipamTypes.VirtualNetwork{{ID: "v-1"}})
105+
api := NewAPI([]*ipamTypes.Subnet{subnet})
108106
require.NotNil(t, api)
109107

110108
api.SetLimiter(10.0, 2)

pkg/azure/ipam/instances.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828

2929
// AzureAPI is the API surface used of the Azure API.
3030
type AzureAPI interface {
31-
GetVpcsAndSubnets(ctx context.Context) (ipamTypes.VirtualNetworkMap, ipamTypes.SubnetMap, error)
3231
GetSubnetsByIDs(ctx context.Context, nodeSubnetIDs []string) (ipamTypes.SubnetMap, error)
3332
AssignPrivateIpAddressesVM(ctx context.Context, subnetID, interfaceName string, addresses int) error
3433
AssignPrivateIpAddressesVMSS(ctx context.Context, instanceID, vmssName, subnetID, interfaceName string, addresses int) error

pkg/azure/ipam/instances_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ var (
6262
},
6363
},
6464
}
65-
66-
vnets = []*ipamTypes.VirtualNetwork{
67-
{ID: "vpc-0"},
68-
{ID: "vpc-1"},
69-
}
7065
)
7166

7267
func iteration1(t *testing.T, api *apimock.API, mngr *InstancesManager) {
@@ -158,7 +153,7 @@ func iteration2(t *testing.T, api *apimock.API, mngr *InstancesManager) {
158153
}
159154

160155
func TestSubnetDiscovery(t *testing.T) {
161-
api := apimock.NewAPI(subnets, vnets)
156+
api := apimock.NewAPI(subnets)
162157
require.NotNil(t, api)
163158

164159
mngr := NewInstancesManager(hivetest.Logger(t), api, false)
@@ -193,7 +188,7 @@ func TestSubnetDiscovery(t *testing.T) {
193188
// the wrong subnet, which Azure rejects with
194189
// VMScaleSetIpConfigurationsOnSameNicCannotUseDifferentSubnets.
195190
func TestResyncInstancePreservesOtherNodesSubnets(t *testing.T) {
196-
api := apimock.NewAPI(subnets2, vnets)
191+
api := apimock.NewAPI(subnets2)
197192
require.NotNil(t, api)
198193

199194
mngr := NewInstancesManager(hivetest.Logger(t), api, false)
@@ -250,7 +245,7 @@ func TestResyncInstancePreservesOtherNodesSubnets(t *testing.T) {
250245
}
251246

252247
func TestExtractSubnetIDs(t *testing.T) {
253-
api := apimock.NewAPI(subnets, vnets)
248+
api := apimock.NewAPI(subnets)
254249
require.NotNil(t, api)
255250

256251
mngr := NewInstancesManager(hivetest.Logger(t), api, false)

pkg/azure/ipam/ipam_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ var (
3737
{ID: "s-2", CIDR: netip.MustParsePrefix("2.2.0.0/16"), VirtualNetworkID: "vpc-1"},
3838
{ID: "s-3", CIDR: netip.MustParsePrefix("3.3.3.3/16"), VirtualNetworkID: "vpc-1"},
3939
}
40-
41-
testVnet = &ipamTypes.VirtualNetwork{
42-
ID: "vpc-1",
43-
}
4440
)
4541

4642
type k8sMock struct {
@@ -146,7 +142,7 @@ func TestIpamPreAllocate8(t *testing.T) {
146142
minAllocate := 0
147143
toUse := 7
148144

149-
api := apimock.NewAPI([]*ipamTypes.Subnet{testSubnet}, []*ipamTypes.VirtualNetwork{testVnet})
145+
api := apimock.NewAPI([]*ipamTypes.Subnet{testSubnet})
150146
instances := NewInstancesManager(hivetest.Logger(t), api, false)
151147
require.NotNil(t, instances)
152148

@@ -208,7 +204,7 @@ func TestIpamMinAllocate10(t *testing.T) {
208204
minAllocate := 10
209205
toUse := 7
210206

211-
api := apimock.NewAPI([]*ipamTypes.Subnet{testSubnet}, []*ipamTypes.VirtualNetwork{testVnet})
207+
api := apimock.NewAPI([]*ipamTypes.Subnet{testSubnet})
212208
instances := NewInstancesManager(hivetest.Logger(t), api, false)
213209
require.NotNil(t, instances)
214210

@@ -292,7 +288,7 @@ func TestIpamManyNodes(t *testing.T) {
292288
numNodes = 2
293289
minAllocate = 1
294290
)
295-
api := apimock.NewAPI(testSubnets, []*ipamTypes.VirtualNetwork{testVnet})
291+
api := apimock.NewAPI(testSubnets)
296292
instances := NewInstancesManager(hivetest.Logger(t), api, false)
297293
require.NotNil(t, instances)
298294

@@ -364,7 +360,7 @@ func TestIpamManyNodes(t *testing.T) {
364360
}
365361

366362
func benchmarkAllocWorker(b *testing.B, workers int64, delay time.Duration, rateLimit float64, burst int) {
367-
api := apimock.NewAPI(testSubnets, []*ipamTypes.VirtualNetwork{testVnet})
363+
api := apimock.NewAPI(testSubnets)
368364
api.SetDelay(apimock.AllOperations, delay)
369365
api.SetLimiter(rateLimit, burst)
370366

pkg/azure/ipam/node_stats_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func newCapacityTestNode(t *testing.T, ifaces []*types.AzureInterface, usePrimar
2525
node: mockIPAMNode("vm1"),
2626
manager: &InstancesManager{
2727
instances: m,
28-
api: apimock.NewAPI(nil, nil),
28+
api: apimock.NewAPI(nil),
2929
usePrimary: usePrimary,
3030
},
3131
k8sObj: &v2.CiliumNode{

0 commit comments

Comments
 (0)