Skip to content

Commit 6f3ec94

Browse files
author
李志朋
committed
fix:add block port in volc engine
1 parent 51aad5b commit 6f3ec94

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

cloudprovider/options/volcenginecloud_options.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ type VolcengineOptions struct {
66
}
77

88
type CLBOptions struct {
9-
MaxPort int32 `toml:"max_port"`
10-
MinPort int32 `toml:"min_port"`
9+
MaxPort int32 `toml:"max_port"`
10+
MinPort int32 `toml:"min_port"`
11+
BlockPorts []int32 `toml:"block_ports"`
1112
}
1213

1314
func (v VolcengineOptions) Valid() bool {
1415
clbOptions := v.CLBOptions
1516

17+
for _, blockPort := range clbOptions.BlockPorts {
18+
if blockPort >= clbOptions.MaxPort || blockPort < clbOptions.MinPort {
19+
return false
20+
}
21+
}
22+
1623
if clbOptions.MaxPort > 65535 {
1724
return false
1825
}

cloudprovider/volcengine/clb.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type portAllocated map[int32]bool
6363
type ClbPlugin struct {
6464
maxPort int32
6565
minPort int32
66+
blockPorts []int32
6667
cache map[string]portAllocated
6768
podAllocate map[string]string
6869
mutex sync.RWMutex
@@ -94,18 +95,19 @@ func (c *ClbPlugin) Init(client client.Client, options cloudprovider.CloudProvid
9495
}
9596
c.minPort = clbOptions.CLBOptions.MinPort
9697
c.maxPort = clbOptions.CLBOptions.MaxPort
98+
c.blockPorts = clbOptions.CLBOptions.BlockPorts
9799

98100
svcList := &corev1.ServiceList{}
99101
err := client.List(ctx, svcList)
100102
if err != nil {
101103
return err
102104
}
103105

104-
c.cache, c.podAllocate = initLbCache(svcList.Items, c.minPort, c.maxPort)
106+
c.cache, c.podAllocate = initLbCache(svcList.Items, c.minPort, c.maxPort, c.blockPorts)
105107
return nil
106108
}
107109

108-
func initLbCache(svcList []corev1.Service, minPort, maxPort int32) (map[string]portAllocated, map[string]string) {
110+
func initLbCache(svcList []corev1.Service, minPort, maxPort int32, blockPorts []int32) (map[string]portAllocated, map[string]string) {
109111
newCache := make(map[string]portAllocated)
110112
newPodAllocate := make(map[string]string)
111113
for _, svc := range svcList {
@@ -117,6 +119,12 @@ func initLbCache(svcList []corev1.Service, minPort, maxPort int32) (map[string]p
117119
newCache[lbId][i] = false
118120
}
119121
}
122+
123+
// block ports
124+
for _, blockPort := range blockPorts {
125+
newCache[lbId][blockPort] = true
126+
}
127+
120128
var ports []int32
121129
for _, port := range getPorts(svc.Spec.Ports) {
122130
if port <= maxPort && port >= minPort {
@@ -308,6 +316,11 @@ func (c *ClbPlugin) allocate(lbIds []string, num int, nsName string) (string, []
308316
for i := c.minPort; i < c.maxPort; i++ {
309317
c.cache[lbId][i] = false
310318
}
319+
320+
// block ports
321+
for _, blockPort := range c.blockPorts {
322+
c.cache[lbId][blockPort] = true
323+
}
311324
}
312325

313326
for p, allocated := range c.cache[lbId] {
@@ -340,6 +353,10 @@ func (c *ClbPlugin) deAllocate(nsName string) {
340353
for _, port := range ports {
341354
c.cache[lbId][port] = false
342355
}
356+
// block ports
357+
for _, blockPort := range c.blockPorts {
358+
c.cache[lbId][blockPort] = true
359+
}
343360

344361
delete(c.podAllocate, nsName)
345362
log.Infof("pod %s deallocate clb %s ports %v", nsName, lbId, ports)

cloudprovider/volcengine/clb_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,21 @@ func TestInitLbCache(t *testing.T) {
143143
svcList []corev1.Service
144144
minPort int32
145145
maxPort int32
146+
blockPorts []int32
146147
cache map[string]portAllocated
147148
podAllocate map[string]string
148149
}{
149-
minPort: 512,
150-
maxPort: 712,
150+
minPort: 512,
151+
maxPort: 712,
152+
blockPorts: []int32{593},
151153
cache: map[string]portAllocated{
152154
"xxx-A": map[int32]bool{
153155
666: true,
156+
593: true,
154157
},
155158
"xxx-B": map[int32]bool{
156159
555: true,
160+
593: true,
157161
},
158162
},
159163
podAllocate: map[string]string{
@@ -208,7 +212,7 @@ func TestInitLbCache(t *testing.T) {
208212
},
209213
}
210214

211-
actualCache, actualPodAllocate := initLbCache(test.svcList, test.minPort, test.maxPort)
215+
actualCache, actualPodAllocate := initLbCache(test.svcList, test.minPort, test.maxPort, test.blockPorts)
212216
for lb, pa := range test.cache {
213217
for port, isAllocated := range pa {
214218
if actualCache[lb][port] != isAllocated {

config/manager/config.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ block_ports = [1025, 1434, 1068]
1818
[volcengine]
1919
enable = true
2020
[volcengine.clb]
21-
max_port = 700
22-
min_port = 500
21+
max_port = 600
22+
min_port = 550
23+
block_ports = [593]
2324

2425
[aws]
2526
enable = false

0 commit comments

Comments
 (0)