Browse Source

Add support for /clusterInfoMap to allow returning an id -> info map for the client

Matt Bolt 5 years ago
parent
commit
a4a119f04c
2 changed files with 39 additions and 6 deletions
  1. 21 5
      pkg/costmodel/clusters/clustermap.go
  2. 18 1
      pkg/costmodel/router.go

+ 21 - 5
pkg/costmodel/clusters/clustermap.go

@@ -20,14 +20,17 @@ const (
 )
 
 type ClusterInfo struct {
-	ID          string
-	Name        string
-	Profile     string
-	Provider    string
-	Provisioner string
+	ID          string `json:"id"`
+	Name        string `json:"name"`
+	Profile     string `json:"profile"`
+	Provider    string `json:"provider"`
+	Provisioner string `json:"provisioner"`
 }
 
 type ClusterMap interface {
+	// GetClusterIDs returns a slice containing all of the cluster identifiers.
+	GetClusterIDs() []string
+
 	// InfoFor returns the ClusterInfo entry for the provided clusterID or nil if it
 	// doesn't exist
 	InfoFor(clusterID string) *ClusterInfo
@@ -185,6 +188,19 @@ func (pcm *PrometheusClusterMap) refreshClusters() {
 	pcm.lock.Unlock()
 }
 
+// GetClusterIDs returns a slice containing all of the cluster identifiers.
+func (pcm *PrometheusClusterMap) GetClusterIDs() []string {
+	pcm.lock.RLock()
+	defer pcm.lock.RUnlock()
+
+	var clusterIDs []string
+	for id := range pcm.clusters {
+		clusterIDs = append(clusterIDs, id)
+	}
+
+	return clusterIDs
+}
+
 // InfoFor returns the ClusterInfo entry for the provided clusterID or nil if it
 // doesn't exist
 func (pcm *PrometheusClusterMap) InfoFor(clusterID string) *ClusterInfo {

+ 18 - 1
pkg/costmodel/router.go

@@ -621,6 +621,20 @@ func (p *Accesses) ClusterInfo(w http.ResponseWriter, r *http.Request, ps httpro
 	w.Write(WrapData(data, nil))
 }
 
+func (p *Accesses) GetClusterInfoMap(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+	w.Header().Set("Content-Type", "application/json")
+	w.Header().Set("Access-Control-Allow-Origin", "*")
+
+	data := make(map[string]*clusters.ClusterInfo)
+
+	cm := p.ClusterMap
+	for _, id := range cm.GetClusterIDs() {
+		data[id] = cm.InfoFor(id)
+	}
+
+	w.Write(WrapData(data, nil))
+}
+
 func (p *Accesses) GetServiceAccountStatus(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
 	w.Header().Set("Content-Type", "application/json")
 	w.Header().Set("Access-Control-Allow-Origin", "*")
@@ -1008,8 +1022,11 @@ func Initialize(additionalConfigWatchers ...ConfigWatchers) {
 	Router.GET("/validatePrometheus", A.GetPrometheusMetadata)
 	Router.GET("/managementPlatform", A.ManagementPlatform)
 	Router.GET("/clusterInfo", A.ClusterInfo)
-	Router.GET("/clusters", managerEndpoints.GetAllClusters)
+	Router.GET("/clusterInfoMap", A.GetClusterInfoMap)
 	Router.GET("/serviceAccountStatus", A.GetServiceAccountStatus)
+
+	// cluster manager endpoints
+	Router.GET("/clusters", managerEndpoints.GetAllClusters)
 	Router.PUT("/clusters", managerEndpoints.PutCluster)
 	Router.DELETE("/clusters/:id", managerEndpoints.DeleteCluster)
 }