github.com/google/cloudprober@v0.11.3/targets/endpoint/endpoint.go (about) 1 // Copyright 2019 The Cloudprober 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 endpoint provides the type Endpoint, to be used with the 16 // targets.Targets interface. 17 package endpoint 18 19 import ( 20 "sort" 21 "strconv" 22 "strings" 23 "time" 24 ) 25 26 // Endpoint represents a targets and associated parameters. 27 type Endpoint struct { 28 Name string 29 Labels map[string]string 30 LastUpdated time.Time 31 Port int 32 } 33 34 // Key returns a string key that uniquely identifies that endpoint. 35 // Endpoint key consists of endpoint name, port and labels. 36 func (ep *Endpoint) Key() string { 37 labelSlice := make([]string, len(ep.Labels)) 38 i := 0 39 for k, v := range ep.Labels { 40 labelSlice[i] = k + ":" + v 41 i++ 42 } 43 sort.Strings(labelSlice) 44 45 return strings.Join(append([]string{ep.Name, strconv.Itoa(ep.Port)}, labelSlice...), "_") 46 } 47 48 // Lister should implement the ListEndpoints method. 49 type Lister interface { 50 // ListEndpoints returns list of endpoints (name, port tupples). 51 ListEndpoints() []Endpoint 52 } 53 54 // EndpointsFromNames is convenience function to build a list of endpoints 55 // from only names. It leaves the Port field in Endpoint unset and initializes 56 // Labels field to an empty map. 57 func EndpointsFromNames(names []string) []Endpoint { 58 result := make([]Endpoint, len(names)) 59 for i, name := range names { 60 result[i].Name = name 61 result[i].Labels = make(map[string]string) 62 } 63 return result 64 } 65 66 // NamesFromEndpoints is convenience function to build a list of names 67 // from endpoints. 68 func NamesFromEndpoints(endpoints []Endpoint) []string { 69 result := make([]string, len(endpoints)) 70 for i, ep := range endpoints { 71 result[i] = ep.Name 72 } 73 return result 74 }