Skip to content

Commit 14cf07c

Browse files
committed
ipam: Move node manager to operator/pkg/ipam/nodemanager
The `Node` and `NodeManager` types in `pkg/ipam` (`node.go` and `node_manager.go`) drive IP orchestration on the operator side: they import `operator/k8s` and `operator/watchers`, and their only callers are the cloud-provider allocators (now under `operator/pkg/ipam/allocator`). This commit continues work started in afbcb75 and moves those types under `operator/pkg/ipam/nodemanager` so the agent/operator boundary becomes more clear. Signed-off-by: Hadrien Patte <hadrien.patte@datadoghq.com>
1 parent 416d83a commit 14cf07c

26 files changed

Lines changed: 182 additions & 133 deletions

File tree

operator/pkg/ipam/allocator/alibabacloud/alibabacloud.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import (
1313

1414
operatorOption "github.com/cilium/cilium/operator/option"
1515
"github.com/cilium/cilium/operator/pkg/ipam/allocator"
16+
"github.com/cilium/cilium/operator/pkg/ipam/nodemanager"
1617
alibabacloudAPI "github.com/cilium/cilium/pkg/alibabacloud/api"
1718
"github.com/cilium/cilium/pkg/alibabacloud/eni"
1819
"github.com/cilium/cilium/pkg/alibabacloud/eni/limits"
1920
"github.com/cilium/cilium/pkg/alibabacloud/metadata"
20-
"github.com/cilium/cilium/pkg/ipam"
2121
"github.com/cilium/cilium/pkg/logging/logfields"
2222
)
2323

