From 38142ec216cac81a938b4d2d67799ef518f47ae1 Mon Sep 17 00:00:00 2001 From: Rahul Sharma Date: Wed, 13 Nov 2024 15:21:15 +0000 Subject: [PATCH] remove duplicate ip's to avoid patching error --- cloud/linode/instances.go | 18 ++++++++++++++---- cloud/linode/instances_test.go | 8 ++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/cloud/linode/instances.go b/cloud/linode/instances.go index 61e48b6d..f5cc449a 100644 --- a/cloud/linode/instances.go +++ b/cloud/linode/instances.go @@ -279,19 +279,29 @@ func (i *instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloud return nil, err } - addresses := []v1.NodeAddress{{Type: v1.NodeHostName, Address: linode.Label}} - + uniqueAddrs := make(map[string]v1.NodeAddressType, len(node.Status.Addresses)+len(ips)) for _, ip := range ips { - addresses = append(addresses, v1.NodeAddress{Type: ip.ipType, Address: ip.ip}) + if _, ok := uniqueAddrs[ip.ip]; ok { + continue + } + uniqueAddrs[ip.ip] = ip.ipType } // include IPs set by kubelet for internal node IP for _, addr := range node.Status.Addresses { + if _, ok := uniqueAddrs[addr.Address]; ok { + continue + } if addr.Type == v1.NodeInternalIP { - addresses = append(addresses, v1.NodeAddress{Type: v1.NodeInternalIP, Address: addr.Address}) + uniqueAddrs[addr.Address] = v1.NodeInternalIP } } + addresses := []v1.NodeAddress{{Type: v1.NodeHostName, Address: linode.Label}} + for k, v := range uniqueAddrs { + addresses = append(addresses, v1.NodeAddress{Type: v, Address: k}) + } + klog.Infof("Instance %s, assembled IP addresses: %v", node.Name, addresses) // note that Zone is omitted as it's not a thing in Linode meta := &cloudprovider.InstanceMetadata{ diff --git a/cloud/linode/instances_test.go b/cloud/linode/instances_test.go index b85fc804..b63c6d5c 100644 --- a/cloud/linode/instances_test.go +++ b/cloud/linode/instances_test.go @@ -4,7 +4,9 @@ import ( "context" "fmt" "net" + "slices" "strconv" + "strings" "testing" "github.com/golang/mock/gomock" @@ -273,6 +275,12 @@ func TestMetadataRetrieval(t *testing.T) { addresses := append([]v1.NodeAddress{ {Type: v1.NodeHostName, Address: name}, }, test.outputAddresses...) + slices.SortFunc(meta.NodeAddresses, func(a v1.NodeAddress, b v1.NodeAddress) int { + return strings.Compare(a.Address, b.Address) + }) + slices.SortFunc(addresses, func(a, b v1.NodeAddress) int { + return strings.Compare(a.Address, b.Address) + }) assert.Equal(t, meta.NodeAddresses, addresses) } })