Skip to content

Commit 744f833

Browse files
committed
add lbBlacklist to port pool
Signed-off-by: roc <[email protected]>
1 parent 3b42029 commit 744f833

File tree

6 files changed

+81
-23
lines changed

6 files changed

+81
-23
lines changed

api/v1alpha1/clbportpool_types.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,34 @@ type CLBPortPoolSpec struct {
5555
// +kubebuilder:validation:Enum=Uniform;InOrder;Random
5656
// +optional
5757
LbPolicy *string `json:"lbPolicy,omitempty"`
58-
// 已有负载均衡器ID列表
58+
// CLB 黑名单,负载均衡实例 ID 的数组,用于禁止某些 CLB 实例被分配端口,可动态追加和移除。
59+
// 如果发现某个 CLB 被 DDoS 攻击或其他原因导致不可用,可将该 CLB 的实例 ID 加入到黑名单中,
60+
// 避免后续端口分配使用该 CLB。
61+
// +optional
62+
LbBlacklist []string `json:"lbBlacklist,omitempty"`
63+
// 已有负载均衡器实例 ID 列表,可动态追加。
64+
// 该列表的负载均衡器将会被端口池用于分配端口映射。
65+
// +optional
5966
ExsistedLoadBalancerIDs []string `json:"exsistedLoadBalancerIDs,omitempty"`
60-
// 自动创建配置
67+
// 自动创建的配置,如果启用,则当端口池中负载均衡器可用监听器数量不足时会自动创建新的负载
68+
// 均衡器来补充可分配监听器数量。
69+
// +optional
6170
AutoCreate *AutoCreateConfig `json:"autoCreate,omitempty"`
6271
}
6372

6473
func (pool *CLBPortPool) GetRegion() string {
6574
return util.GetRegionFromPtr(pool.Spec.Region)
6675
}
6776

68-
// AutoCreateConfig 定义自动创建CLB的配置
77+
// AutoCreateConfig 定义自动创建 CLB 的配置
6978
type AutoCreateConfig struct {
7079
// 是否启用自动创建
7180
Enabled bool `json:"enabled"`
7281
// 自动创建的最大负载均衡器数量
82+
// +optional
7383
MaxLoadBalancers *uint16 `json:"maxLoadBalancers,omitempty"`
7484
// 自动创建参数
85+
// +optional
7586
Parameters *CreateLBParameters `json:"parameters,omitempty"`
7687
}
7788

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

charts/tke-extend-network-controller/templates/networking.cloud.tencent.com_clbportpools.yaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ spec:
4747
description: CLBPortPoolSpec defines the desired state of CLBPortPool.
4848
properties:
4949
autoCreate:
50-
description: 自动创建配置
50+
description: |-
51+
自动创建的配置,如果启用,则当端口池中负载均衡器可用监听器数量不足时会自动创建新的负载
52+
均衡器来补充可分配监听器数量。
5153
properties:
5254
enabled:
5355
description: 是否启用自动创建
@@ -203,7 +205,17 @@ spec:
203205
- message: Value is immutable
204206
rule: self == oldSelf
205207
exsistedLoadBalancerIDs:
206-
description: 已有负载均衡器ID列表
208+
description: |-
209+
已有负载均衡器实例 ID 列表,可动态追加。
210+
该列表的负载均衡器将会被端口池用于分配端口映射。
211+
items:
212+
type: string
213+
type: array
214+
lbBlacklist:
215+
description: |-
216+
CLB 黑名单,负载均衡实例 ID 的数组,用于禁止某些 CLB 实例被分配端口,可动态追加和移除。
217+
如果发现某个 CLB 被 DDoS 攻击或其他原因导致不可用,可将该 CLB 的实例 ID 加入到黑名单中,
218+
避免后续端口分配使用该 CLB。
207219
items:
208220
type: string
209221
type: array

config/crd/bases/networking.cloud.tencent.com_clbportpools.yaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ spec:
4747
description: CLBPortPoolSpec defines the desired state of CLBPortPool.
4848
properties:
4949
autoCreate:
50-
description: 自动创建配置
50+
description: |-
51+
自动创建的配置,如果启用,则当端口池中负载均衡器可用监听器数量不足时会自动创建新的负载
52+
均衡器来补充可分配监听器数量。
5153
properties:
5254
enabled:
5355
description: 是否启用自动创建
@@ -203,7 +205,17 @@ spec:
203205
- message: Value is immutable
204206
rule: self == oldSelf
205207
exsistedLoadBalancerIDs:
206-
description: 已有负载均衡器ID列表
208+
description: |-
209+
已有负载均衡器实例 ID 列表,可动态追加。
210+
该列表的负载均衡器将会被端口池用于分配端口映射。
211+
items:
212+
type: string
213+
type: array
214+
lbBlacklist:
215+
description: |-
216+
CLB 黑名单,负载均衡实例 ID 的数组,用于禁止某些 CLB 实例被分配端口,可动态追加和移除。
217+
如果发现某个 CLB 被 DDoS 攻击或其他原因导致不可用,可将该 CLB 的实例 ID 加入到黑名单中,
218+
避免后续端口分配使用该 CLB。
207219
items:
208220
type: string
209221
type: array

