istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/echo/services.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 echo 16 17 import ( 18 "sort" 19 "strings" 20 ) 21 22 // Services is a set of Instances that share the same FQDN. While an Instance contains 23 // multiple deployments (a single service in a single cluster), Instances contains multiple 24 // deployments that may contain multiple Services. 25 type Services []Instances 26 27 // GetByService finds the first Instances with the given Service name. It is possible to have multiple deployments 28 // with the same service name but different namespaces (and therefore different FQDNs). Use caution when relying on 29 // Service. 30 func (d Services) GetByService(service string) Target { 31 for _, target := range d { 32 if target.Config().Service == service { 33 return target 34 } 35 } 36 return nil 37 } 38 39 // FQDNs gives the fully-qualified-domain-names each deployment in order. 40 func (d Services) FQDNs() []string { 41 var out []string 42 for _, target := range d { 43 out = append(out, target.Config().ClusterLocalFQDN()) 44 } 45 return out 46 } 47 48 func (d Services) Instances() Instances { 49 var out Instances 50 for _, target := range d { 51 out = append(out, target.Instances()...) 52 } 53 return out 54 } 55 56 func (d Services) Callers() Callers { 57 var out Callers 58 for _, s := range d { 59 out = append(out, s[0]) 60 } 61 return out 62 } 63 64 func (d Services) MatchFQDNs(fqdns ...string) Services { 65 match := map[string]bool{} 66 for _, fqdn := range fqdns { 67 match[fqdn] = true 68 } 69 var out Services 70 for _, target := range d { 71 if match[target.Config().ClusterLocalFQDN()] { 72 out = append(out, target) 73 } 74 } 75 return out 76 } 77 78 // Services must be sorted to make sure tests have consistent ordering 79 var _ sort.Interface = Services{} 80 81 // Len returns the number of deployments 82 func (d Services) Len() int { 83 return len(d) 84 } 85 86 // Less returns true if the element at i should appear before the element at j in a sorted Services 87 func (d Services) Less(i, j int) bool { 88 return strings.Compare(d[i].Config().ClusterLocalFQDN(), d[j].Config().ClusterLocalFQDN()) < 0 89 } 90 91 // Swap switches the positions of elements at i and j (used for sorting). 92 func (d Services) Swap(i, j int) { 93 d[i], d[j] = d[j], d[i] 94 } 95 96 // Copy this services array. 97 func (d Services) Copy() Services { 98 return append(Services{}, d...) 99 } 100 101 // Append returns a new Services array with the given values appended. 102 func (d Services) Append(others ...Services) Services { 103 out := d.Copy() 104 for _, o := range others { 105 out = append(out, o...) 106 } 107 sort.Stable(out) 108 return out 109 } 110 111 func (d Services) NamespacedNames() NamespacedNames { 112 out := make(NamespacedNames, 0, d.Len()) 113 for _, svc := range d { 114 out = append(out, svc.NamespacedName()) 115 } 116 117 sort.Stable(out) 118 return out 119 }