@@ -22,6 +22,7 @@ import (
22
22
"fmt"
23
23
"net/http"
24
24
"regexp"
25
+ "slices"
25
26
"strconv"
26
27
"strings"
27
28
@@ -386,23 +387,22 @@ func nodeAddressForLB(node *corev1.Node, preferredIPFamily corev1.IPFamily) (str
386
387
}
387
388
388
389
allowedAddrTypes := []corev1.NodeAddressType {corev1 .NodeInternalIP , corev1 .NodeExternalIP }
389
-
390
- for _ , allowedAddrType := range allowedAddrTypes {
391
- for _ , addr := range addrs {
392
- if addr .Type == allowedAddrType {
393
- switch preferredIPFamily {
394
- case corev1 .IPv4Protocol :
395
- if netutils .IsIPv4String (addr .Address ) {
396
- return addr .Address , nil
397
- }
398
- case corev1 .IPv6Protocol :
399
- if netutils .IsIPv6String (addr .Address ) {
400
- return addr .Address , nil
401
- }
402
- default :
403
- return addr .Address , nil
404
- }
390
+ for _ , addr := range addrs {
391
+ if ! slices .Contains (allowedAddrTypes , addr .Type ) {
392
+ // Skip the address type that is not allowed
393
+ continue
394
+ }
395
+ switch preferredIPFamily {
396
+ case corev1 .IPv4Protocol :
397
+ if netutils .IsIPv4String (addr .Address ) {
398
+ return addr .Address , nil
405
399
}
400
+ case corev1 .IPv6Protocol :
401
+ if netutils .IsIPv6String (addr .Address ) {
402
+ return addr .Address , nil
403
+ }
404
+ default :
405
+ return addr .Address , nil
406
406
}
407
407
}
408
408
@@ -558,7 +558,7 @@ func (lbaas *LbaasV2) deleteListeners(lbID string, listenerList []listeners.List
558
558
func (lbaas * LbaasV2 ) deleteOctaviaListeners (lbID string , listenerList []listeners.Listener , isLBOwner bool , lbName string ) error {
559
559
for _ , listener := range listenerList {
560
560
// If the listener was created by this Service before or after supporting shared LB.
561
- if (isLBOwner && len (listener .Tags ) == 0 ) || cpoutil .Contains (listener .Tags , lbName ) {
561
+ if (isLBOwner && len (listener .Tags ) == 0 ) || slices .Contains (listener .Tags , lbName ) {
562
562
klog .InfoS ("Deleting listener" , "listenerID" , listener .ID , "lbID" , lbID )
563
563
564
564
pool , err := openstackutil .GetPoolByListener (lbaas .lb , lbID , listener .ID )
@@ -1069,7 +1069,7 @@ func (lbaas *LbaasV2) ensureOctaviaListener(lbID string, name string, curListene
1069
1069
updateOpts := listeners.UpdateOpts {}
1070
1070
1071
1071
if svcConf .supportLBTags {
1072
- if ! cpoutil .Contains (listener .Tags , svcConf .lbName ) {
1072
+ if ! slices .Contains (listener .Tags , svcConf .lbName ) {
1073
1073
var newTags []string
1074
1074
copy (newTags , listener .Tags )
1075
1075
newTags = append (newTags , svcConf .lbName )
@@ -1612,7 +1612,7 @@ func (lbaas *LbaasV2) checkListenerPorts(service *corev1.Service, curListenerMap
1612
1612
if listener , isPresent := curListenerMapping [key ]; isPresent {
1613
1613
// The listener is used by this Service if LB name is in the tags, or
1614
1614
// the listener was created by this Service.
1615
- if cpoutil .Contains (listener .Tags , lbName ) || (len (listener .Tags ) == 0 && isLBOwner ) {
1615
+ if slices .Contains (listener .Tags , lbName ) || (len (listener .Tags ) == 0 && isLBOwner ) {
1616
1616
continue
1617
1617
} else {
1618
1618
return fmt .Errorf ("the listener port %d already exists" , svcPort .Port )
@@ -1721,7 +1721,7 @@ func (lbaas *LbaasV2) ensureOctaviaLoadBalancer(ctx context.Context, clusterName
1721
1721
sharedCount ++
1722
1722
}
1723
1723
}
1724
- if ! isLBOwner && ! cpoutil .Contains (loadbalancer .Tags , lbName ) && sharedCount + 1 > lbaas .opts .MaxSharedLB {
1724
+ if ! isLBOwner && ! slices .Contains (loadbalancer .Tags , lbName ) && sharedCount + 1 > lbaas .opts .MaxSharedLB {
1725
1725
return nil , fmt .Errorf ("load balancer %s already shared with %d Services" , loadbalancer .ID , sharedCount )
1726
1726
}
1727
1727
@@ -1825,7 +1825,7 @@ func (lbaas *LbaasV2) ensureOctaviaLoadBalancer(ctx context.Context, clusterName
1825
1825
// add LB name to load balancer tags.
1826
1826
if svcConf .supportLBTags {
1827
1827
lbTags := loadbalancer .Tags
1828
- if ! cpoutil .Contains (lbTags , lbName ) {
1828
+ if ! slices .Contains (lbTags , lbName ) {
1829
1829
lbTags = append (lbTags , lbName )
1830
1830
klog .InfoS ("Updating load balancer tags" , "lbID" , loadbalancer .ID , "tags" , lbTags )
1831
1831
if err := openstackutil .UpdateLoadBalancerTags (lbaas .lb , loadbalancer .ID , lbTags ); err != nil {
@@ -2029,7 +2029,7 @@ func (lbaas *LbaasV2) deleteLoadBalancer(loadbalancer *loadbalancers.LoadBalance
2029
2029
Protocol : proto ,
2030
2030
Port : int (port .Port ),
2031
2031
}]
2032
- if isPresent && cpoutil .Contains (listener .Tags , svcConf .lbName ) {
2032
+ if isPresent && slices .Contains (listener .Tags , svcConf .lbName ) {
2033
2033
listenersToDelete = append (listenersToDelete , * listener )
2034
2034
}
2035
2035
}
@@ -2186,32 +2186,33 @@ func (lbaas *LbaasV2) ensureLoadBalancerDeleted(ctx context.Context, clusterName
2186
2186
// If the field is not specified, turn to parse and verify the AnnotationLoadBalancerSourceRangesKey annotation from a service,
2187
2187
// extracting the source ranges to allow, and if not present returns a default (allow-all) value.
2188
2188
func GetLoadBalancerSourceRanges (service * corev1.Service , preferredIPFamily corev1.IPFamily ) (netsets.IPNet , error ) {
2189
- var ipnets netsets.IPNet
2190
- var err error
2191
2189
// if SourceRange field is specified, ignore sourceRange annotation
2192
2190
if len (service .Spec .LoadBalancerSourceRanges ) > 0 {
2193
2191
specs := service .Spec .LoadBalancerSourceRanges
2194
- ipnets , err = netsets .ParseIPNets (specs ... )
2192
+ ipnets , err : = netsets .ParseIPNets (specs ... )
2195
2193
2196
2194
if err != nil {
2197
2195
return nil , fmt .Errorf ("service.Spec.LoadBalancerSourceRanges: %v is not valid. Expecting a list of IP ranges. For example, 10.0.0.0/24. Error msg: %v" , specs , err )
2198
2196
}
2199
- } else {
2200
- val := service .Annotations [corev1 .AnnotationLoadBalancerSourceRangesKey ]
2201
- val = strings .TrimSpace (val )
2202
- if val == "" {
2203
- if preferredIPFamily == corev1 .IPv6Protocol {
2204
- val = defaultLoadBalancerSourceRangesIPv6
2205
- } else {
2206
- val = defaultLoadBalancerSourceRangesIPv4
2207
- }
2208
- }
2209
- specs := strings .Split (val , "," )
2210
- ipnets , err = netsets .ParseIPNets (specs ... )
2211
- if err != nil {
2212
- return nil , fmt .Errorf ("%s: %s is not valid. Expecting a comma-separated list of source IP ranges. For example, 10.0.0.0/24,192.168.2.0/24" , corev1 .AnnotationLoadBalancerSourceRangesKey , val )
2197
+
2198
+ return ipnets , nil
2199
+ }
2200
+
2201
+ val := service .Annotations [corev1 .AnnotationLoadBalancerSourceRangesKey ]
2202
+ val = strings .TrimSpace (val )
2203
+ if val == "" {
2204
+ if preferredIPFamily == corev1 .IPv6Protocol {
2205
+ val = defaultLoadBalancerSourceRangesIPv6
2206
+ } else {
2207
+ val = defaultLoadBalancerSourceRangesIPv4
2213
2208
}
2214
2209
}
2210
+ specs := strings .Split (val , "," )
2211
+ ipnets , err := netsets .ParseIPNets (specs ... )
2212
+ if err != nil {
2213
+ return nil , fmt .Errorf ("%s: %s is not valid. Expecting a comma-separated list of source IP ranges. For example, 10.0.0.0/24,192.168.2.0/24" , corev1 .AnnotationLoadBalancerSourceRangesKey , val )
2214
+ }
2215
+
2215
2216
return ipnets , nil
2216
2217
}
2217
2218
0 commit comments