Skip to content

Commit

Permalink
Merge pull request #44 from GDATASoftwareAG/speedup-failoverGroups
Browse files Browse the repository at this point in the history
speedup failoverGroups
  • Loading branch information
farodin91 authored Nov 22, 2023
2 parents df34d2b + 9225598 commit 4421db0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 56 deletions.
38 changes: 10 additions & 28 deletions internal/controller/ionoscloudmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,34 +439,16 @@ func (r *IONOSCloudMachineReconciler) reconcileFailoverGroups(ctx *context.Machi
if err != nil {
return err
}
for _, ip := range *block.Properties.Ips {
err = ctx.IONOSClient.EnsureAdditionalIPOnNic(ctx, ctx.IONOSCloudCluster.Spec.DataCenterID, ctx.IONOSCloudMachine.Spec.ProviderID, *serverNic.Id, ip)
if err != nil {
return err
}
lanId := fmt.Sprint(*lanSpec.LanID)
lan, _, err := ctx.IONOSClient.GetLan(ctx, ctx.IONOSCloudCluster.Spec.DataCenterID, lanId)
if err != nil {
return err
}
registered := false
if lan.Properties.IpFailover != nil {
for _, f := range *lan.Properties.IpFailover {
if *f.Ip == ip {
registered = true
continue
}
}
if registered {
continue
}
}

//todo only once per cluster and change if machine gets delete
err = ctx.IONOSClient.EnsureFailoverIPOnLan(ctx, ctx.IONOSCloudCluster.Spec.DataCenterID, lanId, ip, *serverNic.Id)
if err != nil {
return err
}
ips := *block.Properties.Ips
err = ctx.IONOSClient.EnsureAdditionalIPsOnNic(ctx, ctx.IONOSCloudCluster.Spec.DataCenterID, ctx.IONOSCloudMachine.Spec.ProviderID, *serverNic.Id, ips)
if err != nil {
return err
}
//todo only once per cluster and change if machine gets delete
lanId := fmt.Sprint(*lanSpec.LanID)
err = ctx.IONOSClient.EnsureFailoverIPsOnLan(ctx, ctx.IONOSCloudCluster.Spec.DataCenterID, lanId, *serverNic.Id, ips)
if err != nil {
return err
}
}
}
Expand Down
66 changes: 40 additions & 26 deletions internal/ionos/apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package ionos
import (
"context"
"fmt"
"github.com/GDATASoftwareAG/cluster-api-provider-ionoscloud/api/v1alpha1"
ionoscloud "github.com/ionos-cloud/sdk-go/v6"
"strings"
"sync"

"github.com/GDATASoftwareAG/cluster-api-provider-ionoscloud/api/v1alpha1"
ionoscloud "github.com/ionos-cloud/sdk-go/v6"
)

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

type DefaultAPI interface {
Expand All @@ -51,7 +52,7 @@ type ServerAPI interface {
CreateServer(ctx context.Context, datacenterId string, server ionoscloud.Server) (ionoscloud.Server, *ionoscloud.APIResponse, error)
GetServer(ctx context.Context, datacenterId, serverId string) (ionoscloud.Server, *ionoscloud.APIResponse, error)
DeleteServer(ctx context.Context, datacenterId, serverId string) (*ionoscloud.APIResponse, error)
EnsureAdditionalIPOnNic(ctx context.Context, datacenterId, serverId, nic, ip string) error
EnsureAdditionalIPsOnNic(ctx context.Context, datacenterId, serverId, nicUuid string, ips []string) error
}

type Client interface {
Expand Down Expand Up @@ -85,53 +86,66 @@ type APIClient struct {
client *ionoscloud.APIClient
}

func (c *APIClient) EnsureAdditionalIPOnNic(ctx context.Context, datacenterId, serverId, nicUuid, ip string) error {
func (c *APIClient) EnsureAdditionalIPsOnNic(ctx context.Context, datacenterId, serverId, nicUuid string, toEnsureIPs []string) error {
serverId = strings.TrimPrefix(serverId, "ionos://")
nic, _, err := c.client.NetworkInterfacesApi.DatacentersServersNicsFindById(ctx, datacenterId, serverId, nicUuid).Execute()
if err != nil {
return err
}
ips := []string{}
ips := make([]string, 0)
if nic.Properties.Ips != nil {
for _, current := range *nic.Properties.Ips {
ips = *nic.Properties.Ips
}
for _, ip := range toEnsureIPs {
toAdd := true
for _, current := range ips {
if current == ip {
return nil
toAdd = false
break
}
}
ips = *nic.Properties.Ips
if toAdd {
ips = append(ips, ip)
}
}
ips = append(ips, ip)

_, _, err = c.client.NetworkInterfacesApi.DatacentersServersNicsPatch(ctx, datacenterId, serverId, nicUuid).Nic(ionoscloud.NicProperties{
Ips: &ips,
}).Execute()
return err
}

func (c *APIClient) EnsureFailoverIPOnLan(ctx context.Context, datacenterId, lanId, ip, nicUuid string) error {
func (c *APIClient) EnsureFailoverIPsOnLan(ctx context.Context, datacenterId, lanId, nicUuid string, toEnsureIPs []string) error {
lan, _, err := c.client.LANsApi.DatacentersLansFindById(ctx, datacenterId, lanId).Execute()
if err != nil {
return err
}
failovers := []ionoscloud.IPFailover{}
ignore := false

requireRegister := false
failovers := make([]ionoscloud.IPFailover, 0)
if lan.Properties.IpFailover != nil {
for _, failover := range *lan.Properties.IpFailover {
if *failover.Ip == ip {
if *failover.NicUuid == nicUuid {
return nil
}
failover.NicUuid = &nicUuid
ignore = true
failovers = *lan.Properties.IpFailover
}
for _, ip := range toEnsureIPs {
toAdd := true
for _, current := range failovers {
if *current.Ip == ip {
toAdd = false
break
}
failovers = append(failovers, failover)
}
if toAdd {
requireRegister = true
failovers = append(failovers, ionoscloud.IPFailover{
Ip: &ip,
NicUuid: &nicUuid,
})
}
}
if !ignore {
failovers = append(failovers, ionoscloud.IPFailover{
Ip: &ip,
NicUuid: &nicUuid,
})
if !requireRegister {
return nil
}

_, _, err = c.client.LANsApi.DatacentersLansPatch(ctx, datacenterId, lanId).Lan(ionoscloud.LanProperties{
IpFailover: &failovers,
}).Execute()
Expand Down
4 changes: 2 additions & 2 deletions testing/fakeClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ type FakeClient struct {
CredentialsAreValid bool
}

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

func (f FakeClient) EnsureAdditionalIPOnNic(ctx context.Context, datacenterId, serverId, nic, ip string) error {
func (f FakeClient) EnsureAdditionalIPsOnNic(_ context.Context, _, _, _ string, _ []string) error {
//TODO implement me
panic("implement me")
}
Expand Down

0 comments on commit 4421db0

Please sign in to comment.