diff --git a/pkg/client/client.go b/pkg/client/client.go index 85ecad4..2c0931e 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -89,7 +89,7 @@ func (a *IONOSClient) RemoveIPFromNode(ctx context.Context, loadBalancerIP, prov return errors.New("node has no nics") } - primaryNic := (*nics.Items)[0] + primaryNic := getPrimaryNic(*nics.Items) ips := *primaryNic.Properties.Ips for idx, v := range ips { @@ -105,6 +105,15 @@ func (a *IONOSClient) RemoveIPFromNode(ctx context.Context, loadBalancerIP, prov return err } +func getPrimaryNic(nics []ionoscloud.Nic) *ionoscloud.Nic { + for _, nic := range nics { + if *nic.Properties.PciSlot == 6 { + return &nic + } + } + return nil +} + func (a *IONOSClient) AttachIPToNode(ctx context.Context, loadBalancerIP, providerID string) (bool, error) { if a.client == nil { return false, errors.New("client isn't initialized") @@ -123,7 +132,7 @@ func (a *IONOSClient) AttachIPToNode(ctx context.Context, loadBalancerIP, provid return false, errors.New("node has no nics") } - primaryNic := (*nics.Items)[1] + primaryNic := getPrimaryNic(*nics.Items) ips := *primaryNic.Properties.Ips ips = append(ips, loadBalancerIP) diff --git a/pkg/ionos/loadbalancer.go b/pkg/ionos/loadbalancer.go index 2911bb1..33c7062 100644 --- a/pkg/ionos/loadbalancer.go +++ b/pkg/ionos/loadbalancer.go @@ -134,6 +134,20 @@ func (l loadbalancer) syncLoadBalancer(ctx context.Context, clusterName string, return nil, errors.New("we are only handling LoadBalancers with spec.loadBalancerID != ''") } + if len(service.Status.LoadBalancer.Ingress) > 0 && service.Status.LoadBalancer.Ingress[0].IP != service.Spec.LoadBalancerIP { + klog.Infof("service %s/%s changed IP from %s to %s", service.Namespace, service.Name, service.Status.LoadBalancer.Ingress[0].IP, service.Spec.LoadBalancerIP) + server, err := l.ServerWithLoadBalancer(ctx, service.Status.LoadBalancer.Ingress[0].IP) + if err != nil { + return nil, err + } + + if server != nil { + if err := l.deleteLoadBalancerFromNode(ctx, service.Status.LoadBalancer.Ingress[0].IP, server); err != nil { + return nil, err + } + } + } + server, err := l.ServerWithLoadBalancer(ctx, service.Spec.LoadBalancerIP) if err != nil { return nil, err @@ -159,7 +173,7 @@ func (l loadbalancer) syncLoadBalancer(ctx context.Context, clusterName string, if loadBalancerNode == nil { return nil, errors.New("no valid nodes found") } - klog.Infof("server %s is elected as new loadbalancer node", server) + klog.Infof("server %s is elected as new loadbalancer node", loadBalancerNode) for _, client := range l.ionosClients { ok, err := client.AttachIPToNode(ctx, service.Spec.LoadBalancerIP, stripProviderFromID(loadBalancerNode.Spec.ProviderID))