Skip to content

Commit ef2d734

Browse files
committed
fix edge case: make sure port retry allocate after clb scaling
Signed-off-by: roc <roc@imroc.cc>
1 parent 1e7543a commit ef2d734

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

internal/controller/clbbinding.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ func (r *CLBBindingReconciler[T]) sync(ctx context.Context, bd T) (result ctrl.R
6464
if err := r.ensureCLBBinding(ctx, bd); err != nil {
6565
errCause := errors.Cause(err)
6666
switch errCause {
67+
case ErrLBNotFoundInPool: // lb 不存在于端口池中,通常是 lb 扩容了但还未将 lb 信息写入端口池的 status 中,重新入队重试
68+
result.RequeueAfter = 20 * time.Microsecond
69+
return result, nil
6770
case portpool.ErrPortPoolNotAllocatable: // 端口池不可用
6871
if err := r.ensureState(ctx, bd, networkingv1alpha1.CLBBindingStatePortPoolNotAllocatable); err != nil {
6972
return result, errors.WithStack(err)
@@ -437,6 +440,8 @@ func NewLBStatusGetter(client client.Client) *LBStatusGetter {
437440
}
438441
}
439442

443+
var ErrLBNotFoundInPool = errors.New("loadbalancer not found in port pool")
444+
440445
func (g *LBStatusGetter) Get(ctx context.Context, poolName, lbId string) (*networkingv1alpha1.LoadBalancerStatus, error) {
441446
key := lbStatusKey(poolName, lbId)
442447
status, exists := g.cache[key]
@@ -455,7 +460,7 @@ func (g *LBStatusGetter) Get(ctx context.Context, poolName, lbId string) (*netwo
455460
if exists {
456461
return status, nil
457462
}
458-
return nil, errors.Errorf("loadbalancer %s not found in pool %s", lbId, poolName)
463+
return nil, errors.Wrapf(ErrLBNotFoundInPool, "loadbalancer %s not found in port pool %s", lbId, poolName)
459464
}
460465

461466
// 确保映射的结果写到 backend 资源的注解上

internal/controller/clbportpool_controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ func (r *CLBPortPoolReconciler) ensureLbStatus(ctx context.Context, pool *networ
203203
lbStatuses = append(lbStatuses, lbStatus)
204204
}
205205

206+
// 确保所有可分配的 lb 在分配器缓存中
207+
if err := portpool.Allocator.EnsureLbIds(pool.Name, allocatableLBs); err != nil {
208+
return errors.WithStack(err)
209+
}
210+
206211
// 如果 status 有变更就更新下
207212
if quota != pool.Status.Quota || !reflect.DeepEqual(lbStatuses, pool.Status.LoadbalancerStatuses) {
208213
pool.Status.LoadbalancerStatuses = lbStatuses
@@ -212,11 +217,6 @@ func (r *CLBPortPoolReconciler) ensureLbStatus(ctx context.Context, pool *networ
212217
}
213218
}
214219

215-
// 确保所有可分配的 lb 在分配器缓存中
216-
if err := portpool.Allocator.EnsureLbIds(pool.Name, allocatableLBs); err != nil {
217-
return errors.WithStack(err)
218-
}
219-
220220
if insufficientPorts { // 可分配端口不足,尝试扩容 clb
221221
if pool.Spec.AutoCreate != nil && pool.Spec.AutoCreate.Enabled { // 必须启用了 clb 自动创建
222222
if pool.Spec.AutoCreate.MaxLoadBalancers == nil || autoCreatedLbNum < *pool.Spec.AutoCreate.MaxLoadBalancers { // 满足可以自动创建clb的条件:没有限制自动创建的 clb 数量,或者自动创建的 clb 数量未达到限制

internal/controller/pod_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23+
"time"
2324

2425
"sigs.k8s.io/controller-runtime/pkg/controller"
2526
"sigs.k8s.io/controller-runtime/pkg/log"
@@ -87,6 +88,10 @@ func (r *PodReconciler) sync(ctx context.Context, pod *corev1.Pod) (result ctrl.
8788
}
8889
result, err = r.syncCLBHostPortMapping(ctx, pod)
8990
if err != nil {
91+
if errors.Is(err, ErrLBNotFoundInPool) { // lb 不存在于端口池中,通常是 lb 扩容了但还未将 lb 信息写入端口池的 status 中,重新入队重试
92+
result.RequeueAfter = 20 * time.Microsecond
93+
return result, nil
94+
}
9095
return result, errors.WithStack(err)
9196
}
9297
}

0 commit comments

Comments
 (0)