Skip to content

Commit 468b2c7

Browse files
authored
enhance: add block ports config for AlibabaCloud LB network models (openkruise#175)
Signed-off-by: ChrisLiu <[email protected]>
1 parent c114781 commit 468b2c7

File tree

5 files changed

+62
-14
lines changed

5 files changed

+62
-14
lines changed

cloudprovider/alibabacloud/nlb.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const (
7070
type NlbPlugin struct {
7171
maxPort int32
7272
minPort int32
73+
blockPorts []int32
7374
cache map[string]portAllocated
7475
podAllocate map[string]string
7576
mutex sync.RWMutex
@@ -106,14 +107,15 @@ func (n *NlbPlugin) Init(c client.Client, options cloudprovider.CloudProviderOpt
106107
slbOptions := options.(provideroptions.AlibabaCloudOptions).NLBOptions
107108
n.minPort = slbOptions.MinPort
108109
n.maxPort = slbOptions.MaxPort
110+
n.blockPorts = slbOptions.BlockPorts
109111

110112
svcList := &corev1.ServiceList{}
111113
err := c.List(ctx, svcList)
112114
if err != nil {
113115
return err
114116
}
115117

116-
n.cache, n.podAllocate = initLbCache(svcList.Items, n.minPort, n.maxPort)
118+
n.cache, n.podAllocate = initLbCache(svcList.Items, n.minPort, n.maxPort, n.blockPorts)
117119
log.Infof("[%s] podAllocate cache complete initialization: %v", NlbNetwork, n.podAllocate)
118120
return nil
119121
}
@@ -385,10 +387,15 @@ func (n *NlbPlugin) allocate(lbIds []string, num int, nsName string) (string, []
385387
for i := 0; i < num; i++ {
386388
var port int32
387389
if n.cache[lbId] == nil {
390+
// init cache for new lb
388391
n.cache[lbId] = make(portAllocated, n.maxPort-n.minPort)
389392
for i := n.minPort; i < n.maxPort; i++ {
390393
n.cache[lbId][i] = false
391394
}
395+
// block ports
396+
for _, blockPort := range n.blockPorts {
397+
n.cache[lbId][blockPort] = true
398+
}
392399
}
393400

394401
for p, allocated := range n.cache[lbId] {
@@ -421,6 +428,10 @@ func (n *NlbPlugin) deAllocate(nsName string) {
421428
for _, port := range ports {
422429
n.cache[lbId][port] = false
423430
}
431+
// block ports
432+
for _, blockPort := range n.blockPorts {
433+
n.cache[lbId][blockPort] = true
434+
}
424435

425436
delete(n.podAllocate, nsName)
426437
log.Infof("pod %s deallocate nlb %s ports %v", nsName, lbId, ports)

cloudprovider/alibabacloud/slb.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type portAllocated map[int32]bool
6767
type SlbPlugin struct {
6868
maxPort int32
6969
minPort int32
70+
blockPorts []int32
7071
cache map[string]portAllocated
7172
podAllocate map[string]string
7273
mutex sync.RWMutex
@@ -105,30 +106,39 @@ func (s *SlbPlugin) Init(c client.Client, options cloudprovider.CloudProviderOpt
105106
slbOptions := options.(provideroptions.AlibabaCloudOptions).SLBOptions
106107
s.minPort = slbOptions.MinPort
107108
s.maxPort = slbOptions.MaxPort
109+
s.blockPorts = slbOptions.BlockPorts
108110

109111
svcList := &corev1.ServiceList{}
110112
err := c.List(ctx, svcList)
111113
if err != nil {
112114
return err
113115
}
114116

115-
s.cache, s.podAllocate = initLbCache(svcList.Items, s.minPort, s.maxPort)
117+
s.cache, s.podAllocate = initLbCache(svcList.Items, s.minPort, s.maxPort, s.blockPorts)
116118
log.Infof("[%s] podAllocate cache complete initialization: %v", SlbNetwork, s.podAllocate)
117119
return nil
118120
}
119121

120-
func initLbCache(svcList []corev1.Service, minPort, maxPort int32) (map[string]portAllocated, map[string]string) {
122+
func initLbCache(svcList []corev1.Service, minPort, maxPort int32, blockPorts []int32) (map[string]portAllocated, map[string]string) {
121123
newCache := make(map[string]portAllocated)
122124
newPodAllocate := make(map[string]string)
123125
for _, svc := range svcList {
124126
lbId := svc.Labels[SlbIdLabelKey]
125127
if lbId != "" && svc.Spec.Type == corev1.ServiceTypeLoadBalancer {
128+
// init cache for that lb
126129
if newCache[lbId] == nil {
127130
newCache[lbId] = make(portAllocated, maxPort-minPort)
128131
for i := minPort; i < maxPort; i++ {
129132
newCache[lbId][i] = false
130133
}
131134
}
135+
136+
// block ports
137+
for _, blockPort := range blockPorts {
138+
newCache[lbId][blockPort] = true
139+
}
140+
141+
// fill in cache for that lb
132142
var ports []int32
133143
for _, port := range getPorts(svc.Spec.Ports) {
134144
if port <= maxPort && port >= minPort {
@@ -335,10 +345,15 @@ func (s *SlbPlugin) allocate(lbIds []string, num int, nsName string) (string, []
335345
for i := 0; i < num; i++ {
336346
var port int32
337347
if s.cache[lbId] == nil {
348+
// init cache for new lb
338349
s.cache[lbId] = make(portAllocated, s.maxPort-s.minPort)
339350
for i := s.minPort; i < s.maxPort; i++ {
340351
s.cache[lbId][i] = false
341352
}
353+
// block ports
354+
for _, blockPort := range s.blockPorts {
355+
s.cache[lbId][blockPort] = true
356+
}
342357
}
343358

344359
for p, allocated := range s.cache[lbId] {
@@ -371,6 +386,10 @@ func (s *SlbPlugin) deAllocate(nsName string) {
371386
for _, port := range ports {
372387
s.cache[lbId][port] = false
373388
}
389+
// block ports
390+
for _, blockPort := range s.blockPorts {
391+
s.cache[lbId][blockPort] = true
392+
}
374393

375394
delete(s.podAllocate, nsName)
376395
log.Infof("pod %s deallocate slb %s ports %v", nsName, lbId, ports)

cloudprovider/alibabacloud/slb_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,21 @@ func TestInitLbCache(t *testing.T) {
201201
svcList []corev1.Service
202202
minPort int32
203203
maxPort int32
204+
blockPorts []int32
204205
cache map[string]portAllocated
205206
podAllocate map[string]string
206207
}{
207-
minPort: 512,
208-
maxPort: 712,
208+
minPort: 512,
209+
maxPort: 712,
210+
blockPorts: []int32{593},
209211
cache: map[string]portAllocated{
210212
"xxx-A": map[int32]bool{
211213
666: true,
214+
593: true,
212215
},
213216
"xxx-B": map[int32]bool{
214217
555: true,
218+
593: true,
215219
},
216220
},
217221
podAllocate: map[string]string{
@@ -266,7 +270,7 @@ func TestInitLbCache(t *testing.T) {
266270
},
267271
}
268272

269-
actualCache, actualPodAllocate := initLbCache(test.svcList, test.minPort, test.maxPort)
273+
actualCache, actualPodAllocate := initLbCache(test.svcList, test.minPort, test.maxPort, test.blockPorts)
270274
for lb, pa := range test.cache {
271275
for port, isAllocated := range pa {
272276
if actualCache[lb][port] != isAllocated {

cloudprovider/options/alibabacloud_options.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,39 @@ type AlibabaCloudOptions struct {
77
}
88

99
type SLBOptions struct {
10-
MaxPort int32 `toml:"max_port"`
11-
MinPort int32 `toml:"min_port"`
10+
MaxPort int32 `toml:"max_port"`
11+
MinPort int32 `toml:"min_port"`
12+
BlockPorts []int32 `toml:"block_ports"`
1213
}
1314

1415
type NLBOptions struct {
15-
MaxPort int32 `toml:"max_port"`
16-
MinPort int32 `toml:"min_port"`
16+
MaxPort int32 `toml:"max_port"`
17+
MinPort int32 `toml:"min_port"`
18+
BlockPorts []int32 `toml:"block_ports"`
1719
}
1820

1921
func (o AlibabaCloudOptions) Valid() bool {
2022
// SLB valid
2123
slbOptions := o.SLBOptions
22-
if slbOptions.MaxPort-slbOptions.MinPort != 200 {
24+
for _, blockPort := range slbOptions.BlockPorts {
25+
if blockPort >= slbOptions.MaxPort || blockPort < slbOptions.MinPort {
26+
return false
27+
}
28+
}
29+
if int(slbOptions.MaxPort-slbOptions.MinPort)-len(slbOptions.BlockPorts) != 200 {
2330
return false
2431
}
2532
if slbOptions.MinPort <= 0 {
2633
return false
2734
}
2835
// NLB valid
2936
nlbOptions := o.NLBOptions
30-
if nlbOptions.MaxPort-nlbOptions.MinPort != 500 {
37+
for _, blockPort := range nlbOptions.BlockPorts {
38+
if blockPort >= nlbOptions.MaxPort || blockPort < nlbOptions.MinPort {
39+
return false
40+
}
41+
}
42+
if int(nlbOptions.MaxPort-nlbOptions.MinPort)-len(nlbOptions.BlockPorts) != 500 {
3143
return false
3244
}
3345
if nlbOptions.MinPort <= 0 {

config/manager/config.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ min_port = 8000
77
[alibabacloud]
88
enable = true
99
[alibabacloud.slb]
10-
max_port = 700
10+
max_port = 701
1111
min_port = 500
12+
block_ports = [593]
1213
[alibabacloud.nlb]
13-
max_port = 1500
14+
max_port = 1503
1415
min_port = 1000
16+
block_ports = [1025, 1434, 1068]
1517

1618
[volcengine]
1719
enable = true

0 commit comments

Comments
 (0)