Skip to content

Commit acab990

Browse files
committed
remove lb from port pool status if lb is existed lb and been remove
manually from existedLbIds Signed-off-by: roc <[email protected]>
1 parent 7d84bc1 commit acab990

File tree

5 files changed

+61
-19
lines changed

5 files changed

+61
-19
lines changed

internal/controller/clbbinding.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,24 +237,37 @@ func (r *CLBBindingReconciler[T]) createListener(ctx context.Context, bd clbbind
237237
// 对账单个监听器
238238
func (r *CLBBindingReconciler[T]) ensureListener(ctx context.Context, bd clbbinding.CLBBinding, binding *networkingv1alpha1.PortBindingStatus) (*networkingv1alpha1.PortBindingStatus, error) {
239239
log.FromContext(ctx).V(10).Info("ensureListener", "binding", binding)
240+
// 如果 lb 已被移除,且当前还未绑定成功,则移除该端口绑定,等待重新分配端口(配错 lb导致一直无法绑定成功,更正后,可以触发重新分配以便能够成功绑定)
241+
if bd.GetStatus().State != networkingv1alpha1.CLBBindingStateBound && !portpool.Allocator.IsLbExists(binding.Pool, portpool.NewLBKeyFromBinding(binding)) {
242+
r.Recorder.Eventf(bd.GetObject(), corev1.EventTypeNormal, "PortBindingRemoved", "lb %q not exists, remove port binding (lbPort:%s protocol:%s)", binding.LoadbalancerId, binding.LoadbalancerPort, binding.Protocol)
243+
return nil, nil
244+
}
245+
246+
removeReason := ""
247+
removeMsg := ""
248+
240249
// 确保关联的 pool 无误
241250
pool := portpool.Allocator.GetPool(binding.Pool)
242-
if pool == nil { // 端口池不存在,移除 binding 并并记录事件
243-
r.Recorder.Event(bd.GetObject(), corev1.EventTypeWarning, "PortPoolDeleted", fmt.Sprintf("port pool has been deleted (%s/%s/%d)", binding.Pool, binding.LoadbalancerId, binding.LoadbalancerPort))
251+
if pool == nil {
252+
// 如果端口池被删,且当前还未绑定成功,则移除该端口绑定
253+
if bd.GetStatus().State != networkingv1alpha1.CLBBindingStateBound {
254+
removeReason = "PortPoolDeleted"
255+
removeMsg = "port pool has been deleted"
256+
}
257+
} else {
258+
// 如果 lb 已被移除,且当前还未绑定成功,则移除该端口绑定。等待重新分配端口(配错 lb导致一直无法绑定成功,更正后,可以触发重新分配以便能够成功绑定)
259+
if bd.GetStatus().State != networkingv1alpha1.CLBBindingStateBound && !pool.IsLbExists(portpool.NewLBKeyFromBinding(binding)) {
260+
removeReason = "CLBDeleted"
261+
removeMsg = "clb has been removed"
262+
}
263+
}
264+
if removeMsg != "" {
265+
r.Recorder.Eventf(bd.GetObject(), corev1.EventTypeWarning, removeReason, "%s (%s/%s/%d/%s)", removeMsg, binding.Pool, binding.LoadbalancerId, binding.LoadbalancerPort, binding.Protocol)
244266
// 确保监听器被清理
245267
if err := r.cleanupPortBinding(ctx, binding); err != nil {
246268
return binding, errors.WithStack(err)
247269
}
248270
return nil, nil
249-
} else {
250-
if !pool.IsLbExists(portpool.NewLBKeyFromBinding(binding)) { // lb 被删除,移除 binding 并记录到事件
251-
r.Recorder.Event(bd.GetObject(), corev1.EventTypeWarning, "CLBDeleted", fmt.Sprintf("clb has been deleted (%s/%s/%d)", binding.Pool, binding.LoadbalancerId, binding.LoadbalancerPort))
252-
// 确保监听器被清理
253-
if err := r.cleanupPortBinding(ctx, binding); err != nil {
254-
return binding, errors.WithStack(err)
255-
}
256-
return nil, nil
257-
}
258271
}
259272

260273
// 没有监听器 ID,尝试直接新建(不调查接口,加速大规模场景扩容速度)

internal/controller/clbportpool_controller.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ func (r *CLBPortPoolReconciler) ensureLbStatus(ctx context.Context, pool *networ
171171
insufficientPorts := true
172172
autoCreatedLbNum := uint16(0)
173173

174+
existedLbIds := make(map[string]struct{})
175+
for _, lbId := range pool.Spec.ExsistedLoadBalancerIDs {
176+
existedLbIds[lbId] = struct{}{}
177+
}
178+
174179
// 构造当前 lb status 列表
175180
for _, lbStatus := range pool.Status.LoadbalancerStatuses {
176181
lbId := lbStatus.LoadbalancerID
@@ -187,6 +192,13 @@ func (r *CLBPortPoolReconciler) ensureLbStatus(ctx context.Context, pool *networ
187192
}
188193
if util.GetValue(lbStatus.AutoCreated) {
189194
autoCreatedLbNum++
195+
} else { // 已有的 lb,如果被手动移除,可能是 lb 有误(比如错误的 vpc),也将其从status和分配器中移除
196+
if _, exists := existedLbIds[lbId]; !exists {
197+
if portpool.Allocator.RemoveLB(pool.Name, portpool.NewLBKey(lbId, pool.GetRegion())) {
198+
r.Recorder.Eventf(pool, corev1.EventTypeNormal, "RemoveLoadBalancer", "remove existed clb %s from pool", lbId)
199+
}
200+
continue
201+
}
190202
}
191203
allocatableLBs = append(allocatableLBs, lbKey)
192204
} else {

internal/portpool/allocator.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ func (pa *PortAllocator) Release(pool string, lbKey LBKey, port ProtocolPort) bo
117117
return false
118118
}
119119

120+
func (pa *PortAllocator) IsLbExists(pool string, lbKey LBKey) bool {
121+
if pp := pa.GetPool(pool); pp != nil {
122+
return pp.IsLbExists(lbKey)
123+
}
124+
return false
125+
}
126+
127+
func (pa *PortAllocator) RemoveLB(pool string, lbKey LBKey) bool {
128+
if pp := pa.GetPool(pool); pp != nil {
129+
return pp.RemoveLB(lbKey)
130+
}
131+
return false
132+
}
133+
120134
var Allocator = NewPortAllocator()
121135

122136
func (pa *PortAllocator) MarkAllocated(poolName string, lbKey LBKey, port uint16, endPort *uint16, protocol string) {

internal/portpool/portpool.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ func (pp *PortPool) AllocatePort(ctx context.Context, quota int64, ports ...Prot
153153
return nil, quotaExceeded
154154
}
155155

156+
func (pp *PortPool) RemoveLB(lbKey LBKey) bool {
157+
pp.mu.Lock()
158+
defer pp.mu.Unlock()
159+
_, exists := pp.cache[lbKey]
160+
if !exists {
161+
return false
162+
}
163+
delete(pp.cache, lbKey)
164+
return true
165+
}
166+
156167
// 释放已分配的端口
157168
func (pp *PortPool) ReleasePort(lbKey LBKey, port ProtocolPort) bool {
158169
pp.mu.Lock()

pkg/util/retry.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,8 @@ import (
44
apierrors "k8s.io/apimachinery/pkg/api/errors"
55
"k8s.io/apimachinery/pkg/util/wait"
66
"k8s.io/client-go/util/retry"
7-
ctrl "sigs.k8s.io/controller-runtime"
87
)
98

10-
func RequeueIfConflict(err error) (ctrl.Result, error) {
11-
if apierrors.IsConflict(err) {
12-
return ctrl.Result{Requeue: true}, nil
13-
}
14-
return ctrl.Result{}, err
15-
}
16-
179
func RetryIfPossible(fn func() error) error {
1810
return RetryOnErrors(
1911
retry.DefaultBackoff,

0 commit comments

Comments
 (0)