@@ -67,6 +67,7 @@ type portAllocated map[int32]bool
67
67
type SlbPlugin struct {
68
68
maxPort int32
69
69
minPort int32
70
+ blockPorts []int32
70
71
cache map [string ]portAllocated
71
72
podAllocate map [string ]string
72
73
mutex sync.RWMutex
@@ -105,30 +106,39 @@ func (s *SlbPlugin) Init(c client.Client, options cloudprovider.CloudProviderOpt
105
106
slbOptions := options .(provideroptions.AlibabaCloudOptions ).SLBOptions
106
107
s .minPort = slbOptions .MinPort
107
108
s .maxPort = slbOptions .MaxPort
109
+ s .blockPorts = slbOptions .BlockPorts
108
110
109
111
svcList := & corev1.ServiceList {}
110
112
err := c .List (ctx , svcList )
111
113
if err != nil {
112
114
return err
113
115
}
114
116
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 )
116
118
log .Infof ("[%s] podAllocate cache complete initialization: %v" , SlbNetwork , s .podAllocate )
117
119
return nil
118
120
}
119
121
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 ) {
121
123
newCache := make (map [string ]portAllocated )
122
124
newPodAllocate := make (map [string ]string )
123
125
for _ , svc := range svcList {
124
126
lbId := svc .Labels [SlbIdLabelKey ]
125
127
if lbId != "" && svc .Spec .Type == corev1 .ServiceTypeLoadBalancer {
128
+ // init cache for that lb
126
129
if newCache [lbId ] == nil {
127
130
newCache [lbId ] = make (portAllocated , maxPort - minPort )
128
131
for i := minPort ; i < maxPort ; i ++ {
129
132
newCache [lbId ][i ] = false
130
133
}
131
134
}
135
+
136
+ // block ports
137
+ for _ , blockPort := range blockPorts {
138
+ newCache [lbId ][blockPort ] = true
139
+ }
140
+
141
+ // fill in cache for that lb
132
142
var ports []int32
133
143
for _ , port := range getPorts (svc .Spec .Ports ) {
134
144
if port <= maxPort && port >= minPort {
@@ -335,10 +345,15 @@ func (s *SlbPlugin) allocate(lbIds []string, num int, nsName string) (string, []
335
345
for i := 0 ; i < num ; i ++ {
336
346
var port int32
337
347
if s .cache [lbId ] == nil {
348
+ // init cache for new lb
338
349
s .cache [lbId ] = make (portAllocated , s .maxPort - s .minPort )
339
350
for i := s .minPort ; i < s .maxPort ; i ++ {
340
351
s.cache [lbId ][i ] = false
341
352
}
353
+ // block ports
354
+ for _ , blockPort := range s .blockPorts {
355
+ s.cache [lbId ][blockPort ] = true
356
+ }
342
357
}
343
358
344
359
for p , allocated := range s .cache [lbId ] {
@@ -371,6 +386,10 @@ func (s *SlbPlugin) deAllocate(nsName string) {
371
386
for _ , port := range ports {
372
387
s.cache [lbId ][port ] = false
373
388
}
389
+ // block ports
390
+ for _ , blockPort := range s .blockPorts {
391
+ s.cache [lbId ][blockPort ] = true
392
+ }
374
393
375
394
delete (s .podAllocate , nsName )
376
395
log .Infof ("pod %s deallocate slb %s ports %v" , nsName , lbId , ports )
0 commit comments