github.phpd.cn/cilium/cilium@v1.6.12/pkg/envoy/xds/node.go (about) 1 // Copyright 2018 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package xds 16 17 import ( 18 "errors" 19 "fmt" 20 "net" 21 "strings" 22 23 envoy_api_v2_core "github.com/cilium/proxy/go/envoy/api/v2/core" 24 ) 25 26 // NodeToIDFunc extracts a string identifier from an Envoy Node identifier. 27 type NodeToIDFunc func(node *envoy_api_v2_core.Node) (string, error) 28 29 // IstioNodeToIP extract the IP address from an Envoy node identifier 30 // configured by Istio's pilot-agent. 31 // 32 // Istio's pilot-agent structures the node.id as the concatenation of the 33 // following parts separated by ~: 34 // 35 // - node type: one of "sidecar", "ingress", or "router" 36 // - node IP address 37 // - node ID: the unique platform-specific sidecar proxy ID 38 // - node domain: the DNS domain suffix for short hostnames, e.g. "default.svc.cluster.local" 39 // 40 // For instance: 41 // 42 // "sidecar~10.1.1.0~v0.default~default.svc.cluster.local" 43 func IstioNodeToIP(nodeId string) (string, error) { 44 if nodeId == "" { 45 return "", errors.New("nodeId is empty") 46 } 47 48 parts := strings.Split(nodeId, "~") 49 if len(parts) != 4 { 50 return "", fmt.Errorf("nodeId is invalid: %s", nodeId) 51 } 52 53 ip := parts[1] 54 55 if net.ParseIP(ip) == nil { 56 return "", fmt.Errorf("node.id contains an invalid node IP address: %s", nodeId) 57 } 58 59 return ip, nil 60 }