@@ -89,11 +89,11 @@ func (a *AllocatorAlibabaCloud) Init(ctx context.Context, logger *slog.Logger, a
8989
// Start kicks off ENI allocation, the initial connection to AlibabaCloud
9090
// APIs is done in a blocking manner. Provided this is successful, a controller is
9191
// started to manage allocation based on CiliumNode custom resources
92-
func (a *AllocatorAlibabaCloud) Start(ctx context.Context, getterUpdater ipam.CiliumNodeGetterUpdater, iMetrics ipam.MetricsAPI) (allocator.NodeEventHandler, error) {
92+
func (a *AllocatorAlibabaCloud) Start(ctx context.Context, getterUpdater allocator.CiliumNodeGetterUpdater, iMetrics nodemanager.MetricsAPI) (allocator.NodeEventHandler, error) {
9393
a.logger.Info("Starting AlibabaCloud ENI allocator...")
9494

9595
instances := eni.NewInstancesManager(a.rootLogger, a.client)
96-
nodeManager, err := ipam.NewNodeManager(a.logger, instances, getterUpdater, iMetrics,
96+
nodeManager, err := nodemanager.NewNodeManager(a.logger, instances, getterUpdater, iMetrics,
9797
a.ParallelAllocWorkers, a.AlibabaCloudReleaseExcessIPs, 0, false)
9898
if err != nil {
9999
return nil, fmt.Errorf("unable to initialize AlibabaCloud node manager: %w", err)

operator/pkg/ipam/allocator/aws/aws.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import (
1313

1414
operatorOption "github.com/cilium/cilium/operator/option"
1515
"github.com/cilium/cilium/operator/pkg/ipam/allocator"
16+
"github.com/cilium/cilium/operator/pkg/ipam/nodemanager"
1617
ec2shim "github.com/cilium/cilium/pkg/aws/ec2"
1718
"github.com/cilium/cilium/pkg/aws/eni"
1819
"github.com/cilium/cilium/pkg/aws/metadata"
1920
"github.com/cilium/cilium/pkg/defaults"
20-
"github.com/cilium/cilium/pkg/ipam"
2121
"github.com/cilium/cilium/pkg/logging/logfields"
2222
"github.com/cilium/cilium/pkg/option"
2323
"github.com/cilium/cilium/pkg/time"
@@ -125,7 +125,7 @@ func (a *AllocatorAWS) Init(ctx context.Context, logger *slog.Logger, aMetrics e
125125
// Start kicks of ENI allocation, the initial connection to AWS
126126
// APIs is done in a blocking manner, given that is successful, a controller is
127127
// started to manage allocation based on CiliumNode custom resources
128-
func (a *AllocatorAWS) Start(ctx context.Context, getterUpdater ipam.CiliumNodeGetterUpdater, iMetrics ipam.MetricsAPI) (allocator.NodeEventHandler, error) {
128+
func (a *AllocatorAWS) Start(ctx context.Context, getterUpdater allocator.CiliumNodeGetterUpdater, iMetrics nodemanager.MetricsAPI) (allocator.NodeEventHandler, error) {
129129
a.logger.Info("Starting ENI allocator...")
130130

131131
imds, err := metadata.NewClient(ctx)
@@ -136,7 +136,7 @@ func (a *AllocatorAWS) Start(ctx context.Context, getterUpdater ipam.CiliumNodeG
136136
if err != nil {
137137
return nil, fmt.Errorf("unable to initialize ENI instances manager: %w", err)
138138
}
139-
nodeManager, err := ipam.NewNodeManager(a.logger, instances, getterUpdater, iMetrics,
139+
nodeManager, err := nodemanager.NewNodeManager(a.logger, instances, getterUpdater, iMetrics,
140140
a.ParallelAllocWorkers, a.AWSReleaseExcessIPs, a.ExcessIPReleaseDelay,
141141
a.AWSEnablePrefixDelegation)
142142
if err != nil {

operator/pkg/ipam/allocator/azure/azure.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
"log/slog"
1010

1111
"github.com/cilium/cilium/operator/pkg/ipam/allocator"
12+
"github.com/cilium/cilium/operator/pkg/ipam/nodemanager"
1213
azureAPI "github.com/cilium/cilium/pkg/azure/api"
1314
azureIPAM "github.com/cilium/cilium/pkg/azure/ipam"
14-
"github.com/cilium/cilium/pkg/ipam"
1515
"github.com/cilium/cilium/pkg/logging/logfields"
1616
)
1717

@@ -37,7 +37,7 @@ func (a *AllocatorAzure) Init(ctx context.Context, logger *slog.Logger) error {
3737
}
3838

3939
// Start kicks of the Azure IP allocation
40-
func (a *AllocatorAzure) Start(ctx context.Context, getterUpdater ipam.CiliumNodeGetterUpdater, azMetrics azureAPI.MetricsAPI, iMetrics ipam.MetricsAPI) (allocator.NodeEventHandler, error) {
40+
func (a *AllocatorAzure) Start(ctx context.Context, getterUpdater allocator.CiliumNodeGetterUpdater, azMetrics azureAPI.MetricsAPI, iMetrics nodemanager.MetricsAPI) (allocator.NodeEventHandler, error) {
4141
a.logger.Info("Starting Azure IP allocator...")
4242

4343
a.logger.Debug("Retrieving Azure cloud name via Azure IMS")
@@ -73,7 +73,7 @@ func (a *AllocatorAzure) Start(ctx context.Context, getterUpdater ipam.CiliumNod
7373
return nil, fmt.Errorf("unable to create Azure client: %w", err)
7474
}
7575
instances := azureIPAM.NewInstancesManager(a.rootLogger, azureClient)
76-
nodeManager, err := ipam.NewNodeManager(a.logger, instances, getterUpdater, iMetrics, a.ParallelAllocWorkers, false, 0, false)
76+
nodeManager, err := nodemanager.NewNodeManager(a.logger, instances, getterUpdater, iMetrics, a.ParallelAllocWorkers, false, 0, false)
7777
if err != nil {
7878
return nil, fmt.Errorf("unable to initialize Azure node manager: %w", err)
7979
}

operator/pkg/ipam/allocator/clusterpool/clusterpool.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/cilium/cilium/operator/pkg/ipam/allocator"
1212
"github.com/cilium/cilium/operator/pkg/ipam/allocator/clusterpool/cidralloc"
1313
"github.com/cilium/cilium/operator/pkg/ipam/allocator/podcidr"
14-
"github.com/cilium/cilium/pkg/ipam"
1514
"github.com/cilium/cilium/pkg/logging/logfields"
1615
"github.com/cilium/cilium/pkg/option"
1716
"github.com/cilium/cilium/pkg/trigger"
@@ -77,7 +76,7 @@ func (a *AllocatorOperator) Init(ctx context.Context, logger *slog.Logger) error
7776
}
7877

7978
// Start kicks of Operator allocation.
80-
func (a *AllocatorOperator) Start(ctx context.Context, updater ipam.CiliumNodeGetterUpdater, iMetrics trigger.MetricsObserver) (allocator.NodeEventHandler, error) {
79+
func (a *AllocatorOperator) Start(ctx context.Context, updater allocator.CiliumNodeGetterUpdater, iMetrics trigger.MetricsObserver) (allocator.NodeEventHandler, error) {
8180
a.logger.Info(
8281
"Starting ClusterPool IP allocator",
8382
logfields.IPv4CIDRs, a.ClusterPoolIPv4CIDR,

operator/pkg/ipam/allocator/podcidr/podcidr.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414

1515
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
1616

17+
"github.com/cilium/cilium/operator/pkg/ipam/allocator"
1718
"github.com/cilium/cilium/operator/pkg/ipam/allocator/clusterpool/cidralloc"
1819
"github.com/cilium/cilium/pkg/controller"
19-
"github.com/cilium/cilium/pkg/ipam"
2020
v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
2121
"github.com/cilium/cilium/pkg/lock"
2222
"github.com/cilium/cilium/pkg/logging/logfields"
@@ -191,7 +191,7 @@ type NodesPodCIDRManager struct {
191191
func NewNodesPodCIDRManager(
192192
logger *slog.Logger,
193193
v4Allocators, v6Allocators []cidralloc.CIDRAllocator,
194-
nodeGetter ipam.CiliumNodeGetterUpdater,
194+
nodeGetter allocator.CiliumNodeGetterUpdater,
195195
triggerMetrics trigger.MetricsObserver) *NodesPodCIDRManager {
196196

197197
n := &NodesPodCIDRManager{
@@ -242,7 +242,7 @@ func NewNodesPodCIDRManager(
242242
// In case any of the nodes failed to be synced with kubernetes the returned
243243
// error is for one of those nodes. Remaining nodes will still be synced with
244244
// kubernetes.
245-
func syncToK8s(logger *slog.Logger, nodeGetterUpdater ipam.CiliumNodeGetterUpdater, ciliumNodesToK8s map[string]*ciliumNodeK8sOp) (retErr error) {
245+
func syncToK8s(logger *slog.Logger, nodeGetterUpdater allocator.CiliumNodeGetterUpdater, ciliumNodesToK8s map[string]*ciliumNodeK8sOp) (retErr error) {
246246
for nodeName, nodeToK8s := range ciliumNodesToK8s {
247247
var (
248248
err, err2 error

operator/pkg/ipam/allocator/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,12 @@ type NodeEventHandler interface {
1616
Delete(resource *v2.CiliumNode)
1717
Resync(context.Context, time.Time)
1818
}
19+
20+
// CiliumNodeGetterUpdater defines the interface used to interact with the k8s
21+
// apiserver to retrieve and update the CiliumNode custom resource
22+
type CiliumNodeGetterUpdater interface {
23+
Create(node *v2.CiliumNode) (*v2.CiliumNode, error)
24+
Update(origResource, newResource *v2.CiliumNode) (*v2.CiliumNode, error)
25+
UpdateStatus(origResource, newResource *v2.CiliumNode) (*v2.CiliumNode, error)
26+
Get(name string) (*v2.CiliumNode, error)
27+
}

operator/pkg/ipam/metrics/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func NewMetrics() *Metrics {
196196
}
197197
}
198198

199-
// ipam.MetricsAPI implementation
199+
// nodemanager.MetricsAPI implementation
200200

201201
func (m *Metrics) SetIPAvailable(node string, cap int) {
202202
m.AvailableIPs.WithLabelValues(node).Set(float64(cap))
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
// Copyright 2017 Lyft, Inc.
55

6-
package ipam
6+
package nodemanager
77

88
import (
99
"context"
@@ -47,6 +47,8 @@ const (
4747
// operator status
4848
success = "success"
4949
failed = "failed"
50+
51+
fieldName = "name"
5052
)
5153

5254
func (n *Node) SetOpts(ops NodeOperations) {
@@ -478,9 +480,7 @@ func (n *Node) recalculate(ctx context.Context) {
478480
defer n.mutex.Unlock()
479481

480482
if err != nil {
481-
var limitsNotFound LimitsNotFound
482-
ok := errors.As(err, &limitsNotFound)
483-
if ok {
483+
if errors.Is(err, ErrLimitsNotFound) {
484484
scopedLog.Warn("Instance limits not found.", logfields.Error, err)
485485
} else {
486486
scopedLog.Warn("Instance not found! Please delete corresponding ciliumnode if instance has already been deleted.", logfields.Error, err)
@@ -765,6 +765,9 @@ type ReleaseAction struct {
765765
IPPrefixesToRelease []string
766766
}
767767

768+
// ErrLimitsNotFound signals lack of limits for given instance type.
769+
var ErrLimitsNotFound = errors.New("Limits not found")
770+
768771
// maintenanceAction represents the resources available for allocation for a
769772
// particular ciliumNode. If an existing interface has IP allocation capacity
770773
// left, that capacity is used up first. If not, an available index is found to
@@ -1283,7 +1286,7 @@ func (n *Node) syncToAPIServer() error {
12831286
// The PreAllocate value is added here rather than where the CiliumNode
12841287
// resource is created ((*NodeDiscovery).mutateNodeResource() inside
12851288
// pkg/nodediscovery), because mutateNodeResource() does not have
1286-
// access to the ipam.Node object. Since we are in the CiliumNode
1289+
// access to the nodemanager.Node object. Since we are in the CiliumNode
12871290
// update sync loop, we can compute the value.
12881291
if node.Spec.IPAM.PreAllocate == 0 {
12891292
node.Spec.IPAM.PreAllocate = n.ops.GetMinimumAllocatableIPv4()

pkg/ipam/node_manager.go renamed to operator/pkg/ipam/nodemanager/node_manager.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
// Copyright 2017 Lyft, Inc.
55

6-
package ipam
6+
package nodemanager
77

88
import (
99
"context"
@@ -13,6 +13,7 @@ import (
1313

1414
"golang.org/x/sync/semaphore"
1515

16+
"github.com/cilium/cilium/operator/pkg/ipam/allocator"
1617
ipamStats "github.com/cilium/cilium/operator/pkg/ipam/stats"
1718
"github.com/cilium/cilium/pkg/backoff"
1819
"github.com/cilium/cilium/pkg/controller"
@@ -27,15 +28,6 @@ import (
2728

2829
var ipamNodeIntervalControllerGroup = controller.NewGroup("ipam-node-interval-refresh")
2930

30-
// CiliumNodeGetterUpdater defines the interface used to interact with the k8s
31-
// apiserver to retrieve and update the CiliumNode custom resource
32-
type CiliumNodeGetterUpdater interface {
33-
Create(node *v2.CiliumNode) (*v2.CiliumNode, error)
34-
Update(origResource, newResource *v2.CiliumNode) (*v2.CiliumNode, error)
35-
UpdateStatus(origResource, newResource *v2.CiliumNode) (*v2.CiliumNode, error)
36-
Get(name string) (*v2.CiliumNode, error)
37-
}
38-
3931
// NodeOperations is the interface an IPAM implementation must provide in order
4032
// to provide IP allocation for a node. The structure implementing this API
4133
// *must* be aware of the node connected to this implementation. This is
@@ -171,7 +163,7 @@ type NodeManager struct {
171163
mutex lock.RWMutex
172164
nodes nodeMap
173165
instancesAPI AllocationImplementation
174-
k8sAPI CiliumNodeGetterUpdater
166+
k8sAPI allocator.CiliumNodeGetterUpdater
175167
metricsAPI MetricsAPI
176168
parallelWorkers int64
177169
releaseExcessIPs bool
@@ -189,7 +181,7 @@ func (n *NodeManager) ClusterSizeDependantInterval(baseInterval time.Duration) t
189181
}
190182

191183
// NewNodeManager returns a new NodeManager
192-
func NewNodeManager(logger *slog.Logger, instancesAPI AllocationImplementation, k8sAPI CiliumNodeGetterUpdater, metrics MetricsAPI,
184+
func NewNodeManager(logger *slog.Logger, instancesAPI AllocationImplementation, k8sAPI allocator.CiliumNodeGetterUpdater, metrics MetricsAPI,
193185
parallelWorkers int64, releaseExcessIPs bool, excessIPReleaseDelay int, prefixDelegation bool) (*NodeManager, error) {
194186
if parallelWorkers < 1 {
195187
parallelWorkers = 1

pkg/ipam/node_manager_test.go renamed to operator/pkg/ipam/nodemanager/node_manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// Copyright Authors of Cilium
33

4-
package ipam
4+
package nodemanager
55

66
import (
77
"context"

0 commit comments

Comments
 (0)