Skip to content

Commit

Permalink
fix:add block port in volc engine (#182)
Browse files Browse the repository at this point in the history
Co-authored-by: 李志朋 <[email protected]>
  • Loading branch information
lizhipeng629 and 李志朋 authored Nov 12, 2024
1 parent 51aad5b commit b841f0d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
11 changes: 9 additions & 2 deletions cloudprovider/options/volcenginecloud_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ type VolcengineOptions struct {
}

type CLBOptions struct {
MaxPort int32 `toml:"max_port"`
MinPort int32 `toml:"min_port"`
MaxPort int32 `toml:"max_port"`
MinPort int32 `toml:"min_port"`
BlockPorts []int32 `toml:"block_ports"`
}

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

for _, blockPort := range clbOptions.BlockPorts {
if blockPort >= clbOptions.MaxPort || blockPort < clbOptions.MinPort {
return false
}
}

if clbOptions.MaxPort > 65535 {
return false
}
Expand Down
21 changes: 19 additions & 2 deletions cloudprovider/volcengine/clb.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type portAllocated map[int32]bool
type ClbPlugin struct {
maxPort int32
minPort int32
blockPorts []int32
cache map[string]portAllocated
podAllocate map[string]string
mutex sync.RWMutex
Expand Down Expand Up @@ -94,18 +95,19 @@ func (c *ClbPlugin) Init(client client.Client, options cloudprovider.CloudProvid
}
c.minPort = clbOptions.CLBOptions.MinPort
c.maxPort = clbOptions.CLBOptions.MaxPort
c.blockPorts = clbOptions.CLBOptions.BlockPorts

svcList := &corev1.ServiceList{}
err := client.List(ctx, svcList)
if err != nil {
return err
}

c.cache, c.podAllocate = initLbCache(svcList.Items, c.minPort, c.maxPort)
c.cache, c.podAllocate = initLbCache(svcList.Items, c.minPort, c.maxPort, c.blockPorts)
return nil
}

func initLbCache(svcList []corev1.Service, minPort, maxPort int32) (map[string]portAllocated, map[string]string) {
func initLbCache(svcList []corev1.Service, minPort, maxPort int32, blockPorts []int32) (map[string]portAllocated, map[string]string) {
newCache := make(map[string]portAllocated)
newPodAllocate := make(map[string]string)
for _, svc := range svcList {
Expand All @@ -117,6 +119,12 @@ func initLbCache(svcList []corev1.Service, minPort, maxPort int32) (map[string]p
newCache[lbId][i] = false
}
}

// block ports
for _, blockPort := range blockPorts {
newCache[lbId][blockPort] = true
}

var ports []int32
for _, port := range getPorts(svc.Spec.Ports) {
if port <= maxPort && port >= minPort {
Expand Down Expand Up @@ -308,6 +316,11 @@ func (c *ClbPlugin) allocate(lbIds []string, num int, nsName string) (string, []
for i := c.minPort; i < c.maxPort; i++ {
c.cache[lbId][i] = false
}

// block ports
for _, blockPort := range c.blockPorts {
c.cache[lbId][blockPort] = true
}
}

for p, allocated := range c.cache[lbId] {
Expand Down Expand Up @@ -340,6 +353,10 @@ func (c *ClbPlugin) deAllocate(nsName string) {
for _, port := range ports {
c.cache[lbId][port] = false
}
// block ports
for _, blockPort := range c.blockPorts {
c.cache[lbId][blockPort] = true
}

delete(c.podAllocate, nsName)
log.Infof("pod %s deallocate clb %s ports %v", nsName, lbId, ports)
Expand Down
10 changes: 7 additions & 3 deletions cloudprovider/volcengine/clb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,21 @@ func TestInitLbCache(t *testing.T) {
svcList []corev1.Service
minPort int32
maxPort int32
blockPorts []int32
cache map[string]portAllocated
podAllocate map[string]string
}{
minPort: 512,
maxPort: 712,
minPort: 512,
maxPort: 712,
blockPorts: []int32{593},
cache: map[string]portAllocated{
"xxx-A": map[int32]bool{
666: true,
593: true,
},
"xxx-B": map[int32]bool{
555: true,
593: true,
},
},
podAllocate: map[string]string{
Expand Down Expand Up @@ -208,7 +212,7 @@ func TestInitLbCache(t *testing.T) {
},
}

actualCache, actualPodAllocate := initLbCache(test.svcList, test.minPort, test.maxPort)
actualCache, actualPodAllocate := initLbCache(test.svcList, test.minPort, test.maxPort, test.blockPorts)
for lb, pa := range test.cache {
for port, isAllocated := range pa {
if actualCache[lb][port] != isAllocated {
Expand Down
5 changes: 3 additions & 2 deletions config/manager/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ block_ports = [1025, 1434, 1068]
[volcengine]
enable = true
[volcengine.clb]
max_port = 700
min_port = 500
max_port = 600
min_port = 550
block_ports = [593]

[aws]
enable = false
Expand Down

0 comments on commit b841f0d

Please sign in to comment.