Browse Source

Merge pull request #1440 from fabric8io/kubernetes-discovery

Kubernetes SD: Fix node IP discovery
pull/1444/head
Brian Brazil 9 years ago
parent
commit
671cc59de7
  1. 32
      retrieval/discovery/kubernetes/discovery.go
  2. 4
      retrieval/discovery/kubernetes/types.go

32
retrieval/discovery/kubernetes/discovery.go

@ -265,7 +265,13 @@ func (kd *Discovery) updateNodesTargetGroup() *config.TargetGroup {
// Now let's loop through the nodes & add them to the target group with appropriate labels.
for nodeName, node := range kd.nodes {
address := fmt.Sprintf("%s:%d", node.Status.Addresses[0].Address, kd.Conf.KubeletPort)
nodeAddress, err := nodeHostIP(node)
if err != nil {
log.Debugf("Skipping node %s: %s", node.Name, err)
continue
}
address := fmt.Sprintf("%s:%d", nodeAddress.String(), kd.Conf.KubeletPort)
t := model.LabelSet{
model.AddressLabel: model.LabelValue(address),
@ -744,3 +750,27 @@ func until(f func(), period time.Duration, stopCh <-chan struct{}) {
}
}
}
// nodeHostIP returns the provided node's address, based on the priority:
// 1. NodeInternalIP
// 2. NodeExternalIP
// 3. NodeLegacyHostIP
//
// Copied from k8s.io/kubernetes/pkg/util/node/node.go
func nodeHostIP(node *Node) (net.IP, error) {
addresses := node.Status.Addresses
addressMap := make(map[NodeAddressType][]NodeAddress)
for i := range addresses {
addressMap[addresses[i].Type] = append(addressMap[addresses[i].Type], addresses[i])
}
if addresses, ok := addressMap[NodeInternalIP]; ok {
return net.ParseIP(addresses[0].Address), nil
}
if addresses, ok := addressMap[NodeExternalIP]; ok {
return net.ParseIP(addresses[0].Address), nil
}
if addresses, ok := addressMap[NodeLegacyHostIP]; ok {
return net.ParseIP(addresses[0].Address), nil
}
return nil, fmt.Errorf("host IP unknown; known addresses: %v", addresses)
}

4
retrieval/discovery/kubernetes/types.go

@ -202,8 +202,10 @@ type NodeStatus struct {
type NodeAddressType string
// These are valid address type of node.
// These are valid address types of node. NodeLegacyHostIP is used to transit
// from out-dated HostIP field to NodeAddress.
const (
NodeLegacyHostIP NodeAddressType = "LegacyHostIP"
NodeHostName NodeAddressType = "Hostname"
NodeExternalIP NodeAddressType = "ExternalIP"
NodeInternalIP NodeAddressType = "InternalIP"

Loading…
Cancel
Save