istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/serviceregistry/util/label/label.go (about) 1 // Copyright Istio Authors 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 label 16 17 import ( 18 "strings" 19 20 "istio.io/api/label" 21 "istio.io/istio/pkg/cluster" 22 "istio.io/istio/pkg/config/labels" 23 "istio.io/istio/pkg/network" 24 ) 25 26 // copied from https://github.com/kubernetes/api/blob/master/core/v1/well_known_labels.go 27 // It is to remove dependency on k8s.io/api/core/v1 28 const ( 29 LabelHostname = "kubernetes.io/hostname" 30 31 LabelTopologyZone = "topology.kubernetes.io/zone" 32 LabelTopologySubzone = "topology.istio.io/subzone" 33 LabelTopologyRegion = "topology.kubernetes.io/region" 34 ) 35 36 // AugmentLabels adds additional labels to the those provided. 37 func AugmentLabels(in labels.Instance, clusterID cluster.ID, locality, k8sNode string, networkID network.ID) labels.Instance { 38 // Copy the original labels to a new map. 39 out := make(labels.Instance, len(in)+6) 40 for k, v := range in { 41 out[k] = v 42 } 43 44 region, zone, subzone := SplitLocalityLabel(locality) 45 if len(region) > 0 { 46 out[LabelTopologyRegion] = region 47 } 48 if len(zone) > 0 { 49 out[LabelTopologyZone] = zone 50 } 51 if len(subzone) > 0 { 52 out[label.TopologySubzone.Name] = subzone 53 } 54 if len(clusterID) > 0 { 55 out[label.TopologyCluster.Name] = clusterID.String() 56 } 57 if len(k8sNode) > 0 { 58 out[LabelHostname] = k8sNode 59 } 60 if len(networkID) > 0 { 61 out[label.TopologyNetwork.Name] = networkID.String() 62 } 63 return out 64 } 65 66 // SplitLocalityLabel splits a locality label into region, zone and subzone strings. 67 func SplitLocalityLabel(locality string) (region, zone, subzone string) { 68 items := strings.Split(locality, "/") 69 switch len(items) { 70 case 1: 71 return items[0], "", "" 72 case 2: 73 return items[0], items[1], "" 74 default: 75 return items[0], items[1], items[2] 76 } 77 }