@@ -22,6 +22,7 @@ import (
2222 "fmt"
2323 "net/http"
2424 "regexp"
25+ "slices"
2526 "strconv"
2627 "strings"
2728
@@ -386,23 +387,22 @@ func nodeAddressForLB(node *corev1.Node, preferredIPFamily corev1.IPFamily) (str
386387 }
387388
388389 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
405399 }
400+ case corev1 .IPv6Protocol :
401+ if netutils .IsIPv6String (addr .Address ) {
402+ return addr .Address , nil
403+ }
404+ default :
405+ return addr .Address , nil
406406 }
407407 }
408408
@@ -558,7 +558,7 @@ func (lbaas *LbaasV2) deleteListeners(lbID string, listenerList []listeners.List
558558func (lbaas * LbaasV2 ) deleteOctaviaListeners (lbID string , listenerList []listeners.Listener , isLBOwner bool , lbName string ) error {
559559 for _ , listener := range listenerList {
560560 // 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 ) {
562562 klog .InfoS ("Deleting listener" , "listenerID" , listener .ID , "lbID" , lbID )
563563
564564 pool , err := openstackutil .GetPoolByListener (lbaas .lb , lbID , listener .ID )
@@ -1069,7 +1069,7 @@ func (lbaas *LbaasV2) ensureOctaviaListener(lbID string, name string, curListene
10691069 updateOpts := listeners.UpdateOpts {}
10701070
10711071 if svcConf .supportLBTags {
1072- if ! cpoutil .Contains (listener .Tags , svcConf .lbName ) {
1072+ if ! slices .Contains (listener .Tags , svcConf .lbName ) {
10731073 var newTags []string
10741074 copy (newTags , listener .Tags )
10751075 newTags = append (newTags , svcConf .lbName )
@@ -1612,7 +1612,7 @@ func (lbaas *LbaasV2) checkListenerPorts(service *corev1.Service, curListenerMap
16121612 if listener , isPresent := curListenerMapping [key ]; isPresent {
16131613 // The listener is used by this Service if LB name is in the tags, or
16141614 // 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 ) {
16161616 continue
16171617 } else {
16181618 return fmt .Errorf ("the listener port %d already exists" , svcPort .Port )
@@ -1721,7 +1721,7 @@ func (lbaas *LbaasV2) ensureOctaviaLoadBalancer(ctx context.Context, clusterName
17211721 sharedCount ++
17221722 }
17231723 }
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 {
17251725 return nil , fmt .Errorf ("load balancer %s already shared with %d Services" , loadbalancer .ID , sharedCount )
17261726 }
17271727
@@ -1825,7 +1825,7 @@ func (lbaas *LbaasV2) ensureOctaviaLoadBalancer(ctx context.Context, clusterName
18251825 // add LB name to load balancer tags.
18261826 if svcConf .supportLBTags {
18271827 lbTags := loadbalancer .Tags
1828- if ! cpoutil .Contains (lbTags , lbName ) {
1828+ if ! slices .Contains (lbTags , lbName ) {
18291829 lbTags = append (lbTags , lbName )
18301830 klog .InfoS ("Updating load balancer tags" , "lbID" , loadbalancer .ID , "tags" , lbTags )
18311831 if err := openstackutil .UpdateLoadBalancerTags (lbaas .lb , loadbalancer .ID , lbTags ); err != nil {
@@ -2029,7 +2029,7 @@ func (lbaas *LbaasV2) deleteLoadBalancer(loadbalancer *loadbalancers.LoadBalance
20292029 Protocol : proto ,
20302030 Port : int (port .Port ),
20312031 }]
2032- if isPresent && cpoutil .Contains (listener .Tags , svcConf .lbName ) {
2032+ if isPresent && slices .Contains (listener .Tags , svcConf .lbName ) {
20332033 listenersToDelete = append (listenersToDelete , * listener )
20342034 }
20352035 }
@@ -2186,32 +2186,33 @@ func (lbaas *LbaasV2) ensureLoadBalancerDeleted(ctx context.Context, clusterName
21862186// If the field is not specified, turn to parse and verify the AnnotationLoadBalancerSourceRangesKey annotation from a service,
21872187// extracting the source ranges to allow, and if not present returns a default (allow-all) value.
21882188func GetLoadBalancerSourceRanges (service * corev1.Service , preferredIPFamily corev1.IPFamily ) (netsets.IPNet , error ) {
2189- var ipnets netsets.IPNet
2190- var err error
21912189 // if SourceRange field is specified, ignore sourceRange annotation
21922190 if len (service .Spec .LoadBalancerSourceRanges ) > 0 {
21932191 specs := service .Spec .LoadBalancerSourceRanges
2194- ipnets , err = netsets .ParseIPNets (specs ... )
2192+ ipnets , err : = netsets .ParseIPNets (specs ... )
21952193
21962194 if err != nil {
21972195 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 )
21982196 }
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
22132208 }
22142209 }
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+
22152216 return ipnets , nil
22162217}
22172218
0 commit comments