github.com/cilium/cilium@v1.16.2/pkg/node/addressing/addresstype.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package addressing 5 6 import ( 7 "net" 8 ) 9 10 // AddressType represents a type of IP address for a node. They are copied 11 // from k8s.io/api/core/v1/types.go to avoid pulling in a lot of Kubernetes 12 // imports into this package. 13 type AddressType string 14 15 const ( 16 NodeHostName AddressType = "Hostname" 17 NodeExternalIP AddressType = "ExternalIP" 18 NodeInternalIP AddressType = "InternalIP" 19 NodeExternalDNS AddressType = "ExternalDNS" 20 NodeInternalDNS AddressType = "InternalDNS" 21 NodeCiliumInternalIP AddressType = "CiliumInternalIP" 22 ) 23 24 type Address interface { 25 AddrType() AddressType 26 ToString() string 27 } 28 29 // ExtractNodeIP returns one of the provided IP addresses available with the following priority: 30 // - NodeInternalIP 31 // - NodeExternalIP 32 // - other IP address type 33 // An error is returned if ExtractNodeIP fails to get an IP based on the provided address family. 34 func ExtractNodeIP[T Address](addrs []T, ipv6 bool) net.IP { 35 var backupIP net.IP 36 for _, addr := range addrs { 37 parsed := net.ParseIP(addr.ToString()) 38 if parsed == nil { 39 continue 40 } 41 if (ipv6 && parsed.To4() != nil) || 42 (!ipv6 && parsed.To4() == nil) { 43 continue 44 } 45 switch addr.AddrType() { 46 // Ignore CiliumInternalIPs 47 case NodeCiliumInternalIP: 48 continue 49 // Always prefer a cluster internal IP 50 case NodeInternalIP: 51 return parsed 52 case NodeExternalIP: 53 // Fall back to external Node IP 54 // if no internal IP could be found 55 backupIP = parsed 56 default: 57 // As a last resort, if no internal or external 58 // IP was found, use any node address available 59 if backupIP == nil { 60 backupIP = parsed 61 } 62 } 63 } 64 return backupIP 65 }