Skip to content

Commit

Permalink
remove duplicate ip's to avoid patching error
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulait committed Nov 13, 2024
1 parent 1b046af commit 38142ec
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

Check warning on line 285 in cloud/linode/instances.go

View check run for this annotation

Codecov / codecov/patch

cloud/linode/instances.go#L285

Added line #L285 was not covered by tests
}
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 38142ec

Please sign in to comment.