|
|
@@ -68,23 +68,31 @@ type ClusterMap interface {
|
|
|
StopRefresh()
|
|
|
}
|
|
|
|
|
|
+// LocalClusterInfoProvider is a contract which is capable of performing local cluster info lookups.
|
|
|
+type LocalClusterInfoProvider interface {
|
|
|
+ // GetClusterInfo returns a string map containing the local cluster info
|
|
|
+ GetClusterInfo() map[string]string
|
|
|
+}
|
|
|
+
|
|
|
// ClusterMap keeps records of all known cost-model clusters.
|
|
|
type PrometheusClusterMap struct {
|
|
|
- lock *sync.RWMutex
|
|
|
- client prometheus.Client
|
|
|
- clusters map[string]*ClusterInfo
|
|
|
- stop chan struct{}
|
|
|
+ lock *sync.RWMutex
|
|
|
+ client prometheus.Client
|
|
|
+ clusters map[string]*ClusterInfo
|
|
|
+ localCluster LocalClusterInfoProvider
|
|
|
+ stop chan struct{}
|
|
|
}
|
|
|
|
|
|
// NewClusterMap creates a new ClusterMap implementation using a prometheus or thanos client
|
|
|
-func NewClusterMap(client prometheus.Client, refresh time.Duration) ClusterMap {
|
|
|
+func NewClusterMap(client prometheus.Client, lcip LocalClusterInfoProvider, refresh time.Duration) ClusterMap {
|
|
|
stop := make(chan struct{})
|
|
|
|
|
|
cm := &PrometheusClusterMap{
|
|
|
- lock: new(sync.RWMutex),
|
|
|
- client: client,
|
|
|
- clusters: make(map[string]*ClusterInfo),
|
|
|
- stop: stop,
|
|
|
+ lock: new(sync.RWMutex),
|
|
|
+ client: client,
|
|
|
+ clusters: make(map[string]*ClusterInfo),
|
|
|
+ localCluster: lcip,
|
|
|
+ stop: stop,
|
|
|
}
|
|
|
|
|
|
// Run an updater to ensure cluster data stays relevant over time
|
|
|
@@ -175,9 +183,59 @@ func (pcm *PrometheusClusterMap) loadClusters() (map[string]*ClusterInfo, error)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ if len(clusters) == 0 {
|
|
|
+ localInfo, err := pcm.getLocalClusterInfo()
|
|
|
+ if err != nil {
|
|
|
+ log.Warningf("Failed to load local cluster info: %s", err)
|
|
|
+ } else {
|
|
|
+ clusters[localInfo.ID] = localInfo
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return clusters, nil
|
|
|
}
|
|
|
|
|
|
+// getLocalClusterInfo returns the local cluster info in the event there does not exist a metric available.
|
|
|
+func (pcm *PrometheusClusterMap) getLocalClusterInfo() (*ClusterInfo, error) {
|
|
|
+ info := pcm.localCluster.GetClusterInfo()
|
|
|
+
|
|
|
+ var id string
|
|
|
+ var name string
|
|
|
+
|
|
|
+ if i, ok := info["id"]; ok {
|
|
|
+ id = i
|
|
|
+ } else {
|
|
|
+ return nil, fmt.Errorf("Local Cluster Info Missing ID")
|
|
|
+ }
|
|
|
+ if n, ok := info["name"]; ok {
|
|
|
+ name = n
|
|
|
+ } else {
|
|
|
+ return nil, fmt.Errorf("Local Cluster Info Missing Name")
|
|
|
+ }
|
|
|
+
|
|
|
+ var clusterProfile string
|
|
|
+ var provider string
|
|
|
+ var provisioner string
|
|
|
+
|
|
|
+ if cp, ok := info["clusterProfile"]; ok {
|
|
|
+ clusterProfile = cp
|
|
|
+ }
|
|
|
+ if pvdr, ok := info["provider"]; ok {
|
|
|
+ provider = pvdr
|
|
|
+ }
|
|
|
+ if pvsr, ok := info["provisioner"]; ok {
|
|
|
+ provisioner = pvsr
|
|
|
+ }
|
|
|
+
|
|
|
+ return &ClusterInfo{
|
|
|
+ ID: id,
|
|
|
+ Name: name,
|
|
|
+ Profile: clusterProfile,
|
|
|
+ Provider: provider,
|
|
|
+ Provisioner: provisioner,
|
|
|
+ }, nil
|
|
|
+}
|
|
|
+
|
|
|
// refreshClusters loads the clusters and updates the internal map
|
|
|
func (pcm *PrometheusClusterMap) refreshClusters() {
|
|
|
updated, err := pcm.loadClusters()
|