github.com/jonaz/heapster@v1.3.0-beta.0.0.20170208112634-cd3c15ca3d29/metrics/util/util.go (about) 1 // Copyright 2015 Google Inc. All Rights Reserved. 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 util 16 17 import ( 18 "fmt" 19 kube_api "k8s.io/kubernetes/pkg/api" 20 "k8s.io/kubernetes/pkg/client/cache" 21 kube_client "k8s.io/kubernetes/pkg/client/unversioned" 22 "k8s.io/kubernetes/pkg/fields" 23 "sort" 24 "strings" 25 "time" 26 ) 27 28 var labelSeperator string 29 30 // Concatenates a map of labels into a Seperator-seperated key:value pairs. 31 func LabelsToString(labels map[string]string) string { 32 output := make([]string, 0, len(labels)) 33 for key, value := range labels { 34 output = append(output, fmt.Sprintf("%s:%s", key, value)) 35 } 36 37 // Sort to produce a stable output. 38 sort.Strings(output) 39 return strings.Join(output, labelSeperator) 40 } 41 42 func CopyLabels(labels map[string]string) map[string]string { 43 c := make(map[string]string, len(labels)) 44 for key, val := range labels { 45 c[key] = val 46 } 47 return c 48 } 49 50 func GetLatest(a, b time.Time) time.Time { 51 if a.After(b) { 52 return a 53 } 54 return b 55 } 56 57 func SetLabelSeperator(seperator string) { 58 labelSeperator = seperator 59 } 60 61 func GetNodeLister(kubeClient *kube_client.Client) (*cache.StoreToNodeLister, *cache.Reflector, error) { 62 lw := cache.NewListWatchFromClient(kubeClient, "nodes", kube_api.NamespaceAll, fields.Everything()) 63 nodeLister := &cache.StoreToNodeLister{Store: cache.NewStore(cache.MetaNamespaceKeyFunc)} 64 reflector := cache.NewReflector(lw, &kube_api.Node{}, nodeLister.Store, time.Hour) 65 reflector.Run() 66 return nodeLister, reflector, nil 67 }