Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit 9225598

Browse files
committed
speedup failoverGroups
Signed-off-by: Jan Jansen <jan.jansen@gdata.de>
1 parent df34d2b commit 9225598

File tree

3 files changed

+52
-56
lines changed

3 files changed

+52
-56
lines changed

internal/controller/ionoscloudmachine_controller.go

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -439,34 +439,16 @@ func (r *IONOSCloudMachineReconciler) reconcileFailoverGroups(ctx *context.Machi
439439
if err != nil {
440440
return err
441441
}
442-
for _, ip := range *block.Properties.Ips {
443-
err = ctx.IONOSClient.EnsureAdditionalIPOnNic(ctx, ctx.IONOSCloudCluster.Spec.DataCenterID, ctx.IONOSCloudMachine.Spec.ProviderID, *serverNic.Id, ip)
444-
if err != nil {
445-
return err
446-
}
447-
lanId := fmt.Sprint(*lanSpec.LanID)
448-
lan, _, err := ctx.IONOSClient.GetLan(ctx, ctx.IONOSCloudCluster.Spec.DataCenterID, lanId)
449-
if err != nil {
450-
return err
451-
}
452-
registered := false
453-
if lan.Properties.IpFailover != nil {
454-
for _, f := range *lan.Properties.IpFailover {
455-
if *f.Ip == ip {
456-
registered = true
457-
continue
458-
}
459-
}
460-
if registered {
461-
continue
462-
}
463-
}
464-
465-
//todo only once per cluster and change if machine gets delete
466-
err = ctx.IONOSClient.EnsureFailoverIPOnLan(ctx, ctx.IONOSCloudCluster.Spec.DataCenterID, lanId, ip, *serverNic.Id)
467-
if err != nil {
468-
return err
469-
}
442+
ips := *block.Properties.Ips
443+
err = ctx.IONOSClient.EnsureAdditionalIPsOnNic(ctx, ctx.IONOSCloudCluster.Spec.DataCenterID, ctx.IONOSCloudMachine.Spec.ProviderID, *serverNic.Id, ips)
444+
if err != nil {
445+
return err
446+
}
447+
//todo only once per cluster and change if machine gets delete
448+
lanId := fmt.Sprint(*lanSpec.LanID)
449+
err = ctx.IONOSClient.EnsureFailoverIPsOnLan(ctx, ctx.IONOSCloudCluster.Spec.DataCenterID, lanId, *serverNic.Id, ips)
450+
if err != nil {
451+
return err
470452
}
471453
}
472454
}

internal/ionos/apiclient.go

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package ionos
33
import (
44
"context"
55
"fmt"
6-
"github.com/GDATASoftwareAG/cluster-api-provider-ionoscloud/api/v1alpha1"
7-
ionoscloud "github.com/ionos-cloud/sdk-go/v6"
86
"strings"
97
"sync"
8+
9+
"github.com/GDATASoftwareAG/cluster-api-provider-ionoscloud/api/v1alpha1"
10+
ionoscloud "github.com/ionos-cloud/sdk-go/v6"
1011
)
1112

