瀏覽代碼

Resolve merge conflicts

Niko Kovacevic 6 年之前
父節點
當前提交
c68c4ce0e1
共有 3 個文件被更改,包括 37 次插入38 次删除
  1. 1 1
      deploying-as-a-pod.md
  2. 15 30
      pkg/cloud/awsprovider.go
  3. 21 7
      pkg/costmodel/cluster.go

+ 1 - 1
deploying-as-a-pod.md

@@ -2,7 +2,7 @@
 
 See this page for all [Kubecost install options](http://docs.kubecost.com/install).
 
-If you would like to deploy the cost model (w/o dashboards) directly a pod on your cluster, complete the listed below. 
+If you would like to deploy the cost model (w/o dashboards) directly a pod on your cluster, complete the steps listed below. 
 
 1. Set [this environment variable](https://github.com/kubecost/cost-model/blob/master/kubernetes/deployment.yaml#L30) to the address of your prometheus server
 2. `kubectl create namespace cost-model`

+ 15 - 30
pkg/cloud/awsprovider.go

@@ -928,33 +928,18 @@ func (*AWS) AddServiceKey(formValues url.Values) error {
 	return ioutil.WriteFile("/var/configs/key.json", result, 0644)
 }
 
-func configureAWSAuth(keyFile string) error {
-	jsonFile, err := os.Open(keyFile)
-	if err != nil {
-		if os.IsNotExist(err) {
-			klog.V(2).Infof("Using Default Credentials")
-			return nil
+func (aws *AWS) configureAWSAuth() error {
+	accessKeyID := aws.ServiceKeyName
+	accessKeySecret := aws.ServiceKeySecret
+	if accessKeyID != "" && accessKeySecret != "" { // credentials may exist on the actual AWS node-- if so, use those. If not, override with the service key
+		err := os.Setenv(awsAccessKeyIDEnvVar, accessKeyID)
+		if err != nil {
+			return err
+		}
+		err = os.Setenv(awsAccessKeySecretEnvVar, accessKeySecret)
+		if err != nil {
+			return err
 		}
-
-		return err
-	}
-	defer jsonFile.Close()
-
-	byteValue, _ := ioutil.ReadAll(jsonFile)
-	var result map[string]string
-	err = json.Unmarshal([]byte(byteValue), &result)
-	if err != nil {
-		return err
-	}
-
-	err = os.Setenv(awsAccessKeyIDEnvVar, result["awsServiceKeyName"])
-	if err != nil {
-		return err
-	}
-
-	err = os.Setenv(awsAccessKeySecretEnvVar, result["awsServiceKeySecret"])
-	if err != nil {
-		return err
 	}
 	return nil
 }
@@ -979,8 +964,8 @@ func getClusterConfig(ccFile string) (map[string]string, error) {
 }
 
 // GetDisks returns the AWS disks backing PVs. Useful because sometimes k8s will not clean up PVs correctly. Requires a json config in /var/configs with key region.
-func (*AWS) GetDisks() ([]byte, error) {
-	err := configureAWSAuth("/var/configs/key.json")
+func (a *AWS) GetDisks() ([]byte, error) {
+	err := a.configureAWSAuth()
 	if err != nil {
 		return nil, err
 	}
@@ -1701,9 +1686,9 @@ func getRegionReservedInstances(region string) ([]*AWSReservedInstance, error) {
 }
 
 func (a *AWS) getReservedInstances() ([]*AWSReservedInstance, error) {
-	err := configureAWSAuth("/var/configs/aws.json")
+	err := a.configureAWSAuth()
 	if err != nil {
-		return nil, err
+		return nil, fmt.Errorf("Error Configuring aws auth: %s", err.Error())
 	}
 
 	var reservedInstances []*AWSReservedInstance

+ 21 - 7
pkg/costmodel/cluster.go

@@ -133,7 +133,7 @@ func ComputeClusterCosts(client prometheus.Client, provider cloud.Provider, wind
 	}
 	mins := end.Sub(*start).Minutes()
 
-	const fmtQueryDataCount = `count_over_time(sum(kube_node_status_capacity_cpu_cores)[%s:1m]%s)`
+	const fmtQueryDataCount = `count_over_time(sum(kube_node_status_capacity_cpu_cores) by (cluster_id)[%s:1m]%s)`
 
 	const fmtQueryTotalGPU = `sum(
 		sum_over_time(node_gpu_hourly_cost[%s:1m]%s) / 60
@@ -267,11 +267,21 @@ func ComputeClusterCosts(client prometheus.Client, provider cloud.Provider, wind
 	resultsUsedLocalStorage := <-chUsedLocalStorage
 	close(chUsedLocalStorage)
 
-	dataMins := mins
-	if len(resultsDataCount) > 0 && len(resultsDataCount[0].Values) > 0 {
-		dataMins = resultsDataCount[0].Values[0].Value
-	} else {
-		klog.V(3).Infof("[Warning] cluster cost data count returned no results")
+	defaultClusterID := os.Getenv(clusterIDKey)
+
+	dataMinsByCluster := map[string]float64{}
+	for _, result := range resultsDataCount {
+		clusterID, _ := result.GetString("cluster_id")
+		if clusterID == "" {
+			clusterID = defaultClusterID
+		}
+		dataMins := mins
+		if len(result.Values) > 0 {
+			dataMins = result.Values[0].Value
+		} else {
+			klog.V(3).Infof("[Warning] cluster cost data count returned no results for cluster %s", clusterID)
+		}
+		dataMinsByCluster[clusterID] = dataMins
 	}
 
 	// Determine combined discount
@@ -290,7 +300,6 @@ func ComputeClusterCosts(client prometheus.Client, provider cloud.Provider, wind
 
 	// Intermediate structure storing mapping of [clusterID][type ∈ {cpu, ram, storage, total}]=cost
 	costData := make(map[string]map[string]float64)
-	defaultClusterID := os.Getenv(clusterIDKey)
 
 	// Helper function to iterate over Prom query results, parsing the raw values into
 	// the intermediate costData structure.
@@ -387,6 +396,11 @@ func ComputeClusterCosts(client prometheus.Client, provider cloud.Provider, wind
 	// Convert intermediate structure to Costs instances
 	costsByCluster := map[string]*ClusterCosts{}
 	for id, cd := range costData {
+		dataMins, ok := dataMinsByCluster[id]
+		if !ok {
+			dataMins = mins
+			klog.V(3).Infof("[Warning] cluster cost data count not found for cluster %s", id)
+		}
 		costs, err := NewClusterCostsFromCumulative(cd["cpu"], cd["gpu"], cd["ram"], cd["storage"], window, offset, dataMins/util.MinsPerHour)
 		if err != nil {
 			klog.V(3).Infof("[Warning] Failed to parse cluster costs on %s (%s) from cumulative data: %+v", window, offset, cd)