فهرست منبع

safer querying

AjayTripathy 6 سال پیش
والد
کامیت
6794ca449c
2فایلهای تغییر یافته به همراه34 افزوده شده و 31 حذف شده
  1. 0 6
      cloud/gcpprovider.go
  2. 34 25
      costmodel/costmodel.go

+ 0 - 6
cloud/gcpprovider.go

@@ -621,12 +621,6 @@ func (gcp *GCP) parsePages(inputKeys map[string]Key, pvKeys map[string]PVKey) (m
 		return nil, err
 		return nil, err
 	}
 	}
 	returnPages := make(map[string]*GCPPricing)
 	returnPages := make(map[string]*GCPPricing)
-	for _, page := range pages {
-		klog.V(1).Infof("Page: %s : %+v", page)
-		for k, v := range page {
-			klog.V(1).Infof("Unmerged Page: %s : %+v", k, v)
-		}
-	}
 	for _, page := range pages {
 	for _, page := range pages {
 		for k, v := range page {
 		for k, v := range page {
 			if val, ok := returnPages[k]; ok { //keys may need to be merged
 			if val, ok := returnPages[k]; ok { //keys may need to be merged

+ 34 - 25
costmodel/costmodel.go

@@ -646,33 +646,39 @@ func (cm *CostModel) ComputeCostData(cli prometheusClient.Client, clientset kube
 	err = findDeletedNodeInfo(cli, missingNodes, window)
 	err = findDeletedNodeInfo(cli, missingNodes, window)
 
 
 	if err != nil {
 	if err != nil {
-		return nil, err
+		klog.V(1).Infof("Error fetching historical node data: %s", err.Error())
 	}
 	}
 	err = findDeletedPodInfo(cli, missingContainers, window)
 	err = findDeletedPodInfo(cli, missingContainers, window)
 	if err != nil {
 	if err != nil {
-		return nil, err
+		klog.V(1).Infof("Error fetching historical pod data: %s", err.Error())
 	}
 	}
 	return containerNameCost, err
 	return containerNameCost, err
 }
 }
 
 
 func findDeletedPodInfo(cli prometheusClient.Client, missingContainers map[string]*CostData, window string) error {
 func findDeletedPodInfo(cli prometheusClient.Client, missingContainers map[string]*CostData, window string) error {
 	if len(missingContainers) > 0 {
 	if len(missingContainers) > 0 {
-		q := make([]string, 0, len(missingContainers))
-		for key := range missingContainers {
-			cm, _ := NewContainerMetricFromKey(key)
-			q = append(q, cm.PodName)
-		}
-		l := strings.Join(q, "|")
-		queryHistoricalPodLabels := fmt.Sprintf(`kube_pod_labels{pod=~"%s"}[%s]`, l, window)
+		queryHistoricalPodLabels := fmt.Sprintf(`kube_pod_labels{}[%s]`, window)
 
 
 		podLabelsResult, err := Query(cli, queryHistoricalPodLabels)
 		podLabelsResult, err := Query(cli, queryHistoricalPodLabels)
 		if err != nil {
 		if err != nil {
+<<<<<<< Updated upstream
 			return fmt.Errorf("Error fetching historical pod labels: %s", err.Error())
 			return fmt.Errorf("Error fetching historical pod labels: %s", err.Error())
 		}
 		}
 		podLabels, err := labelsFromPrometheusQuery(podLabelsResult)
 		podLabels, err := labelsFromPrometheusQuery(podLabelsResult)
 		if err != nil {
 		if err != nil {
 			klog.V(1).Infof("Error parsing historical labels: %s", err.Error())
 			klog.V(1).Infof("Error parsing historical labels: %s", err.Error())
 		}
 		}
+=======
+			klog.V(1).Infof("Error parsing historical labels: %s", err.Error())
+		}
+		podLabels := make(map[string]map[string]string)
+		if podLabelsResult != nil {
+			podLabels, err := labelsFromPrometheusQuery(podLabelsResult)
+			if err != nil {
+				klog.V(1).Infof("Error parsing historical labels: %s", err.Error())
+			}
+		}
+>>>>>>> Stashed changes
 		for key, costData := range missingContainers {
 		for key, costData := range missingContainers {
 			cm, _ := NewContainerMetricFromKey(key)
 			cm, _ := NewContainerMetricFromKey(key)
 			labels, ok := podLabels[cm.PodName]
 			labels, ok := podLabels[cm.PodName]
@@ -693,15 +699,15 @@ func findDeletedPodInfo(cli prometheusClient.Client, missingContainers map[strin
 }
 }
 
 
 func labelsFromPrometheusQuery(qr interface{}) (map[string]map[string]string, error) {
 func labelsFromPrometheusQuery(qr interface{}) (map[string]map[string]string, error) {
+	toReturn := make(map[string]map[string]string)
 	data, ok := qr.(map[string]interface{})["data"]
 	data, ok := qr.(map[string]interface{})["data"]
 	if !ok {
 	if !ok {
 		e, err := wrapPrometheusError(qr)
 		e, err := wrapPrometheusError(qr)
 		if err != nil {
 		if err != nil {
-			return nil, err
+			return toReturn, err
 		}
 		}
-		return nil, fmt.Errorf(e)
+		return toReturn, fmt.Errorf(e)
 	}
 	}
-	toReturn := make(map[string]map[string]string)
 	for _, val := range data.(map[string]interface{})["result"].([]interface{}) {
 	for _, val := range data.(map[string]interface{})["result"].([]interface{}) {
 		metricInterface, ok := val.(map[string]interface{})["metric"]
 		metricInterface, ok := val.(map[string]interface{})["metric"]
 		if !ok {
 		if !ok {
@@ -1534,11 +1540,11 @@ func (cm *CostModel) ComputeCostDataRange(cli prometheusClient.Client, clientset
 		wStr := fmt.Sprintf("%dm", int(w.Minutes()))
 		wStr := fmt.Sprintf("%dm", int(w.Minutes()))
 		err = findDeletedNodeInfo(cli, missingNodes, wStr)
 		err = findDeletedNodeInfo(cli, missingNodes, wStr)
 		if err != nil {
 		if err != nil {
-			return nil, err
+			klog.V(1).Infof("Error fetching historical node data: %s", err.Error())
 		}
 		}
 		err = findDeletedPodInfo(cli, missingContainers, wStr)
 		err = findDeletedPodInfo(cli, missingContainers, wStr)
 		if err != nil {
 		if err != nil {
-			return nil, err
+			klog.V(1).Infof("Error fetching historical pod data: %s", err.Error())
 		}
 		}
 	}
 	}
 
 
@@ -1829,22 +1835,22 @@ func QueryRange(cli prometheusClient.Client, query string, start, end time.Time,
 	q.Set("step", strconv.FormatFloat(step.Seconds(), 'f', 3, 64))
 	q.Set("step", strconv.FormatFloat(step.Seconds(), 'f', 3, 64))
 	u.RawQuery = q.Encode()
 	u.RawQuery = q.Encode()
 
 
-	req, err := http.NewRequest(http.MethodGet, u.String(), nil)
+	req, err := http.NewRequest(http.MethodPost, u.String(), nil)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
 
 
-	_, body, _, err := cli.Do(context.Background(), req)
-	if err != nil {
-		klog.V(1).Infof("ERROR" + err.Error())
+	resp, body, warnings, err := cli.Do(context.Background(), req)
+	for _, w := range warnings {
+		klog.V(3).Infof("%s", w)
 	}
 	}
 	if err != nil {
 	if err != nil {
-		return nil, err
+		return nil, fmt.Errorf("%s Error %s fetching query %s", resp.StatusCode, err.Error(), query)
 	}
 	}
 	var toReturn interface{}
 	var toReturn interface{}
 	err = json.Unmarshal(body, &toReturn)
 	err = json.Unmarshal(body, &toReturn)
 	if err != nil {
 	if err != nil {
-		klog.V(1).Infof("ERROR" + err.Error())
+		return nil, fmt.Errorf("Error %s fetching query %s", err.Error(), query)
 	}
 	}
 	return toReturn, err
 	return toReturn, err
 }
 }
@@ -1855,21 +1861,24 @@ func Query(cli prometheusClient.Client, query string) (interface{}, error) {
 	q.Set("query", query)
 	q.Set("query", query)
 	u.RawQuery = q.Encode()
 	u.RawQuery = q.Encode()
 
 
-	req, err := http.NewRequest(http.MethodGet, u.String(), nil)
+	req, err := http.NewRequest(http.MethodPost, u.String(), nil)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
 
 
-	_, body, _, err := cli.Do(context.Background(), req)
+	resp, body, warnings, err := cli.Do(context.Background(), req)
+	for _, w := range warnings {
+		klog.V(3).Infof("%s", w)
+	}
 	if err != nil {
 	if err != nil {
-		return nil, err
+		return nil, fmt.Errorf("%s Error %s fetching query %s", resp.StatusCode, err.Error(), query)
 	}
 	}
 	var toReturn interface{}
 	var toReturn interface{}
 	err = json.Unmarshal(body, &toReturn)
 	err = json.Unmarshal(body, &toReturn)
 	if err != nil {
 	if err != nil {
-		klog.V(1).Infof("ERROR" + err.Error())
+		return nil, fmt.Errorf("Error %s fetching query %s", err.Error(), query)
 	}
 	}
-	return toReturn, err
+	return toReturn, nil
 }
 }
 
 
 //todo: don't cast, implement unmarshaler interface
 //todo: don't cast, implement unmarshaler interface