Skip to content

Commit

Permalink
remove duplicate ip's to avoid patching error (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulait authored Nov 13, 2024
1 parent 3dd71d0 commit dc407dd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 14 additions & 4 deletions cloud/linode/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
8 changes: 8 additions & 0 deletions cloud/linode/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"fmt"
"net"
"slices"
"strconv"
"strings"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -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)
}
})
Expand Down

0 comments on commit dc407dd

Please sign in to comment.