From b841f0d31399614745210488e5ee074906d26bda Mon Sep 17 00:00:00 2001 From: lizhipeng629 <110802158+lizhipeng629@users.noreply.github.com> Date: Tue, 12 Nov 2024 11:11:31 +0800 Subject: [PATCH] fix:add block port in volc engine (#182) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 李志朋 --- .../options/volcenginecloud_options.go | 11 ++++++++-- cloudprovider/volcengine/clb.go | 21 +++++++++++++++++-- cloudprovider/volcengine/clb_test.go | 10 ++++++--- config/manager/config.toml | 5 +++-- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/cloudprovider/options/volcenginecloud_options.go b/cloudprovider/options/volcenginecloud_options.go index 6581584e..2538448d 100644 --- a/cloudprovider/options/volcenginecloud_options.go +++ b/cloudprovider/options/volcenginecloud_options.go @@ -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 } diff --git a/cloudprovider/volcengine/clb.go b/cloudprovider/volcengine/clb.go index 869fa81b..15a60c81 100644 --- a/cloudprovider/volcengine/clb.go +++ b/cloudprovider/volcengine/clb.go @@ -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) diff --git a/cloudprovider/volcengine/clb_test.go b/cloudprovider/volcengine/clb_test.go index 85c4112a..a961b26c 100644 --- a/cloudprovider/volcengine/clb_test.go +++ b/cloudprovider/volcengine/clb_test.go @@ -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 { diff --git a/config/manager/config.toml b/config/manager/config.toml index c493839d..a46dc011 100644 --- a/config/manager/config.toml +++ b/config/manager/config.toml @@ -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