Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ verify-image-stackit-csi-plugin: image-stackit-csi-plugin
@docker run -v ./tools/csi-deps-check.sh:/tools/csi-deps-check.sh --entrypoint=/tools/csi-deps-check.sh $(REGISTRY)/$(REPO)/stackit-csi-plugin-dev:$(VERSION)

# generate mock types for the following services (space-separated list)
MOCK_SERVICES := iaas loadbalancer
MOCK_SERVICES := iaas

.PHONY: mocks
mocks: $(MOCKGEN)
Expand All @@ -136,6 +136,9 @@ mocks: $(MOCKGEN)
INTERFACES=`go doc -all github.com/stackitcloud/stackit-sdk-go/services/$$service | grep '^type Api.* interface' | sed -n 's/^type \(.*\) interface.*/\1/p' | paste -sd,`,DefaultApi; \
$(MOCKGEN) -destination ./pkg/mock/$$service/$$service.go -package $$service github.com/stackitcloud/stackit-sdk-go/services/$$service $$INTERFACES; \
done

@$(MOCKGEN) -destination ./pkg/mock/loadbalancer/loadbalancer.go -package loadbalancer github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api DefaultAPI

@$(MOCKGEN) -destination ./pkg/stackit/iaas_mock.go -package stackit ./pkg/stackit IaasClient
@$(MOCKGEN) -destination ./pkg/stackit/loadbalancer_mock.go -package stackit ./pkg/stackit LoadbalancerClient
@$(MOCKGEN) -destination ./pkg/stackit/server_mock.go -package stackit ./pkg/stackit NodeClient
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ require (
github.com/prometheus/client_golang v1.23.2
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
github.com/stackitcloud/stackit-sdk-go/core v0.23.0
github.com/stackitcloud/stackit-sdk-go/core v0.24.0
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.3.5
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.8.0
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.11.1
go.uber.org/mock v0.6.0
golang.org/x/sync v0.20.0
golang.org/x/sys v0.42.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stackitcloud/cloud-provider v0.35.1-ske-1 h1:Oo71mALP7hh50wAeGOogng2sAsi7AUre4177WS2n9NE=
github.com/stackitcloud/cloud-provider v0.35.1-ske-1/go.mod h1:zGF/i9YuBODKxj7szGMMIz4DRnjsDy5mg2JU+XbbULA=
github.com/stackitcloud/stackit-sdk-go/core v0.23.0 h1:zPrOhf3Xe47rKRs1fg/AqKYUiJJRYjdcv+3qsS50mEs=
github.com/stackitcloud/stackit-sdk-go/core v0.23.0/go.mod h1:osMglDby4csGZ5sIfhNyYq1bS1TxIdPY88+skE/kkmI=
github.com/stackitcloud/stackit-sdk-go/core v0.24.0 h1:kHCcezCJ5OGSP7RRuGOxD5rF2wejpkEiRr/OdvNcuPQ=
github.com/stackitcloud/stackit-sdk-go/core v0.24.0/go.mod h1:osMglDby4csGZ5sIfhNyYq1bS1TxIdPY88+skE/kkmI=
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.3.5 h1:W57+XRa8wTLsi5CV9Tqa7mGgt/PvlRM//RurXSmvII8=
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.3.5/go.mod h1:lTWjW57eAq1bwfM6nsNinhoBr3MHFW/GaFasdAsYfDM=
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.8.0 h1:DxrN85V738CRLynu6MULQHO+OXyYnkhVPgoZKULfFIs=
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.8.0/go.mod h1:ClPE4TOM1FeaJiwTXvApq4gWaSgTLq6nU3PPHAIQDN4=
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.11.1 h1:8hLLCP4n9w3K3dqJaKF1PnMpX4qjR8UQL3gDr80hgDI=
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.11.1/go.mod h1:vqTjmecQ4+oVWzeo9tCOUZ3TrpIIzM+C0apC7cdAD0Y=
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.18.5 h1:MZ5aTO2NQ1Jecmi67ByGskve5nKXHl91fE+z+vFjxt4=
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.18.5/go.mod h1:CJLmdqWvJm5/3+lXPDKu8k4WXs2UG8euGoqQX5xE79k=
github.com/stoewer/go-strcase v1.3.1 h1:iS0MdW+kVTxgMoE1LAZyMiYJFKlOzLooE4MxjirtkAs=
Expand Down
31 changes: 15 additions & 16 deletions pkg/ccm/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"time"

stackitconfig "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/config"
"github.com/stackitcloud/stackit-sdk-go/services/loadbalancer"
loadbalancer "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api"
lbwait "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api/wait"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
cloudprovider "k8s.io/cloud-provider"
Expand Down Expand Up @@ -158,7 +159,7 @@ func (l *LoadBalancer) EnsureLoadBalancer( //nolint:gocyclo // not really comple
PrivateAddress: spec.PrivateAddress,
Region: spec.Region,
Labels: spec.Labels,
Status: loadbalancer.UpdateLoadBalancerPayloadGetStatusAttributeType(spec.Status),
Status: spec.Status,
TargetPools: spec.TargetPools,
Version: spec.Version,
}
Expand All @@ -178,10 +179,10 @@ func (l *LoadBalancer) EnsureLoadBalancer( //nolint:gocyclo // not really comple
}
}

