mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-14 05:36:12 +00:00
Merge pull request #11766 from deads2k/kubelet-prefers-ipv4
make kubelet prefer ipv4 address if available
This commit is contained in:
@@ -1997,7 +1997,27 @@ func (kl *Kubelet) setNodeStatus(node *api.Node) error {
|
||||
} else if len(addrs) == 0 {
|
||||
return fmt.Errorf("no ip address for node %v", node.Name)
|
||||
} else {
|
||||
node.Status.Addresses = []api.NodeAddress{{Type: api.NodeLegacyHostIP, Address: addrs[0].String()}}
|
||||
// check all ip addresses for this node.Name and try to find the first non-loopback IPv4 address.
|
||||
// If no match is found, it uses the IP of the interface with gateway on it.
|
||||
for _, ip := range addrs {
|
||||
if ip.IsLoopback() {
|
||||
continue
|
||||
}
|
||||
|
||||
if ip.To4() != nil {
|
||||
node.Status.Addresses = []api.NodeAddress{{Type: api.NodeLegacyHostIP, Address: ip.String()}}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if len(node.Status.Addresses) == 0 {
|
||||
ip, err := util.ChooseHostInterface()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
node.Status.Addresses = []api.NodeAddress{{Type: api.NodeLegacyHostIP, Address: ip.String()}}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user