Skip to content

Commit 4cf60b8

Browse files
committed
ipam: Remove unused ForeachAddress abstraction
Following cilium#45985, the `Interface.ForeachAddress` method now only has one caller: azure's `ResyncInterfacesAndIPs`, where it is followed by a `ForeachInterface`. The addresses loop can be inlined under that existing interfaces one, resulting in a slight optimization. With that change, ForeachAddress is no longer used anywhere, meaning all 3 cloud provider implementations can be removed, as well as the wrapper `InstanceMap.ForeachAddress`. Signed-off-by: Hadrien Patte <hadrien.patte@datadoghq.com>
1 parent 5eb066a commit 4cf60b8

7 files changed

Lines changed: 13 additions & 200 deletions

File tree

pkg/alibabacloud/eni/types/types.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,6 @@ func (e *ENI) InterfaceID() string {
135135
return e.NetworkInterfaceID
136136
}
137137

138-
// ForeachAddress iterates over all addresses and calls fn
139-
func (e *ENI) ForeachAddress(id string, fn types.AddressIterator) error {
140-
for _, address := range e.PrivateIPSets {
141-
if address.Primary {
142-
continue
143-
}
144-
if err := fn(id, e.NetworkInterfaceID, address.PrivateIpAddress, address); err != nil {
145-
return err
146-
}
147-
}
148-
149-
return nil
150-
}
151-
152138
// ENIStatus is the status of ENI addressing of the node
153139
type ENIStatus struct {
154140
// ENIs is the list of ENIs on the node

pkg/aws/eni/types/types.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,6 @@ func (e *ENI) InterfaceID() string {
178178
return e.ID
179179
}
180180

181-
// ForeachAddress iterates over all addresses and calls fn
182-
func (e *ENI) ForeachAddress(id string, fn types.AddressIterator) error {
183-
for _, address := range e.Addresses {
184-
if err := fn(id, e.ID, address, address); err != nil {
185-
return err
186-
}
187-
}
188-
189-
return nil
190-
}
191-
192181
// IsExcludedBySpec returns true if the ENI is excluded by the provided spec and
193182
// therefore should not be managed by Cilium.
194183
func (e *ENI) IsExcludedBySpec(spec ENISpec) bool {

pkg/azure/ipam/node.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -169,30 +169,6 @@ func (n *Node) ResyncInterfacesAndIPs(ctx context.Context, scopedLog *slog.Logge
169169
n.manager.mutex.RLock()
170170
defer n.manager.mutex.RUnlock()
171171
usePrimary := n.manager.usePrimary
172-
err = n.manager.instances.ForeachAddress(n.node.InstanceID(), func(instanceID, interfaceID, ip string, addressObj ipamTypes.Address) error {
173-
address, ok := addressObj.(types.AzureAddress)
174-
if !ok {
175-
scopedLog.Warn(
176-
"Not an Azure address object, ignoring IP",
177-
logfields.IPAddr, ip,
178-
)
179-
return nil
180-
}
181-
182-
if address.State == types.StateSucceeded {
183-
available[address.IP] = ipamTypes.AllocationIP{Resource: interfaceID}
184-
} else {
185-
scopedLog.Warn(
186-
"Ignoring potentially available IP due to non-successful state",
187-
logfields.IPAddr, ip,
188-
logfields.State, address.State,
189-
)
190-
}
191-
return nil
192-
})
193-
if err != nil {
194-
return nil, stats, err
195-
}
196172

197173
// Azure caps both NICs and VMs at 256 addresses; start from that ceiling
198174
// and decrement per NIC below for any primary slot we can't allocate.
@@ -204,6 +180,18 @@ func (n *Node) ResyncInterfacesAndIPs(ctx context.Context, scopedLog *slog.Logge
204180
return fmt.Errorf("invalid interface object")
205181
}
206182

183+
for _, address := range iface.Addresses {
184+
if address.State == types.StateSucceeded {
185+
available[address.IP] = ipamTypes.AllocationIP{Resource: interfaceID}
186+
} else {
187+
scopedLog.Warn(
188+
"Ignoring potentially available IP due to non-successful state",
189+
logfields.IPAddr, address.IP,
190+
logfields.State, address.State,
191+
)
192+
}
193+
}
194+
207195
// Cache the VMSS name from the first interface we see
208196
if n.vmss == "" {
209197
n.vmss = iface.GetVMScaleSetName()
@@ -215,8 +203,7 @@ func (n *Node) ResyncInterfacesAndIPs(ctx context.Context, scopedLog *slog.Logge
215203
nodeCapacity--
216204
}
217205

218-
_, available := isAvailableInterface(requiredIfaceName, iface, usePrimary, scopedLog)
219-
if available {
206+
if _, isAvailable := isAvailableInterface(requiredIfaceName, iface, usePrimary, scopedLog); isAvailable {
220207
stats.RemainingAvailableInterfaceCount++
221208
}
222209
return nil

pkg/azure/types/types.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,3 @@ func (a *AzureInterface) GetVMScaleSetName() string {
186186
func (a *AzureInterface) GetVMID() string {
187187
return a.vmID
188188
}
189-
190-
// ForeachAddress iterates over all addresses and calls fn.
191-
func (a *AzureInterface) ForeachAddress(id string, fn types.AddressIterator) error {
192-
for _, address := range a.Addresses {
193-
if err := fn(id, a.ID, address.IP, address); err != nil {
194-
return err
195-
}
196-
}
197-
198-
return nil
199-
}

pkg/azure/types/types_test.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,8 @@ import (
1212
// Register the Azure resource-ID parser so SetID()/extractIDs() populate
1313
// the VMSS/VM/RG fields exercised by TestExtractIDs.
1414
_ "github.com/cilium/cilium/pkg/azure/types/azureid"
15-
"github.com/cilium/cilium/pkg/ipam/types"
1615
)
1716

18-
func TestForeachAddresses(t *testing.T) {
19-
m := types.NewInstanceMap()
20-
m.Update("i-1", &azuretypes.AzureInterface{ID: "1", Addresses: []azuretypes.AzureAddress{
21-
{IP: "1.1.1.1"},
22-
{IP: "2.2.2.2"},
23-
}})
24-
m.Update("i-2", &azuretypes.AzureInterface{ID: "1", Addresses: []azuretypes.AzureAddress{
25-
{IP: "3.3.3.3"},
26-
{IP: "4.4.4.4"},
27-
}})
28-
29-
// Iterate over all instances
30-
addresses := 0
31-
m.ForeachAddress("", func(instanceID, interfaceID, ip string, address types.Address) error {
32-
addresses++
33-
return nil
34-
})
35-
require.Equal(t, 4, addresses)
36-
37-
// Iterate over "i-1"
38-
addresses = 0
39-
m.ForeachAddress("i-1", func(instanceID, interfaceID, ip string, address types.Address) error {
40-
addresses++
41-
return nil
42-
})
43-
require.Equal(t, 2, addresses)
44-
45-
// Iterate over all interfaces
46-
interfaces := 0
47-
m.ForeachInterface("", func(instanceID, interfaceID string, interfaceObj types.Interface) error {
48-
interfaces++
49-
return nil
50-
})
51-
require.Equal(t, 2, interfaces)
52-
}
53-
5417
func TestExtractIDs(t *testing.T) {
5518
tests := []struct {
5619
name string

pkg/ipam/types/types.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,6 @@ type Interface interface {
468468
// InterfaceID must return the identifier of the interface
469469
InterfaceID() string
470470

471-
// ForeachAddress must iterate over all addresses of the interface and
472-
// call fn for each address
473-
ForeachAddress(instanceID string, fn AddressIterator) error
474-
475471
// DeepCopyInterface returns a deep copy of the underlying interface type.
476472
DeepCopyInterface() Interface
477473
}
@@ -549,49 +545,6 @@ func (m *InstanceMap) updateLocked(instanceID string, iface Interface) {
549545
i.Interfaces[iface.InterfaceID()] = iface
550546
}
551547

552-
type Address any
553-
554-
// AddressIterator is the function called by the ForeachAddress iterator
555-
type AddressIterator func(instanceID, interfaceID, ip string, address Address) error
556-
557-
func foreachAddress(instanceID string, instance *Instance, fn AddressIterator) error {
558-
for _, iface := range instance.Interfaces {
559-
if err := iface.ForeachAddress(instanceID, fn); err != nil {
560-
return err
561-
}
562-
}
563-
564-
return nil
565-
}
566-
567-
// ForeachAddress calls fn for each address on each interface attached to each
568-
// instance. If an instanceID is specified, the only the interfaces and
569-
// addresses of the specified instance are considered.
570-
//
571-
// The InstanceMap is read-locked throughout the iteration process, i.e., no
572-
// updates will occur. However, the address object given to the AddressIterator
573-
// will point to live data and must be deep copied if used outside of the
574-
// context of the iterator function.
575-
func (m *InstanceMap) ForeachAddress(instanceID string, fn AddressIterator) error {
576-
m.mutex.RLock()
577-
defer m.mutex.RUnlock()
578-
579-
if instanceID != "" {
580-
if instance := m.data[instanceID]; instance != nil {
581-
return foreachAddress(instanceID, instance, fn)
582-
}
583-
return fmt.Errorf("instance does not exist: %q", instanceID)
584-
}
585-
586-
for instanceID, instance := range m.data {
587-
if err := foreachAddress(instanceID, instance, fn); err != nil {
588-
return err
589-
}
590-
}
591-
592-
return nil
593-
}
594-
595548
// InterfaceIterator is the function called by the ForeachInterface iterator
596549
type InterfaceIterator func(instanceID, interfaceID string, iface Interface) error
597550

pkg/ipam/types/types_test.go

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -42,60 +42,6 @@ func (m *mockInterface) InterfaceID() string {
4242
return m.id
4343
}
4444

45-
func (m *mockInterface) ForeachAddress(instanceID string, fn AddressIterator) error {
46-
for _, ips := range m.pools {
47-
for _, ip := range ips {
48-
if err := fn(instanceID, m.id, ip.String(), ip); err != nil {
49-
return err
50-
}
51-
}
52-
}
53-
54-
return nil
55-
}
56-
57-
func TestForeachAddresses(t *testing.T) {
58-
m := NewInstanceMap()
59-
m.Update("i-1", &mockInterface{
60-
id: "intf0",
61-
pools: map[string][]net.IP{
62-
"s1": {net.ParseIP("1.1.1.1"), net.ParseIP("2.2.2.2")},
63-
},
64-
})
65-
m.Update("i-2", &mockInterface{
66-
id: "intf0",
67-
pools: map[string][]net.IP{
68-
"s1": {net.ParseIP("3.3.3.3"), net.ParseIP("4.4.4.4")},
69-
},
70-
})
71-
72-
// Iterate over all instances
73-
addresses := 0
74-
m.ForeachAddress("", func(instanceID, interfaceID, ip string, address Address) error {
75-
_, ok := address.(net.IP)
76-
require.True(t, ok)
77-
addresses++
78-
return nil
79-
})
80-
require.Equal(t, 4, addresses)
81-
82-
// Iterate over "i-1"
83-
addresses = 0
84-
m.ForeachAddress("i-1", func(instanceID, interfaceID, ip string, address Address) error {
85-
addresses++
86-
return nil
87-
})
88-
require.Equal(t, 2, addresses)
89-
90-
// Iterate over all interfaces
91-
interfaces := 0
92-
m.ForeachInterface("", func(instanceID, interfaceID string, iface Interface) error {
93-
interfaces++
94-
return nil
95-
})
96-
require.Equal(t, 2, interfaces)
97-
}
98-
9945
func TestGetInterface(t *testing.T) {
10046
m := NewInstanceMap()
10147
rev := &mockInterface{

0 commit comments

Comments
 (0)