github.com/imran-kn/cilium-fork@v1.6.9/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(node *envoy_api_v2_core.Node) (string, error) { 44 if node == nil { 45 return "", errors.New("node is nil") 46 } 47 if node.GetId() == "" { 48 return "", errors.New("node.id is empty") 49 } 50 51 parts := strings.Split(node.Id, "~") 52 if len(parts) != 4 { 53 return "", fmt.Errorf("node.id is invalid: %s", node.Id) 54 } 55 56 ip := parts[1] 57 58 if net.ParseIP(ip) == nil { 59 return "", fmt.Errorf("node.id contains an invalid node IP address: %s", node.Id) 60 } 61 62 return ip, nil 63 }