forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnode.go
More file actions
273 lines (231 loc) · 8.66 KB
/
Copy pathnode.go
File metadata and controls
273 lines (231 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package ipam
import (
"context"
"fmt"
"log/slog"
"github.com/cilium/cilium/pkg/azure/types"
"github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/ipam"
"github.com/cilium/cilium/pkg/ipam/stats"
ipamTypes "github.com/cilium/cilium/pkg/ipam/types"
v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
"github.com/cilium/cilium/pkg/logging/logfields"
)
type ipamNodeActions interface {
InstanceID() string
}
// Node represents a node representing an Azure instance
type Node struct {
// k8sObj is the CiliumNode custom resource representing the node
k8sObj *v2.CiliumNode
// node contains the general purpose fields of a node
node ipamNodeActions
// manager is the Azure node manager responsible for this node
manager *InstancesManager
// vmss is the Azure VM Scale Set the node belongs to (optional)
vmss string
}
// UpdatedNode is called when an update to the CiliumNode is received.
func (n *Node) UpdatedNode(obj *v2.CiliumNode) {
n.k8sObj = obj
}
// PopulateStatusFields fills in the status field of the CiliumNode custom
// resource with Azure specific information
func (n *Node) PopulateStatusFields(k8sObj *v2.CiliumNode) {
interfaces := []types.AzureInterface{}
n.manager.mutex.RLock()
defer n.manager.mutex.RUnlock()
n.manager.instances.ForeachInterface(n.node.InstanceID(), func(instanceID, interfaceID string, interfaceObj ipamTypes.InterfaceRevision) error {
iface, ok := interfaceObj.Resource.(*types.AzureInterface)
if ok {
interfaces = append(interfaces, *(iface.DeepCopy()))
}
return nil
})
// ForeachInterface iterates a Go map, so order is randomized per call;
// SetInterfaces sorts by ID for a stable slice.
k8sObj.Status.Azure.SetInterfaces(interfaces)
}
// PrepareIPRelease prepares the release of IPs
func (n *Node) PrepareIPRelease(excessIPs int, scopedLog *slog.Logger) *ipam.ReleaseAction {
return &ipam.ReleaseAction{}
}
// ReleaseIPPrefixes is a no-op on Azure since Azure ENIs don't
// support prefix delegation.
func (n *Node) ReleaseIPPrefixes(ctx context.Context, r *ipam.ReleaseAction) error {
// nothing to do
return nil
}
// ReleaseIPs performs the IP release operation
func (n *Node) ReleaseIPs(ctx context.Context, r *ipam.ReleaseAction) error {
return fmt.Errorf("not implemented")
}
// PrepareIPAllocation returns the number of IPs that can be allocated/created.
func (n *Node) PrepareIPAllocation(scopedLog *slog.Logger) (a *ipam.AllocationAction, err error) {
a = &ipam.AllocationAction{}
requiredIfaceName := n.k8sObj.Spec.Azure.InterfaceName
n.manager.mutex.RLock()
defer n.manager.mutex.RUnlock()
err = n.manager.instances.ForeachInterface(n.node.InstanceID(), func(instanceID, interfaceID string, interfaceObj ipamTypes.InterfaceRevision) error {
iface, ok := interfaceObj.Resource.(*types.AzureInterface)
if !ok {
return fmt.Errorf("invalid interface object")
}
availableOnInterface, available := isAvailableInterface(requiredIfaceName, iface, scopedLog)
if !available {
return nil
}
a.IPv4.InterfaceCandidates++
if a.InterfaceID == "" {
scopedLog.Debug(
"Interface has IPs available",
logfields.ID, iface.ID,
logfields.AvailableAddresses, availableOnInterface,
)
preferredPoolIDs := []ipamTypes.PoolID{}
for _, address := range iface.Addresses {
if address.Subnet != "" {
preferredPoolIDs = append(preferredPoolIDs, ipamTypes.PoolID(address.Subnet))
}
}
poolID, available := n.manager.subnets.FirstSubnetWithAvailableAddresses(preferredPoolIDs)
if poolID != ipamTypes.PoolNotExists {
scopedLog.Debug(
"Subnet has IPs available",
logfields.SubnetID, poolID,
logfields.AvailableAddresses, available,
)
a.InterfaceID = iface.ID
a.Interface = interfaceObj
a.PoolID = poolID
a.IPv4.AvailableForAllocation = min(available, availableOnInterface)
}
}
return nil
})
return
}
// AllocateIPs performs the Azure IP allocation operation
func (n *Node) AllocateIPs(ctx context.Context, a *ipam.AllocationAction) error {
iface, ok := a.Interface.Resource.(*types.AzureInterface)
if !ok {
return fmt.Errorf("invalid interface object")
}
if iface.GetVMScaleSetName() == "" {
return n.manager.api.AssignPrivateIpAddressesVM(ctx, string(a.PoolID), iface.Name, a.IPv4.AvailableForAllocation)
} else {
return n.manager.api.AssignPrivateIpAddressesVMSS(ctx, iface.GetVMID(), iface.GetVMScaleSetName(), string(a.PoolID), iface.Name, a.IPv4.AvailableForAllocation)
}
}
func (n *Node) AllocateStaticIP(ctx context.Context, staticIPTags ipamTypes.Tags) (string, error) {
if n.vmss == "" {
return n.manager.api.AssignPublicIPAddressesVM(ctx, n.node.InstanceID(), staticIPTags)
}
return n.manager.api.AssignPublicIPAddressesVMSS(ctx, n.node.InstanceID(), n.vmss, staticIPTags)
}
// CreateInterface is called to create a new interface. This operation is
// currently not supported on Azure.
func (n *Node) CreateInterface(ctx context.Context, allocation *ipam.AllocationAction, scopedLog *slog.Logger) (int, string, error) {
return 0, "", fmt.Errorf("not implemented")
}
// ResyncInterfacesAndIPs is called to retrieve interfaces and IPs known
// to the Azure API and return them
func (n *Node) ResyncInterfacesAndIPs(ctx context.Context, scopedLog *slog.Logger) (
available ipamTypes.AllocationMap,
stats stats.InterfaceStats,
err error) {
// Azure virtual machines always have an upper limit of 256 addresses.
// Both VMs and NICs can have a maximum of 256 addresses, so as long as
// there is at least one available NIC, we can allocate up to 256 addresses
// on the VM (minus the primary IP address).
stats.NodeCapacity = max(n.GetMaximumAllocatableIPv4()-1, 0)
if n.node.InstanceID() == "" {
return nil, stats, nil
}
available = ipamTypes.AllocationMap{}
n.manager.mutex.RLock()
defer n.manager.mutex.RUnlock()
err = n.manager.instances.ForeachAddress(n.node.InstanceID(), func(instanceID, interfaceID, ip, poolID string, addressObj ipamTypes.Address) error {
address, ok := addressObj.(types.AzureAddress)
if !ok {
scopedLog.Warn(
"Not an Azure address object, ignoring IP",
logfields.IPAddr, ip,
)
return nil
}
if address.State == types.StateSucceeded {
available[address.IP] = ipamTypes.AllocationIP{Resource: interfaceID}
} else {
scopedLog.Warn(
"Ignoring potentially available IP due to non-successful state",
logfields.IPAddr, ip,
logfields.State, address.State,
)
}
return nil
})
if err != nil {
return nil, stats, err
}
requiredIfaceName := n.k8sObj.Spec.Azure.InterfaceName
err = n.manager.instances.ForeachInterface(n.node.InstanceID(), func(instanceID, interfaceID string, interfaceObj ipamTypes.InterfaceRevision) error {
iface, ok := interfaceObj.Resource.(*types.AzureInterface)
if !ok {
return fmt.Errorf("invalid interface object")
}
// Cache the VMSS name from the first interface we see
if n.vmss == "" {
n.vmss = iface.GetVMScaleSetName()
}
_, available := isAvailableInterface(requiredIfaceName, iface, scopedLog)
if available {
stats.RemainingAvailableInterfaceCount++
}
return nil
})
if err != nil {
return nil, stats, err
}
return available, stats, nil
}
// GetMaximumAllocatableIPv4 returns the maximum amount of IPv4 addresses
// that can be allocated to the instance
func (n *Node) GetMaximumAllocatableIPv4() int {
// An Azure node can allocate up to 256 private IP addresses
// source: https://github.com/MicrosoftDocs/azure-docs/blob/master/includes/azure-virtual-network-limits.md#networking-limits---azure-resource-manager
return types.InterfaceAddressLimit
}
// GetMinimumAllocatableIPv4 returns the minimum amount of IPv4 addresses that
// must be allocated to the instance.
func (n *Node) GetMinimumAllocatableIPv4() int {
return defaults.IPAMPreAllocation
}
func (n *Node) IsPrefixDelegated() bool {
return false
}
// isAvailableInterface returns whether interface is available and the number of available IPs to allocate in interface
func isAvailableInterface(requiredIfaceName string, iface *types.AzureInterface, scopedLog *slog.Logger) (availableOnInterface int, available bool) {
if requiredIfaceName != "" {
if iface.Name != requiredIfaceName {
scopedLog.Debug(
"Not considering interface as available since it does not match the required name",
logfields.Interface, iface.Name,
logfields.Required, requiredIfaceName,
)
return 0, false
}
}
scopedLog.Debug(
"Considering interface as available",
logfields.ID, iface.ID,
logfields.NumAddresses, len(iface.Addresses),
)
availableOnInterface = max(types.InterfaceAddressLimit-len(iface.Addresses), 0)
if availableOnInterface <= 0 {
return 0, false
}
return availableOnInterface, true
}