From 19be17ee667270892f4a21fc1928fa5c498a6e45 Mon Sep 17 00:00:00 2001 From: Karol Wychowaniec Date: Wed, 4 Jul 2018 11:27:07 +0200 Subject: [PATCH] Add an option to read Host ID from node annotation if ExternalID is not available --- events/sinks/factory.go | 2 +- metrics/sources/summary/summary.go | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/events/sinks/factory.go b/events/sinks/factory.go index 5ba260137d..5759ba8f0d 100644 --- a/events/sinks/factory.go +++ b/events/sinks/factory.go @@ -59,7 +59,7 @@ func (this *SinkFactory) BuildAll(uris flags.Uris) []core.EventSink { for _, uri := range uris { sink, err := this.Build(uri) if err != nil { - glog.Errorf("Failed to create sink: %v", err) + glog.Errorf("Failed to create %v sink: %v", uri, err) continue } result = append(result, sink) diff --git a/metrics/sources/summary/summary.go b/metrics/sources/summary/summary.go index 434fe101ea..52083bb9a0 100644 --- a/metrics/sources/summary/summary.go +++ b/metrics/sources/summary/summary.go @@ -385,9 +385,10 @@ func (this *summaryMetricsSource) getSystemContainerName(c *stats.ContainerStats // TODO: The summaryProvider duplicates a lot of code from kubeletProvider, and should be refactored. type summaryProvider struct { - nodeLister v1listers.NodeLister - reflector *cache.Reflector - kubeletClient *kubelet.KubeletClient + nodeLister v1listers.NodeLister + reflector *cache.Reflector + kubeletClient *kubelet.KubeletClient + hostIDAnnotation string } func (this *summaryProvider) GetMetricsSources() []MetricsSource { @@ -418,10 +419,14 @@ func (this *summaryProvider) getNodeInfo(node *kube_api.Node) (NodeInfo, error) if hostname == "" { hostname = node.Name } + hostID := node.Spec.ExternalID + if hostID == "" && this.hostIDAnnotation != "" { + hostID = node.Annotations[this.hostIDAnnotation] + } info := NodeInfo{ NodeName: node.Name, HostName: hostname, - HostID: node.Spec.ExternalID, + HostID: hostID, Host: kubelet.Host{ IP: ip, Port: this.kubeletClient.GetPort(), @@ -432,6 +437,12 @@ func (this *summaryProvider) getNodeInfo(node *kube_api.Node) (NodeInfo, error) } func NewSummaryProvider(uri *url.URL) (MetricsSourceProvider, error) { + opts := uri.Query() + + hostIDAnnotation := "" + if len(opts["host_id_annotation"]) > 0 { + hostIDAnnotation = opts["host_id_annotation"][0] + } // create clients kubeConfig, kubeletConfig, err := kubelet.GetKubeConfigs(uri) if err != nil { @@ -446,8 +457,9 @@ func NewSummaryProvider(uri *url.URL) (MetricsSourceProvider, error) { nodeLister, reflector, _ := util.GetNodeLister(kubeClient) return &summaryProvider{ - nodeLister: nodeLister, - reflector: reflector, - kubeletClient: kubeletClient, + nodeLister: nodeLister, + reflector: reflector, + kubeletClient: kubeletClient, + hostIDAnnotation: hostIDAnnotation, }, nil }