fix:add block port in volc engine (#182)

Co-authored-by: 李志朋 <lizhipeng.629@bytedance.com>
This commit is contained in:
lizhipeng629 2024-11-12 11:11:31 +08:00 committed by GitHub
parent 51aad5b0a0
commit b841f0d313
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 38 additions and 9 deletions

View File

@ -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
}

View File

@ -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
@ -94,6 +95,7 @@ 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)
@ -101,11 +103,11 @@ func (c *ClbPlugin) Init(client client.Client, options cloudprovider.CloudProvid
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 {
@ -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 {
@ -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] {
@ -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)

View File

@ -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{
@ -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 {

View File

@ -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