99 "time"
1010
1111 stackitconfig "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/config"
12- "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer"
12+ //nolint:staticcheck // Temporary workaround: v2api OpenAPI generator currently misses enum constants; fixed in next NVP.
13+ lbLegacy "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer"
14+ loadbalancer "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api"
1315 corev1 "k8s.io/api/core/v1"
1416
1517 "github.com/stackitcloud/cloud-provider-stackit/pkg/cmp"
@@ -239,36 +241,38 @@ func getPlanID(service *corev1.Service) (planID *string, msgs []string, err erro
239241// lbSpecFromService returns a load balancer specification in the form of a create payload matching the specification of the service, nodes and network.
240242// The property name will be empty and must be set by the caller to produce a valid payload for the API.
241243// An error is returned if the service has invalid options.
242- func lbSpecFromService ( //nolint:funlen,gocyclo // It is long but not complex.
244+ //
245+ //nolint:gocyclo,funlen,staticcheck // Temporary workaround: v2api OpenAPI generator currently misses enum constants; fixed in next NVP.
246+ func lbSpecFromService (
243247 service * corev1.Service ,
244248 nodes []* corev1.Node ,
245249 opts stackitconfig.LoadBalancerOpts ,
246250 observability * loadbalancer.LoadbalancerOptionObservability ,
247251) (* loadbalancer.CreateLoadBalancerPayload , []Event , error ) {
248252 lb := & loadbalancer.CreateLoadBalancerPayload {
249253 Options : & loadbalancer.LoadBalancerOptions {},
250- Networks : & []loadbalancer.Network {
254+ Networks : []loadbalancer.Network {
251255 {
252- Role : new (loadbalancer .NETWORKROLE_LISTENERS_AND_TARGETS ),
256+ Role : new (string (lbLegacy .NETWORKROLE_LISTENERS_AND_TARGETS ) ),
253257 NetworkId : & opts .NetworkID ,
254258 },
255259 },
256260 }
257261
258262 if listenerNetwork := service .Annotations [listenerNetworkAnnotation ]; listenerNetwork != "" {
259- lb .Networks = & []loadbalancer.Network {
263+ lb .Networks = []loadbalancer.Network {
260264 {
261- Role : new (loadbalancer .NETWORKROLE_TARGETS ),
265+ Role : new (string (lbLegacy .NETWORKROLE_TARGETS ) ),
262266 NetworkId : & opts .NetworkID ,
263267 }, {
264- Role : new (loadbalancer .NETWORKROLE_LISTENERS ),
268+ Role : new (string (lbLegacy .NETWORKROLE_LISTENERS ) ),
265269 NetworkId : & listenerNetwork ,
266270 },
267271 }
268272 } else {
269- lb .Networks = & []loadbalancer.Network {
273+ lb .Networks = []loadbalancer.Network {
270274 {
271- Role : new (loadbalancer .NETWORKROLE_LISTENERS_AND_TARGETS ),
275+ Role : new (string (lbLegacy .NETWORKROLE_LISTENERS_AND_TARGETS ) ),
272276 NetworkId : & opts .NetworkID ,
273277 },
274278 }
@@ -493,22 +497,22 @@ func lbSpecFromService( //nolint:funlen,gocyclo // It is long but not complex.
493497 name = fmt .Sprintf ("port-%s-%d" , strings .ToLower (string (port .Protocol )), port .Port )
494498 }
495499
496- var protocol loadbalancer .ListenerProtocol
500+ var protocol lbLegacy .ListenerProtocol
497501 var tcpOptions * loadbalancer.OptionsTCP
498502 var udpOptions * loadbalancer.OptionsUDP
499503
500504 switch port .Protocol {
501505 case corev1 .ProtocolTCP :
502506 if proxyProtocolEnableForPort (tcpProxyProtocolEnabled , tcpProxyProtocolPortFilter , port .Port ) {
503- protocol = loadbalancer .LISTENERPROTOCOL_TCP_PROXY
507+ protocol = lbLegacy .LISTENERPROTOCOL_TCP_PROXY
504508 } else {
505- protocol = loadbalancer .LISTENERPROTOCOL_TCP
509+ protocol = lbLegacy .LISTENERPROTOCOL_TCP
506510 }
507511 tcpOptions = & loadbalancer.OptionsTCP {
508512 IdleTimeout : new (fmt.Sprintf ("%.0fs" , tcpIdleTimeout .Seconds ())),
509513 }
510514 case corev1 .ProtocolUDP :
511- protocol = loadbalancer .LISTENERPROTOCOL_UDP
515+ protocol = lbLegacy .LISTENERPROTOCOL_UDP
512516 udpOptions = & loadbalancer.OptionsUDP {
513517 IdleTimeout : new (fmt.Sprintf ("%.0fs" , udpIdleTimeout .Seconds ())),
514518 }
@@ -518,33 +522,33 @@ func lbSpecFromService( //nolint:funlen,gocyclo // It is long but not complex.
518522
519523 listeners = append (listeners , loadbalancer.Listener {
520524 DisplayName : & name ,
521- Port : new (int64 ( port.Port ) ),
525+ Port : new (port.Port ),
522526 TargetPool : & name ,
523- Protocol : new (protocol ),
527+ Protocol : new (string ( protocol ) ),
524528 Tcp : tcpOptions ,
525529 Udp : udpOptions ,
526530 })
527531
528532 targetPools = append (targetPools , loadbalancer.TargetPool {
529533 Name : & name ,
530- TargetPort : new (int64 ( port.NodePort ) ),
531- Targets : & targets ,
534+ TargetPort : new (port.NodePort ),
535+ Targets : targets ,
532536 SessionPersistence : & loadbalancer.SessionPersistence {
533537 UseSourceIpAddress : new (useSourceIP ),
534538 },
535539 })
536540 }
537- lb .Listeners = & listeners
538- lb .TargetPools = & targetPools
541+ lb .Listeners = listeners
542+ lb .TargetPools = targetPools
539543
540544 lb .Options .AccessControl = & loadbalancer.LoadbalancerOptionAccessControl {}
541545 // For backwards-compatibility, the spec takes precedence over the annotation.
542546 if sourceRanges , found := service .Annotations [yawolLoadBalancerSourceRangesAnnotation ]; found {
543547 r := strings .Split (sourceRanges , "," )
544- lb .Options .AccessControl .AllowedSourceRanges = & r
548+ lb .Options .AccessControl .AllowedSourceRanges = r
545549 }
546550 if len (service .Spec .LoadBalancerSourceRanges ) > 0 {
547- lb .Options .AccessControl .AllowedSourceRanges = & service .Spec .LoadBalancerSourceRanges
551+ lb .Options .AccessControl .AllowedSourceRanges = service .Spec .LoadBalancerSourceRanges
548552 }
549553
550554 if event := checkUnsupportedAnnotations (service ); event != nil {
@@ -587,6 +591,8 @@ type resultImmutableChanged struct {
587591// compareLBwithSpec checks whether the load balancer fulfills the specification.
588592// If immutableChanged is not nil then spec differs from lb such that an update will fail.
589593// Otherwise, fulfills will indicate whether an update is necessary.
594+ //
595+ //nolint:staticcheck // Temporary workaround: v2api OpenAPI generator currently misses enum constants; fixed in next NVP.
590596func compareLBwithSpec (lb * loadbalancer.LoadBalancer , spec * loadbalancer.CreateLoadBalancerPayload ) (fulfills bool , immutableChanged * resultImmutableChanged ) { //nolint:gocyclo,funlen,lll // It is long but not complex.
591597 // If a mutable property has changed we must still check the rest of the object because if there is an immutable change it must always be returned.
592598 fulfills = true
@@ -637,11 +643,11 @@ func compareLBwithSpec(lb *loadbalancer.LoadBalancer, spec *loadbalancer.CreateL
637643 return false , & resultImmutableChanged {field : ".options.ephemeralAddress" , annotation : externalIPAnnotation }
638644 }
639645
640- if cmp . LenSlicePtr (lb .Listeners ) != cmp . LenSlicePtr (spec .Listeners ) {
646+ if len (lb .Listeners ) != len (spec .Listeners ) {
641647 fulfills = false
642- } else if lb . Listeners != nil && spec . Listeners != nil {
643- for i , x := range * lb .Listeners {
644- y := ( * spec .Listeners ) [i ]
648+ } else {
649+ for i , x := range lb .Listeners {
650+ y := spec .Listeners [i ]
645651 if ! cmp .PtrValEqual (x .DisplayName , y .DisplayName ) {
646652 fulfills = false
647653 }
@@ -654,40 +660,38 @@ func compareLBwithSpec(lb *loadbalancer.LoadBalancer, spec *loadbalancer.CreateL
654660 if ! cmp .PtrValEqual (x .TargetPool , y .TargetPool ) {
655661 fulfills = false
656662 }
657- if (cmp .UnpackPtr (x .Protocol ) == loadbalancer .LISTENERPROTOCOL_TCP || cmp .UnpackPtr (x .Protocol ) == loadbalancer .LISTENERPROTOCOL_TCP_PROXY ) &&
663+ if (cmp .UnpackPtr (x .Protocol ) == string ( lbLegacy .LISTENERPROTOCOL_TCP ) || cmp .UnpackPtr (x .Protocol ) == string ( lbLegacy .LISTENERPROTOCOL_TCP_PROXY ) ) &&
658664 ! cmp .PtrValEqualFn (x .Tcp , y .Tcp , func (a , b loadbalancer.OptionsTCP ) bool {
659665 return cmp .PtrValEqual (a .IdleTimeout , b .IdleTimeout )
660666 }) {
661667 fulfills = false
662668 }
663- if cmp .UnpackPtr (x .Protocol ) == loadbalancer .LISTENERPROTOCOL_UDP && ! cmp .PtrValEqualFn (x .Udp , y .Udp , func (a , b loadbalancer.OptionsUDP ) bool {
669+ if cmp .UnpackPtr (x .Protocol ) == string ( lbLegacy .LISTENERPROTOCOL_UDP ) && ! cmp .PtrValEqualFn (x .Udp , y .Udp , func (a , b loadbalancer.OptionsUDP ) bool {
664670 return cmp .PtrValEqual (a .IdleTimeout , b .IdleTimeout )
665671 }) {
666672 fulfills = false
667673 }
668674 }
669675 }
670676
671- if cmp . LenSlicePtr (lb .Networks ) != cmp . LenSlicePtr (spec .Networks ) {
677+ if len (lb .Networks ) != len (spec .Networks ) {
672678 return false , & resultImmutableChanged {field : "len(.networks)" , annotation : listenerNetworkAnnotation }
673679 }
674- if cmp .LenSlicePtr (lb .Networks ) > 0 {
675- for i , x := range * lb .Networks {
676- y := (* spec .Networks )[i ]
677- if ! cmp .PtrValEqual (x .NetworkId , y .NetworkId ) {
678- return false , & resultImmutableChanged {field : fmt .Sprintf (".networks[%d].networkId" , i ), annotation : listenerNetworkAnnotation }
679- }
680- if ! cmp .PtrValEqual (x .Role , y .Role ) {
681- return false , & resultImmutableChanged {field : fmt .Sprintf (".networks[%d].role" , i ), annotation : listenerNetworkAnnotation }
682- }
680+ for i , x := range lb .Networks {
681+ y := spec .Networks [i ]
682+ if ! cmp .PtrValEqual (x .NetworkId , y .NetworkId ) {
683+ return false , & resultImmutableChanged {field : fmt .Sprintf (".networks[%d].networkId" , i ), annotation : listenerNetworkAnnotation }
684+ }
685+ if ! cmp .PtrValEqual (x .Role , y .Role ) {
686+ return false , & resultImmutableChanged {field : fmt .Sprintf (".networks[%d].role" , i ), annotation : listenerNetworkAnnotation }
683687 }
684688 }
685689
686- if cmp . LenSlicePtr (lb .TargetPools ) != cmp . LenSlicePtr (spec .TargetPools ) {
690+ if len (lb .TargetPools ) != len (spec .TargetPools ) {
687691 fulfills = false
688- } else if lb . TargetPools != nil && spec . TargetPools != nil {
689- for i , x := range * lb .TargetPools {
690- y := ( * spec .TargetPools ) [i ]
692+ } else {
693+ for i , x := range lb .TargetPools {
694+ y := spec .TargetPools [i ]
691695 if ! cmp .PtrValEqual (x .Name , y .Name ) {
692696 fulfills = false
693697 }
@@ -717,13 +721,7 @@ func compareLBwithSpec(lb *loadbalancer.LoadBalancer, spec *loadbalancer.CreateL
717721 }) {
718722 fulfills = false
719723 }
720- if x .Targets == nil || y .Targets == nil {
721- // At this point one pointer is nil.
722- // We consider nil pointer to be equal to a nil slice and an empty slice.
723- if cmp .LenSlicePtr (x .Targets ) != cmp .LenSlicePtr (y .Targets ) {
724- fulfills = false
725- }
726- } else if ! cmp .SliceEqualUnordered (* x .Targets , * y .Targets , func (a , b loadbalancer.Target ) bool {
724+ if ! cmp .SliceEqualUnordered (x .Targets , y .Targets , func (a , b loadbalancer.Target ) bool {
727725 if ! cmp .PtrValEqual (a .DisplayName , b .DisplayName ) {
728726 return false
729727 }
@@ -745,8 +743,8 @@ func compareLBwithSpec(lb *loadbalancer.LoadBalancer, spec *loadbalancer.CreateL
745743 }
746744
747745 if ! cmp .SliceEqual (
748- cmp .UnpackPtr (cmp .UnpackPtr (cmp . UnpackPtr ( lb .Options ).AccessControl ).AllowedSourceRanges ) ,
749- cmp .UnpackPtr (cmp .UnpackPtr (cmp . UnpackPtr ( spec .Options ).AccessControl ).AllowedSourceRanges ) ,
746+ cmp .UnpackPtr (cmp .UnpackPtr (lb .Options ).AccessControl ).AllowedSourceRanges ,
747+ cmp .UnpackPtr (cmp .UnpackPtr (spec .Options ).AccessControl ).AllowedSourceRanges ,
750748 ) {
751749 fulfills = false
752750 }
0 commit comments