2
0
Эх сурвалжийг харах

Remove the ClusterID -> ClusterName/ClusterId and add ClusterName field on CostData

Matt Bolt 5 жил өмнө
parent
commit
ccf2734669

+ 15 - 0
pkg/costmodel/clusters/clustermap.go

@@ -32,6 +32,9 @@ type ClusterMap interface {
 	// doesn't exist
 	InfoFor(clusterID string) *ClusterInfo
 
+	// NameFor returns the name of the cluster provided the clusterID.
+	NameFor(clusterID string) string
+
 	// NameIDFor returns an identifier in the format "<clusterName>/<clusterID>" if the cluster has an
 	// assigned name. Otherwise, just the clusterID is returned.
 	NameIDFor(clusterID string) string
@@ -195,6 +198,18 @@ func (pcm *PrometheusClusterMap) InfoFor(clusterID string) *ClusterInfo {
 	return nil
 }
 
+// NameFor returns the name of the cluster provided the clusterID.
+func (pcm *PrometheusClusterMap) NameFor(clusterID string) string {
+	pcm.lock.RLock()
+	defer pcm.lock.RUnlock()
+
+	if info, ok := pcm.clusters[clusterID]; ok {
+		return info.Name
+	}
+
+	return ""
+}
+
 // NameIDFor returns an identifier in the format "<clusterName>/<clusterID>" if the cluster has an
 // assigned name. Otherwise, just the clusterID is returned.
 func (pcm *PrometheusClusterMap) NameIDFor(clusterID string) string {

+ 10 - 10
pkg/costmodel/costmodel.go

@@ -86,6 +86,7 @@ type CostData struct {
 	Labels          map[string]string            `json:"labels,omitempty"`
 	NamespaceLabels map[string]string            `json:"namespaceLabels,omitempty"`
 	ClusterID       string                       `json:"clusterId"`
+	ClusterName     string                       `json:"clusterName"`
 }
 
 func (cd *CostData) String() string {
@@ -514,7 +515,8 @@ func (cm *CostModel) ComputeCostData(cli prometheusClient.Client, clientset kube
 					NetworkData:     netReq,
 					Labels:          podLabels,
 					NamespaceLabels: nsLabels,
-					ClusterID:       cm.ClusterMap.NameIDFor(clusterID),
+					ClusterID:       clusterID,
+					ClusterName:     cm.ClusterMap.NameFor(clusterID),
 				}
 				costs.CPUAllocation = getContainerAllocation(costs.CPUReq, costs.CPUUsed, "CPU")
 				costs.RAMAllocation = getContainerAllocation(costs.RAMReq, costs.RAMUsed, "RAM")
@@ -584,7 +586,8 @@ func (cm *CostModel) ComputeCostData(cli prometheusClient.Client, clientset kube
 				CPUUsed:         CPUUsedV,
 				GPUReq:          GPUReqV,
 				NamespaceLabels: namespacelabels,
-				ClusterID:       cm.ClusterMap.NameIDFor(c.ClusterID),
+				ClusterID:       c.ClusterID,
+				ClusterName:     cm.ClusterMap.NameFor(c.ClusterID),
 			}
 			costs.CPUAllocation = getContainerAllocation(costs.CPUReq, costs.CPUUsed, "CPU")
 			costs.RAMAllocation = getContainerAllocation(costs.RAMReq, costs.RAMUsed, "RAM")
@@ -656,7 +659,8 @@ func findUnmountedPVCostData(clusterMap clusters.ClusterMap, unmountedPVs map[st
 				Namespace:       ns,
 				NamespaceLabels: namespacelabels,
 				Labels:          namespacelabels,
-				ClusterID:       clusterMap.NameIDFor(clusterID),
+				ClusterID:       clusterID,
+				ClusterName:     clusterMap.NameFor(clusterID),
 				PVCData:         pv,
 			}
 		} else {
@@ -1398,12 +1402,7 @@ func setToSlice(m map[string]bool) []string {
 
 func costDataPassesFilters(cm clusters.ClusterMap, costs *CostData, namespace string, cluster string) bool {
 	passesNamespace := namespace == "" || costs.Namespace == namespace
-
-	passesCluster := true
-	if cluster != "" {
-		id, name := cm.SplitNameID(costs.ClusterID)
-		passesCluster = id == cluster || name == cluster
-	}
+	passesCluster := cluster == "" || costs.ClusterID == cluster || costs.ClusterName == cluster
 
 	return passesNamespace && passesCluster
 }
@@ -2016,7 +2015,8 @@ func (cm *CostModel) costDataRange(cli prometheusClient.Client, clientset kubern
 			NamespaceLabels: namespaceLabels,
 			PVCData:         podPVs,
 			NetworkData:     podNetCosts,
-			ClusterID:       cm.ClusterMap.NameIDFor(c.ClusterID),
+			ClusterID:       c.ClusterID,
+			ClusterName:     cm.ClusterMap.NameFor(c.ClusterID),
 		}
 
 		if costDataPassesFilters(cm.ClusterMap, costs, filterNamespace, filterCluster) {

+ 1 - 1
pkg/prom/validate.go

@@ -22,7 +22,7 @@ type PrometheusMetadata struct {
 
 // Validate tells the model what data prometheus has on it.
 func Validate(cli prometheus.Client) (*PrometheusMetadata, error) {
-	if IsClientID(cli, ThanosClientID) {
+	if IsThanos(cli) {
 		return validate(cli, thanosValidateQuery)
 	}