Skip to content

Commit 4e297fc

Browse files
committed
refactor: AddPoolIfNotExists --> EnsurePool
Signed-off-by: roc <[email protected]>
1 parent ba7e2c4 commit 4e297fc

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

cmd/app/setup_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (i *initCache) Start(ctx context.Context) error {
6464
}
6565
for index := range ppl.Items {
6666
pp := &ppl.Items[index]
67-
portpool.Allocator.AddPoolIfNotExists(pp)
67+
portpool.Allocator.EnsurePool(pp)
6868

6969
lbKeys := []portpool.LBKey{}
7070
for _, lbStatus := range pp.Status.LoadbalancerStatuses {

internal/controller/clbportpool_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func (r *CLBPortPoolReconciler) ensureQuota(ctx context.Context, pool *networkin
318318
// 同步端口池
319319
func (r *CLBPortPoolReconciler) sync(ctx context.Context, pool *networkingv1alpha1.CLBPortPool) (result ctrl.Result, err error) {
320320
// 确保分配器缓存中存在该 port pool,放在最开头,避免同时创建 CLBPortPool 和 CLBBinding 导致分配端口时找不到 pool
321-
portpool.Allocator.AddPoolIfNotExists(pool)
321+
portpool.Allocator.EnsurePool(pool)
322322

323323
// 初始化状态
324324
if pool.Status.State == "" {

internal/portpool/allocator.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,33 @@ func (pa *PortAllocator) EnsureLbIds(name string, lbKeys []LBKey) error {
5959
return nil
6060
}
6161

62-
// AddPoolIfNotExists 添加新的端口池
63-
func (pa *PortAllocator) AddPoolIfNotExists(pool *networkingv1alpha1.CLBPortPool) bool {
62+
// EnsurePool 添加新的端口池
63+
func (pa *PortAllocator) EnsurePool(pool *networkingv1alpha1.CLBPortPool) (added bool) {
6464
pa.mu.Lock()
6565
defer pa.mu.Unlock()
6666

67-
if _, exists := pa.pools[pool.Name]; exists {
68-
return false
69-
}
67+
p, exists := pa.pools[pool.Name]
7068

7169
lbPolicy := constant.LbPolicyRandom
7270
if pool.Spec.LbPolicy != nil {
7371
lbPolicy = *pool.Spec.LbPolicy
7472
}
7573

76-
p := &PortPool{
77-
Name: pool.Name,
78-
LbPolicy: lbPolicy,
79-
cache: make(map[LBKey]map[ProtocolPort]struct{}),
74+
if !exists {
75+
p = &PortPool{
76+
Name: pool.Name,
77+
LbPolicy: lbPolicy,
78+
cache: make(map[LBKey]map[ProtocolPort]struct{}),
79+
}
80+
pa.pools[pool.Name] = p
81+
added = true
82+
} else {
83+
if p.LbPolicy != lbPolicy {
84+
p.LbPolicy = lbPolicy
85+
}
8086
}
81-
pa.pools[pool.Name] = p
82-
return true
87+
88+
return
8389
}
8490

8591
// RemovePool 移除端口池

0 commit comments

Comments
 (0)