Kaynağa Gözat

Add default clusterID and fix bug with local cluster in DataRange

Matt Bolt 6 yıl önce
ebeveyn
işleme
2d521f260c
2 değiştirilmiş dosya ile 23 ekleme ve 21 silme
  1. 11 9
      costmodel/costmodel.go
  2. 12 12
      costmodel/promparsers.go

+ 11 - 9
costmodel/costmodel.go

@@ -1428,7 +1428,7 @@ func (cm *CostModel) ComputeCostDataRange(cli prometheusClient.Client, clientset
 		addMetricPVData(pvAllocationMapping, pvCostMapping, cp)
 	}
 
-	nsLabels, err := GetNamespaceLabelsMetrics(nsLabelsResults)
+	nsLabels, err := GetNamespaceLabelsMetrics(nsLabelsResults, clusterID)
 	if err != nil {
 		klog.V(1).Infof("Unable to get Namespace Labels for Metrics: %s", err.Error())
 	}
@@ -1436,17 +1436,17 @@ func (cm *CostModel) ComputeCostDataRange(cli prometheusClient.Client, clientset
 		appendNamespaceLabels(namespaceLabelsMapping, nsLabels)
 	}
 
-	podLabels, err := GetPodLabelsMetrics(podLabelsResults)
+	podLabels, err := GetPodLabelsMetrics(podLabelsResults, clusterID)
 	if err != nil {
 		klog.V(1).Infof("Unable to get Pod Labels for Metrics: %s", err.Error())
 	}
 
-	serviceLabels, err := GetServiceSelectorLabelsMetrics(serviceLabelsResults)
+	serviceLabels, err := GetServiceSelectorLabelsMetrics(serviceLabelsResults, clusterID)
 	if err != nil {
 		klog.V(1).Infof("Unable to get Service Selector Labels for Metrics: %s", err.Error())
 	}
 
-	deploymentLabels, err := GetDeploymentMatchLabelsMetrics(deploymentLabelsResults)
+	deploymentLabels, err := GetDeploymentMatchLabelsMetrics(deploymentLabelsResults, clusterID)
 	if err != nil {
 		klog.V(1).Infof("Unable to get Deployment Match Labels for Metrics: %s", err.Error())
 	}
@@ -1538,9 +1538,11 @@ func (cm *CostModel) ComputeCostDataRange(cli prometheusClient.Client, clientset
 			if _, ok := nodes[nodeName]; ok {
 				nodeData = nodes[nodeName]
 			}
+
+			nsKey := ns + "," + clusterID
 			var podDeployments []string
-			if _, ok := podDeploymentsMapping[ns]; ok {
-				if ds, ok := podDeploymentsMapping[ns][pod.GetObjectMeta().GetName()]; ok {
+			if _, ok := podDeploymentsMapping[nsKey]; ok {
+				if ds, ok := podDeploymentsMapping[nsKey][pod.GetObjectMeta().GetName()]; ok {
 					podDeployments = ds
 				} else {
 					podDeployments = []string{}
@@ -1569,15 +1571,15 @@ func (cm *CostModel) ComputeCostDataRange(cli prometheusClient.Client, clientset
 			}
 
 			var podServices []string
-			if _, ok := podServicesMapping[ns]; ok {
-				if svcs, ok := podServicesMapping[ns][pod.GetObjectMeta().GetName()]; ok {
+			if _, ok := podServicesMapping[nsKey]; ok {
+				if svcs, ok := podServicesMapping[nsKey][pod.GetObjectMeta().GetName()]; ok {
 					podServices = svcs
 				} else {
 					podServices = []string{}
 				}
 			}
 
-			nsLabels := namespaceLabelsMapping[ns+","+clusterID]
+			nsLabels := namespaceLabelsMapping[nsKey]
 			podLabels := pod.GetObjectMeta().GetLabels()
 
 			if podLabels == nil {

+ 12 - 12
costmodel/promparsers.go

@@ -230,7 +230,7 @@ func GetPVCostMetrics(queryResult interface{}, defaultClusterID string) (map[str
 	return toReturn, nil
 }
 
-func GetNamespaceLabelsMetrics(queryResult interface{}) (map[string]map[string]string, error) {
+func GetNamespaceLabelsMetrics(queryResult interface{}, defaultClusterID string) (map[string]map[string]string, error) {
 	toReturn := make(map[string]map[string]string)
 	result, err := NewQueryResults(queryResult)
 	if err != nil {
@@ -245,8 +245,8 @@ func GetNamespaceLabelsMetrics(queryResult interface{}) (map[string]map[string]s
 		}
 
 		clusterID, err := val.GetString("cluster_id")
-		if err != nil {
-			return toReturn, err
+		if clusterID == "" {
+			clusterID = defaultClusterID
 		}
 
 		nsKey := ns + "," + clusterID
@@ -256,7 +256,7 @@ func GetNamespaceLabelsMetrics(queryResult interface{}) (map[string]map[string]s
 	return toReturn, nil
 }
 
-func GetPodLabelsMetrics(queryResult interface{}) (map[string]map[string]string, error) {
+func GetPodLabelsMetrics(queryResult interface{}, defaultClusterID string) (map[string]map[string]string, error) {
 	toReturn := make(map[string]map[string]string)
 	result, err := NewQueryResults(queryResult)
 	if err != nil {
@@ -276,8 +276,8 @@ func GetPodLabelsMetrics(queryResult interface{}) (map[string]map[string]string,
 		}
 
 		clusterID, err := val.GetString("cluster_id")
-		if err != nil {
-			return toReturn, err
+		if clusterID == "" {
+			clusterID = defaultClusterID
 		}
 
 		nsKey := ns + "," + pod + "," + clusterID
@@ -287,7 +287,7 @@ func GetPodLabelsMetrics(queryResult interface{}) (map[string]map[string]string,
 	return toReturn, nil
 }
 
-func GetDeploymentMatchLabelsMetrics(queryResult interface{}) (map[string]map[string]string, error) {
+func GetDeploymentMatchLabelsMetrics(queryResult interface{}, defaultClusterID string) (map[string]map[string]string, error) {
 	toReturn := make(map[string]map[string]string)
 	result, err := NewQueryResults(queryResult)
 	if err != nil {
@@ -307,8 +307,8 @@ func GetDeploymentMatchLabelsMetrics(queryResult interface{}) (map[string]map[st
 		}
 
 		clusterID, err := val.GetString("cluster_id")
-		if err != nil {
-			return toReturn, err
+		if clusterID == "" {
+			clusterID = defaultClusterID
 		}
 
 		nsKey := ns + "," + deployment + "," + clusterID
@@ -318,7 +318,7 @@ func GetDeploymentMatchLabelsMetrics(queryResult interface{}) (map[string]map[st
 	return toReturn, nil
 }
 
-func GetServiceSelectorLabelsMetrics(queryResult interface{}) (map[string]map[string]string, error) {
+func GetServiceSelectorLabelsMetrics(queryResult interface{}, defaultClusterID string) (map[string]map[string]string, error) {
 	toReturn := make(map[string]map[string]string)
 	result, err := NewQueryResults(queryResult)
 	if err != nil {
@@ -338,8 +338,8 @@ func GetServiceSelectorLabelsMetrics(queryResult interface{}) (map[string]map[st
 		}
 
 		clusterID, err := val.GetString("cluster_id")
-		if err != nil {
-			return toReturn, err
+		if clusterID == "" {
+			clusterID = defaultClusterID
 		}
 
 		nsKey := ns + "," + service + "," + clusterID