internal/portpool/allocator.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package portpool
33
import (
44
"context"
55
"fmt"
6+
"reflect"
67
"sync"
78

89
networkingv1alpha1 "github.com/imroc/tke-extend-network-controller/api/v1alpha1"
@@ -66,22 +67,31 @@ func (pa *PortAllocator) EnsurePool(pool *networkingv1alpha1.CLBPortPool) (added
6667

6768
p, exists := pa.pools[pool.Name]
6869

69-
lbPolicy := constant.LbPolicyRandom
70-
if pool.Spec.LbPolicy != nil {
71-
lbPolicy = *pool.Spec.LbPolicy
72-
}
73-
7470
if !exists {
7571
p = &PortPool{
76-
Name: pool.Name,
77-
LbPolicy: lbPolicy,
78-
cache: make(map[LBKey]map[ProtocolPort]struct{}),
72+
Name: pool.Name,
73+
LbBlacklist: make(map[LBKey]struct{}),
74+
cache: make(map[LBKey]map[ProtocolPort]struct{}),
7975
}
8076
pa.pools[pool.Name] = p
8177
added = true
82-
} else {
83-
if p.LbPolicy != lbPolicy {
84-
p.LbPolicy = lbPolicy
78+
}
79+
80+
lbPolicy := constant.LbPolicyRandom
81+
if pool.Spec.LbPolicy != nil {
82+
lbPolicy = *pool.Spec.LbPolicy
83+
}
84+
if p.LbPolicy != lbPolicy {
85+
p.LbPolicy = lbPolicy
86+
}
87+
if !reflect.DeepEqual(p.lbBlacklist, pool.Spec.LbBlacklist) {
88+
p.lbBlacklist = pool.Spec.LbBlacklist
89+
p.LbBlacklist = make(map[LBKey]struct{})
90+
for _, lbId := range pool.Spec.LbBlacklist {
91+
lbKey := LBKey{LbId: lbId, Region: pool.GetRegion()}
92+
if _, exists := p.LbBlacklist[lbKey]; !exists {
93+
p.LbBlacklist[lbKey] = struct{}{}
94+
}
8595
}
8696
}
8797
return

internal/portpool/portpool.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,13 @@ func NewLBKeyFromBinding(binding *networkingv1alpha1.PortBindingStatus) LBKey {
106106

107107
// PortPool 管理单个端口池的状态
108108
type PortPool struct {
109-
Name string
110-
LbPolicy string
111-
mu sync.Mutex
112-
cache map[LBKey]map[ProtocolPort]struct{}
113-
lbList []LBKey
109+
Name string
110+
LbPolicy string
111+
LbBlacklist map[LBKey]struct{}
112+
lbBlacklist []string
113+
mu sync.Mutex
114+
cache map[LBKey]map[ProtocolPort]struct{}
115+
lbList []LBKey
114116
}
115117

116118
func (pp *PortPool) getCache() iter.Seq2[LBKey, map[ProtocolPort]struct{}] {
@@ -123,12 +125,18 @@ func (pp *PortPool) getCache() iter.Seq2[LBKey, map[ProtocolPort]struct{}] {
123125
})
124126
}
125127
for _, lbKey := range pp.lbList {
128+
if _, exists := pp.LbBlacklist[lbKey]; exists { // 若 lb 在黑名单中,则跳过
129+
continue
130+
}
126131
if !yield(lbKey, pp.cache[lbKey]) { // 若 yield 返回 false 则中断
127132
return
128133
}
129134
}
130135
default: // 默认用 Random,按 map 的 key 顺序遍历(golang map 的 key 是无序的,每次遍历顺序随机)
131136
for lbKey, allocated := range pp.cache {
137+
if _, exists := pp.LbBlacklist[lbKey]; exists { // 若 lb 在黑名单中,则跳过
138+
continue
139+
}
132140
if !yield(lbKey, allocated) { // 若 yield 返回 false 则中断
133141
return
134142
}

0 commit comments

Comments
 (0)