if *lb.Status == loadbalancer.LOADBALANCERSTATUS_ERROR {
if lb.Status != nil && *lb.Status == lbwait.LOADBALANCERSTATUS_ERROR {
return nil, fmt.Errorf("the load balancer is in an error state")
}
if *lb.Status != loadbalancer.LOADBALANCERSTATUS_READY {
if lb.Status == nil || *lb.Status != lbwait.LOADBALANCERSTATUS_READY {
return nil, api.NewRetryError("waiting for load balancer to become ready. This error is normal while the load balancer starts.", retryDuration)
}

Expand Down Expand Up @@ -221,7 +222,7 @@ func (l *LoadBalancer) createLoadBalancer(ctx context.Context, clusterName strin
return nil, createErr
}

if lb.Status == nil || *lb.Status != loadbalancer.LOADBALANCERSTATUS_READY {
if lb.Status == nil || *lb.Status != lbwait.LOADBALANCERSTATUS_READY {
return nil, api.NewRetryError("waiting for load balancer to become ready. This error is normal while the load balancer starts.", retryDuration)
}

Expand All @@ -245,7 +246,7 @@ func (l *LoadBalancer) UpdateLoadBalancer(ctx context.Context, clusterName strin
l.recorder.Event(service, event.Type, event.Reason, event.Message)
}

for _, pool := range *spec.TargetPools {
for _, pool := range spec.TargetPools {
err := l.client.UpdateTargetPool(ctx, l.projectID, l.GetLoadBalancerName(ctx, clusterName, service), *pool.Name, loadbalancer.UpdateTargetPoolPayload(pool))
if err != nil {
return fmt.Errorf("failed to update target pool %q: %w", *pool.Name, err)
Expand Down Expand Up @@ -274,16 +275,16 @@ func (l *LoadBalancer) EnsureLoadBalancerDeleted(
return nil
case err != nil:
return err
case lb.Status != nil && *lb.Status == loadbalancer.LOADBALANCERSTATUS_TERMINATING:
case lb.Status != nil && *lb.Status == lbwait.LOADBALANCERSTATUS_TERMINATING:
return nil
}

credentialsRef := getMetricsRemoteWriteRef(lb)
if credentialsRef != nil {
// The load balancer is updated to remove the credentials reference and hence enable their deletion.
for i := range *lb.Listeners {
for i := range lb.Listeners {
// Name is an output only field.
(*lb.Listeners)[i].Name = nil
lb.Listeners[i].Name = nil
}
externalAddress := lb.ExternalAddress
if cmp.UnpackPtr(cmp.UnpackPtr(lb.Options).EphemeralAddress) {
Expand Down Expand Up @@ -403,13 +404,11 @@ func (l *LoadBalancer) cleanUpCredentials(ctx context.Context, name string) erro
if err != nil {
return fmt.Errorf("failed to list credentials: %w", err)
}
if res.Credentials != nil {
for _, credentials := range *res.Credentials {
if credentials.DisplayName != nil && *credentials.DisplayName == name {
err = l.client.DeleteCredentials(ctx, l.projectID, *credentials.CredentialsRef)
if err != nil {
return fmt.Errorf("failed to delete credentials %q: %w", *credentials.CredentialsRef, err)
}
for _, credentials := range res.Credentials {
if credentials.DisplayName != nil && *credentials.DisplayName == name {
err = l.client.DeleteCredentials(ctx, l.projectID, *credentials.CredentialsRef)
if err != nil {
return fmt.Errorf("failed to delete credentials %q: %w", *credentials.CredentialsRef, err)
}
}
}
Expand Down
98 changes: 48 additions & 50 deletions pkg/ccm/loadbalancer_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"time"

stackitconfig "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/config"
"github.com/stackitcloud/stackit-sdk-go/services/loadbalancer"
//nolint:staticcheck // Temporary workaround: v2api OpenAPI generator currently misses enum constants; fixed in next NVP.
lbLegacy "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer"
loadbalancer "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api"
corev1 "k8s.io/api/core/v1"

"github.com/stackitcloud/cloud-provider-stackit/pkg/cmp"
Expand Down Expand Up @@ -239,36 +241,38 @@ func getPlanID(service *corev1.Service) (planID *string, msgs []string, err erro
// lbSpecFromService returns a load balancer specification in the form of a create payload matching the specification of the service, nodes and network.
// The property name will be empty and must be set by the caller to produce a valid payload for the API.
// An error is returned if the service has invalid options.
func lbSpecFromService( //nolint:funlen,gocyclo // It is long but not complex.
//
//nolint:gocyclo,funlen,staticcheck // Temporary workaround: v2api OpenAPI generator currently misses enum constants; fixed in next NVP.
func lbSpecFromService(
service *corev1.Service,
nodes []*corev1.Node,
opts stackitconfig.LoadBalancerOpts,
observability *loadbalancer.LoadbalancerOptionObservability,
) (*loadbalancer.CreateLoadBalancerPayload, []Event, error) {
lb := &loadbalancer.CreateLoadBalancerPayload{
Options: &loadbalancer.LoadBalancerOptions{},
Networks: &[]loadbalancer.Network{
Networks: []loadbalancer.Network{
{
Role: new(loadbalancer.NETWORKROLE_LISTENERS_AND_TARGETS),
Role: new(string(lbLegacy.NETWORKROLE_LISTENERS_AND_TARGETS)),
NetworkId: &opts.NetworkID,
},
},
}

if listenerNetwork := service.Annotations[listenerNetworkAnnotation]; listenerNetwork != "" {
lb.Networks = &[]loadbalancer.Network{
lb.Networks = []loadbalancer.Network{
{
Role: new(loadbalancer.NETWORKROLE_TARGETS),
Role: new(string(lbLegacy.NETWORKROLE_TARGETS)),
NetworkId: &opts.NetworkID,
}, {
Role: new(loadbalancer.NETWORKROLE_LISTENERS),
Role: new(string(lbLegacy.NETWORKROLE_LISTENERS)),
NetworkId: &listenerNetwork,
},
}
} else {
lb.Networks = &[]loadbalancer.Network{
lb.Networks = []loadbalancer.Network{
{
Role: new(loadbalancer.NETWORKROLE_LISTENERS_AND_TARGETS),
Role: new(string(lbLegacy.NETWORKROLE_LISTENERS_AND_TARGETS)),
NetworkId: &opts.NetworkID,
},
}
Expand Down Expand Up @@ -493,22 +497,22 @@ func lbSpecFromService( //nolint:funlen,gocyclo // It is long but not complex.
name = fmt.Sprintf("port-%s-%d", strings.ToLower(string(port.Protocol)), port.Port)
}

var protocol loadbalancer.ListenerProtocol
var protocol lbLegacy.ListenerProtocol
var tcpOptions *loadbalancer.OptionsTCP
var udpOptions *loadbalancer.OptionsUDP

switch port.Protocol {
case corev1.ProtocolTCP:
if proxyProtocolEnableForPort(tcpProxyProtocolEnabled, tcpProxyProtocolPortFilter, port.Port) {
protocol = loadbalancer.LISTENERPROTOCOL_TCP_PROXY
protocol = lbLegacy.LISTENERPROTOCOL_TCP_PROXY
} else {
protocol = loadbalancer.LISTENERPROTOCOL_TCP
protocol = lbLegacy.LISTENERPROTOCOL_TCP
}
tcpOptions = &loadbalancer.OptionsTCP{
IdleTimeout: new(fmt.Sprintf("%.0fs", tcpIdleTimeout.Seconds())),
}
case corev1.ProtocolUDP:
protocol = loadbalancer.LISTENERPROTOCOL_UDP
protocol = lbLegacy.LISTENERPROTOCOL_UDP
udpOptions = &loadbalancer.OptionsUDP{
IdleTimeout: new(fmt.Sprintf("%.0fs", udpIdleTimeout.Seconds())),
}
Expand All @@ -518,33 +522,33 @@ func lbSpecFromService( //nolint:funlen,gocyclo // It is long but not complex.

listeners = append(listeners, loadbalancer.Listener{
DisplayName: &name,
Port: new(int64(port.Port)),
Port: new(port.Port),
TargetPool: &name,
Protocol: new(protocol),
Protocol: new(string(protocol)),
Tcp: tcpOptions,
Udp: udpOptions,
})

targetPools = append(targetPools, loadbalancer.TargetPool{
Name: &name,
TargetPort: new(int64(port.NodePort)),
Targets: &targets,
TargetPort: new(port.NodePort),
Targets: targets,
SessionPersistence: &loadbalancer.SessionPersistence{
UseSourceIpAddress: new(useSourceIP),
},
})
}
lb.Listeners = &listeners
lb.TargetPools = &targetPools
lb.Listeners = listeners
lb.TargetPools = targetPools

lb.Options.AccessControl = &loadbalancer.LoadbalancerOptionAccessControl{}
// For backwards-compatibility, the spec takes precedence over the annotation.
if sourceRanges, found := service.Annotations[yawolLoadBalancerSourceRangesAnnotation]; found {
r := strings.Split(sourceRanges, ",")
lb.Options.AccessControl.AllowedSourceRanges = &r
lb.Options.AccessControl.AllowedSourceRanges = r
}
if len(service.Spec.LoadBalancerSourceRanges) > 0 {
lb.Options.AccessControl.AllowedSourceRanges = &service.Spec.LoadBalancerSourceRanges
lb.Options.AccessControl.AllowedSourceRanges = service.Spec.LoadBalancerSourceRanges
}

if event := checkUnsupportedAnnotations(service); event != nil {
Expand Down Expand Up @@ -587,6 +591,8 @@ type resultImmutableChanged struct {
// compareLBwithSpec checks whether the load balancer fulfills the specification.
// If immutableChanged is not nil then spec differs from lb such that an update will fail.
// Otherwise, fulfills will indicate whether an update is necessary.
//
//nolint:staticcheck // Temporary workaround: v2api OpenAPI generator currently misses enum constants; fixed in next NVP.
func compareLBwithSpec(lb *loadbalancer.LoadBalancer, spec *loadbalancer.CreateLoadBalancerPayload) (fulfills bool, immutableChanged *resultImmutableChanged) { //nolint:gocyclo,funlen,lll // It is long but not complex.
// 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.
fulfills = true
Expand Down Expand Up @@ -637,11 +643,11 @@ func compareLBwithSpec(lb *loadbalancer.LoadBalancer, spec *loadbalancer.CreateL
return false, &resultImmutableChanged{field: ".options.ephemeralAddress", annotation: externalIPAnnotation}
}

if cmp.LenSlicePtr(lb.Listeners) != cmp.LenSlicePtr(spec.Listeners) {
if len(lb.Listeners) != len(spec.Listeners) {
fulfills = false
} else if lb.Listeners != nil && spec.Listeners != nil {
for i, x := range *lb.Listeners {
y := (*spec.Listeners)[i]
} else {
for i, x := range lb.Listeners {
y := spec.Listeners[i]
if !cmp.PtrValEqual(x.DisplayName, y.DisplayName) {
fulfills = false
}
Expand All @@ -654,40 +660,38 @@ func compareLBwithSpec(lb *loadbalancer.LoadBalancer, spec *loadbalancer.CreateL
if !cmp.PtrValEqual(x.TargetPool, y.TargetPool) {
fulfills = false
}
if (cmp.UnpackPtr(x.Protocol) == loadbalancer.LISTENERPROTOCOL_TCP || cmp.UnpackPtr(x.Protocol) == loadbalancer.LISTENERPROTOCOL_TCP_PROXY) &&
if (cmp.UnpackPtr(x.Protocol) == string(lbLegacy.LISTENERPROTOCOL_TCP) || cmp.UnpackPtr(x.Protocol) == string(lbLegacy.LISTENERPROTOCOL_TCP_PROXY)) &&
!cmp.PtrValEqualFn(x.Tcp, y.Tcp, func(a, b loadbalancer.OptionsTCP) bool {
return cmp.PtrValEqual(a.IdleTimeout, b.IdleTimeout)
}) {
fulfills = false
}
if cmp.UnpackPtr(x.Protocol) == loadbalancer.LISTENERPROTOCOL_UDP && !cmp.PtrValEqualFn(x.Udp, y.Udp, func(a, b loadbalancer.OptionsUDP) bool {
if cmp.UnpackPtr(x.Protocol) == string(lbLegacy.LISTENERPROTOCOL_UDP) && !cmp.PtrValEqualFn(x.Udp, y.Udp, func(a, b loadbalancer.OptionsUDP) bool {
return cmp.PtrValEqual(a.IdleTimeout, b.IdleTimeout)
}) {
fulfills = false
}
}
}

if cmp.LenSlicePtr(lb.Networks) != cmp.LenSlicePtr(spec.Networks) {
if len(lb.Networks) != len(spec.Networks) {
return false, &resultImmutableChanged{field: "len(.networks)", annotation: listenerNetworkAnnotation}
}
if cmp.LenSlicePtr(lb.Networks) > 0 {
for i, x := range *lb.Networks {
y := (*spec.Networks)[i]
if !cmp.PtrValEqual(x.NetworkId, y.NetworkId) {
return false, &resultImmutableChanged{field: fmt.Sprintf(".networks[%d].networkId", i), annotation: listenerNetworkAnnotation}
}
if !cmp.PtrValEqual(x.Role, y.Role) {
return false, &resultImmutableChanged{field: fmt.Sprintf(".networks[%d].role", i), annotation: listenerNetworkAnnotation}
}
for i, x := range lb.Networks {
y := spec.Networks[i]
if !cmp.PtrValEqual(x.NetworkId, y.NetworkId) {
return false, &resultImmutableChanged{field: fmt.Sprintf(".networks[%d].networkId", i), annotation: listenerNetworkAnnotation}
}
if !cmp.PtrValEqual(x.Role, y.Role) {
return false, &resultImmutableChanged{field: fmt.Sprintf(".networks[%d].role", i), annotation: listenerNetworkAnnotation}
}
}

if cmp.LenSlicePtr(lb.TargetPools) != cmp.LenSlicePtr(spec.TargetPools) {
if len(lb.TargetPools) != len(spec.TargetPools) {
fulfills = false
} else if lb.TargetPools != nil && spec.TargetPools != nil {
for i, x := range *lb.TargetPools {
y := (*spec.TargetPools)[i]
} else {
for i, x := range lb.TargetPools {
y := spec.TargetPools[i]
if !cmp.PtrValEqual(x.Name, y.Name) {
fulfills = false
}
Expand Down Expand Up @@ -717,13 +721,7 @@ func compareLBwithSpec(lb *loadbalancer.LoadBalancer, spec *loadbalancer.CreateL
}) {
fulfills = false
}
if x.Targets == nil || y.Targets == nil {
// At this point one pointer is nil.
// We consider nil pointer to be equal to a nil slice and an empty slice.
if cmp.LenSlicePtr(x.Targets) != cmp.LenSlicePtr(y.Targets) {
fulfills = false
}
} else if !cmp.SliceEqualUnordered(*x.Targets, *y.Targets, func(a, b loadbalancer.Target) bool {
if !cmp.SliceEqualUnordered(x.Targets, y.Targets, func(a, b loadbalancer.Target) bool {
if !cmp.PtrValEqual(a.DisplayName, b.DisplayName) {
return false
}
Expand All @@ -745,8 +743,8 @@ func compareLBwithSpec(lb *loadbalancer.LoadBalancer, spec *loadbalancer.CreateL
}

if !cmp.SliceEqual(
cmp.UnpackPtr(cmp.UnpackPtr(cmp.UnpackPtr(lb.Options).AccessControl).AllowedSourceRanges),
cmp.UnpackPtr(cmp.UnpackPtr(cmp.UnpackPtr(spec.Options).AccessControl).AllowedSourceRanges),
cmp.UnpackPtr(cmp.UnpackPtr(lb.Options).AccessControl).AllowedSourceRanges,
cmp.UnpackPtr(cmp.UnpackPtr(spec.Options).AccessControl).AllowedSourceRanges,
) {
fulfills = false
}
Expand Down
Loading