1213
var _ Client = (*APIClient)(nil)
@@ -29,7 +30,7 @@ type IPBlockAPI interface {
2930
type LanAPI interface {
3031
CreateLan(ctx context.Context, datacenterId string, public bool) (ionoscloud.LanPost, *ionoscloud.APIResponse, error)
3132
GetLan(ctx context.Context, datacenterId, lanId string) (ionoscloud.Lan, *ionoscloud.APIResponse, error)
32-
EnsureFailoverIPOnLan(ctx context.Context, datacenterId, lanId, ip, nicUuid string) error
33+
EnsureFailoverIPsOnLan(ctx context.Context, datacenterId, lanId, nicUuid string, ips []string) error
3334
}
3435

3536
type DefaultAPI interface {
@@ -51,7 +52,7 @@ type ServerAPI interface {
5152
CreateServer(ctx context.Context, datacenterId string, server ionoscloud.Server) (ionoscloud.Server, *ionoscloud.APIResponse, error)
5253
GetServer(ctx context.Context, datacenterId, serverId string) (ionoscloud.Server, *ionoscloud.APIResponse, error)
5354
DeleteServer(ctx context.Context, datacenterId, serverId string) (*ionoscloud.APIResponse, error)
54-
EnsureAdditionalIPOnNic(ctx context.Context, datacenterId, serverId, nic, ip string) error
55+
EnsureAdditionalIPsOnNic(ctx context.Context, datacenterId, serverId, nicUuid string, ips []string) error
5556
}
5657

5758
type Client interface {
@@ -85,53 +86,66 @@ type APIClient struct {
8586
client *ionoscloud.APIClient
8687
}
8788

88-
func (c *APIClient) EnsureAdditionalIPOnNic(ctx context.Context, datacenterId, serverId, nicUuid, ip string) error {
89+
func (c *APIClient) EnsureAdditionalIPsOnNic(ctx context.Context, datacenterId, serverId, nicUuid string, toEnsureIPs []string) error {
8990
serverId = strings.TrimPrefix(serverId, "ionos://")
9091
nic, _, err := c.client.NetworkInterfacesApi.DatacentersServersNicsFindById(ctx, datacenterId, serverId, nicUuid).Execute()
9192
if err != nil {
9293
return err
9394
}
94-
ips := []string{}
95+
ips := make([]string, 0)
9596
if nic.Properties.Ips != nil {
96-
for _, current := range *nic.Properties.Ips {
97+
ips = *nic.Properties.Ips
98+
}
99+
for _, ip := range toEnsureIPs {
100+
toAdd := true
101+
for _, current := range ips {
97102
if current == ip {
98-
return nil
103+
toAdd = false
104+
break
99105
}
100106
}
101-
ips = *nic.Properties.Ips
107+
if toAdd {
108+
ips = append(ips, ip)
109+
}
102110
}
103-
ips = append(ips, ip)
111+
104112
_, _, err = c.client.NetworkInterfacesApi.DatacentersServersNicsPatch(ctx, datacenterId, serverId, nicUuid).Nic(ionoscloud.NicProperties{
105113
Ips: &ips,
106114
}).Execute()
107115
return err
108116
}
109117

110-
func (c *APIClient) EnsureFailoverIPOnLan(ctx context.Context, datacenterId, lanId, ip, nicUuid string) error {
118+
func (c *APIClient) EnsureFailoverIPsOnLan(ctx context.Context, datacenterId, lanId, nicUuid string, toEnsureIPs []string) error {
111119
lan, _, err := c.client.LANsApi.DatacentersLansFindById(ctx, datacenterId, lanId).Execute()
112120
if err != nil {
113121
return err
114122
}
115-
failovers := []ionoscloud.IPFailover{}
116-
ignore := false
123+
124+
requireRegister := false
125+
failovers := make([]ionoscloud.IPFailover, 0)
117126
if lan.Properties.IpFailover != nil {
118-
for _, failover := range *lan.Properties.IpFailover {
119-
if *failover.Ip == ip {
120-
if *failover.NicUuid == nicUuid {
121-
return nil
122-
}
123-
failover.NicUuid = &nicUuid
124-
ignore = true
127+
failovers = *lan.Properties.IpFailover
128+
}
129+
for _, ip := range toEnsureIPs {
130+
toAdd := true
131+
for _, current := range failovers {
132+
if *current.Ip == ip {
133+
toAdd = false
134+
break
125135
}
126-
failovers = append(failovers, failover)
136+
}
137+
if toAdd {
138+
requireRegister = true
139+
failovers = append(failovers, ionoscloud.IPFailover{
140+
Ip: &ip,
141+
NicUuid: &nicUuid,
142+
})
127143
}
128144
}
129-
if !ignore {
130-
failovers = append(failovers, ionoscloud.IPFailover{
131-
Ip: &ip,
132-
NicUuid: &nicUuid,
133-
})
145+
if !requireRegister {
146+
return nil
134147
}
148+
135149
_, _, err = c.client.LANsApi.DatacentersLansPatch(ctx, datacenterId, lanId).Lan(ionoscloud.LanProperties{
136150
IpFailover: &failovers,
137151
}).Execute()

testing/fakeClient.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ type FakeClient struct {
2626
CredentialsAreValid bool
2727
}
2828

29-
func (f FakeClient) EnsureFailoverIPOnLan(ctx context.Context, datacenterId, lanId, ip, nicUuid string) error {
29+
func (f FakeClient) EnsureFailoverIPsOnLan(_ context.Context, _, _ string, _ string, _ []string) error {
3030
//TODO implement me
3131
panic("implement me")
3232
}
3333

34-
func (f FakeClient) EnsureAdditionalIPOnNic(ctx context.Context, datacenterId, serverId, nic, ip string) error {
34+
func (f FakeClient) EnsureAdditionalIPsOnNic(_ context.Context, _, _, _ string, _ []string) error {
3535
//TODO implement me
3636
panic("implement me")
3737
}

0 commit comments

Comments
 (0)