github.com/cilium/cilium@v1.16.2/pkg/envoy/xds/node.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package xds
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"net"
    10  	"strings"
    11  )
    12  
    13  // EnvoyNodeIdToIP extracts the IP address from an Envoy node identifier.
    14  //
    15  // The NodeID is structured as the concatenation of the
    16  // following parts separated by ~:
    17  //
    18  // - node type
    19  // - node IP address
    20  // - node ID
    21  // - node domain: the DNS domain suffix for short hostnames, e.g. "default.svc.cluster.local"
    22  //
    23  // For instance:
    24  //
    25  //	"host~127.0.0.1~no-id~localdomain"
    26  func EnvoyNodeIdToIP(nodeId string) (string, error) {
    27  	if nodeId == "" {
    28  		return "", errors.New("nodeId is empty")
    29  	}
    30  
    31  	parts := strings.Split(nodeId, "~")
    32  	if len(parts) != 4 {
    33  		return "", fmt.Errorf("nodeId is invalid: %s", nodeId)
    34  	}
    35  
    36  	ip := parts[1]
    37  
    38  	if net.ParseIP(ip) == nil {
    39  		return "", fmt.Errorf("nodeId contains an invalid node IP address: %s", nodeId)
    40  	}
    41  
    42  	return ip, nil
    43  }