Skip to content

Commit 631209c

Browse files
author
Rahul Sharma
committed
set addressType based on is_public value
1 parent 137451b commit 631209c

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

cloud/linode/instances.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,22 @@ type nodeCache struct {
3939
}
4040

4141
// getInstanceAddresses returns all addresses configured on a linode.
42-
func (nc *nodeCache) getInstanceAddresses(instance linodego.Instance, vpcips []string) []nodeIP {
42+
func (nc *nodeCache) getInstanceAddresses(instance linodego.Instance, vpcips []string, vpcIPv6AddrTypes map[string]v1.NodeAddressType) []nodeIP {
4343
ips := []nodeIP{}
4444

45+
// We store vpc IPv6 addrs separately so that we can list them after IPv4 addresses.
46+
// Ordering matters in k8s, first address marked as externalIP will be used as external IP for node.
47+
// We prefer to use IPv4 address as external IP if possible, so we list them first.
48+
vpcIPv6Addrs := []nodeIP{}
49+
4550
// If vpc ips are present, list them first
4651
for _, ip := range vpcips {
4752
ipType := v1.NodeInternalIP
53+
if _, ok := vpcIPv6AddrTypes[ip]; ok {
54+
ipType = vpcIPv6AddrTypes[ip]
55+
vpcIPv6Addrs = append(vpcIPv6Addrs, nodeIP{ip: ip, ipType: ipType})
56+
continue
57+
}
4858
ips = append(ips, nodeIP{ip: ip, ipType: ipType})
4959
}
5060

@@ -56,6 +66,9 @@ func (nc *nodeCache) getInstanceAddresses(instance linodego.Instance, vpcips []s
5666
ips = append(ips, nodeIP{ip: ip.String(), ipType: ipType})
5767
}
5868

69+
// Add vpc IPv6 addresses after IPv4 addresses
70+
ips = append(ips, vpcIPv6Addrs...)
71+
5972
if instance.IPv6 != "" {
6073
ips = append(ips, nodeIP{ip: strings.TrimSuffix(instance.IPv6, "/128"), ipType: v1.NodeExternalIP})
6174
}
@@ -80,6 +93,7 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client)
8093

8194
// If running within VPC, find instances and store their ips
8295
vpcNodes := map[int][]string{}
96+
vpcIPv6AddrTypes := map[string]v1.NodeAddressType{}
8397
for _, name := range Options.VPCNames {
8498
vpcName := strings.TrimSpace(name)
8599
if vpcName == "" {
@@ -106,8 +120,13 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client)
106120
if len(r.IPv6Addresses) == 0 {
107121
continue
108122
}
123+
vpcIPv6AddrType := v1.NodeInternalIP
124+
if r.IPv6IsPublic != nil && *r.IPv6IsPublic {
125+
vpcIPv6AddrType = v1.NodeExternalIP
126+
}
109127
for _, ipv6 := range r.IPv6Addresses {
110128
vpcNodes[r.LinodeID] = append(vpcNodes[r.LinodeID], ipv6.SLAACAddress)
129+
vpcIPv6AddrTypes[ipv6.SLAACAddress] = vpcIPv6AddrType
111130
}
112131
}
113132
}
@@ -120,7 +139,7 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client)
120139
}
121140
node := linodeInstance{
122141
instance: &instances[index],
123-
ips: nc.getInstanceAddresses(instance, vpcNodes[instance.ID]),
142+
ips: nc.getInstanceAddresses(instance, vpcNodes[instance.ID], vpcIPv6AddrTypes),
124143
}
125144
newNodes[instance.ID] = node
126145
}

0 commit comments

Comments
 (0)