-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
node_handler.go
157 lines (128 loc) · 4.56 KB
/
node_handler.go
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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package multipool
import (
"context"
"errors"
"fmt"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/cilium/cilium/pkg/controller"
"github.com/cilium/cilium/pkg/ipam"
"github.com/cilium/cilium/pkg/ipam/allocator"
v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/time"
)
type NodeHandler struct {
mutex lock.Mutex
poolManager *PoolAllocator
nodeUpdater ipam.CiliumNodeGetterUpdater
nodesPendingAllocation map[string]*v2.CiliumNode
restoreFinished bool
controllerManager *controller.Manager
controllerErrorRetryBaseDuration time.Duration // only set in unit tests
}
var ipamMultipoolSyncControllerGroup = controller.NewGroup("ipam-multi-pool-sync")
var _ allocator.NodeEventHandler = (*NodeHandler)(nil)
func NewNodeHandler(manager *PoolAllocator, nodeUpdater ipam.CiliumNodeGetterUpdater) *NodeHandler {
return &NodeHandler{
poolManager: manager,
nodeUpdater: nodeUpdater,
nodesPendingAllocation: map[string]*v2.CiliumNode{},
controllerManager: controller.NewManager(),
}
}
func (n *NodeHandler) Upsert(resource *v2.CiliumNode) {
n.mutex.Lock()
defer n.mutex.Unlock()
n.upsertLocked(resource)
}
func (n *NodeHandler) Delete(resource *v2.CiliumNode) {
n.mutex.Lock()
defer n.mutex.Unlock()
err := n.poolManager.ReleaseNode(resource.Name)
if err != nil {
log.WithField(logfields.NodeName, resource.Name).
WithError(err).
Warning("Errors while release node and its CIDRs")
}
delete(n.nodesPendingAllocation, resource.Name)
// Make sure any pending update controller is stopped
n.controllerManager.RemoveController(controllerName(resource.Name))
}
func (n *NodeHandler) Resync(context.Context, time.Time) {
n.mutex.Lock()
defer n.mutex.Unlock()
n.poolManager.RestoreFinished()
for _, cn := range n.nodesPendingAllocation {
delete(n.nodesPendingAllocation, cn.Name)
n.createUpsertController(cn)
}
n.restoreFinished = true
n.nodesPendingAllocation = nil
}
func (n *NodeHandler) upsertLocked(resource *v2.CiliumNode) {
if !n.restoreFinished {
n.nodesPendingAllocation[resource.Name] = resource
_ = n.poolManager.AllocateToNode(resource)
return
}
n.createUpsertController(resource)
}
func (n *NodeHandler) createUpsertController(resource *v2.CiliumNode) {
// This controller serves two purposes:
// 1. It will retry allocations upon failure, e.g. if a pool does not exist yet.
// 2. Will try to synchronize the allocator's state with the CiliumNode CRD in k8s.
refetchNode := false
n.controllerManager.UpdateController(controllerName(resource.Name), controller.ControllerParams{
Group: ipamMultipoolSyncControllerGroup,
ErrorRetryBaseDuration: n.controllerErrorRetryBaseDuration,
DoFunc: func(ctx context.Context) error {
// errorMessage is written to the resource status
errorMessage := ""
var controllerErr error
// If a previous run of the controller failed due to a conflict,
// we need to re-fetch the node to make sure we have the latest version.
if refetchNode {
resource, controllerErr = n.nodeUpdater.Get(resource.Name)
if controllerErr != nil {
return controllerErr
}
refetchNode = false
}
err := n.poolManager.AllocateToNode(resource)
if err != nil {
log.WithField(logfields.NodeName, resource.Name).WithError(err).
Warning("Failed to allocate PodCIDRs to node")
errorMessage = err.Error()
controllerErr = err
}
newResource := resource.DeepCopy()
newResource.Status.IPAM.OperatorStatus.Error = errorMessage
newResource.Spec.IPAM.Pools.Allocated = n.poolManager.AllocatedPools(newResource.Name)
if !newResource.Spec.IPAM.Pools.DeepEqual(&resource.Spec.IPAM.Pools) {
_, err = n.nodeUpdater.Update(resource, newResource)
if err != nil {
controllerErr = errors.Join(controllerErr, fmt.Errorf("failed to update spec: %w", err))
if k8sErrors.IsConflict(err) {
refetchNode = true
}
}
}
if !newResource.Status.IPAM.OperatorStatus.DeepEqual(&resource.Status.IPAM.OperatorStatus) && !refetchNode {
_, err = n.nodeUpdater.UpdateStatus(resource, newResource)
if err != nil {
controllerErr = errors.Join(controllerErr, fmt.Errorf("failed to update status: %w", err))
if k8sErrors.IsConflict(err) {
refetchNode = true
}
}
}
return controllerErr
},
})
}
func controllerName(nodeName string) string {
return "ipam-multi-pool-sync-" + nodeName
}