Browse Source

configuration fixes

AjayTripathy 7 years ago
parent
commit
e4eb94edd9
3 changed files with 68 additions and 28 deletions
  1. 15 3
      cloud/awsprovider.go
  2. 40 12
      cloud/provider.go
  3. 13 13
      costmodel/costmodel.go

+ 15 - 3
cloud/awsprovider.go

@@ -165,6 +165,8 @@ type AwsSpotFeedInfo struct {
 	AccountID        string `json:"accountId"`
 	AccountID        string `json:"accountId"`
 	ServiceKeyName   string `json:"serviceKeyName"`
 	ServiceKeyName   string `json:"serviceKeyName"`
 	ServiceKeySecret string `json:"serviceKeySecret"`
 	ServiceKeySecret string `json:"serviceKeySecret"`
+	SpotLabel        string `json:"spotLabel"`
+	SpotLabelValue   string `json:"spotLabelValue"`
 }
 }
 
 
 func (aws *AWS) GetConfig() (*CustomPricing, error) {
 func (aws *AWS) GetConfig() (*CustomPricing, error) {
@@ -192,13 +194,19 @@ func (aws *AWS) UpdateConfig(r io.Reader) (*CustomPricing, error) {
 	c.SpotDataBucket = a.BucketName
 	c.SpotDataBucket = a.BucketName
 	c.ProjectID = a.AccountID
 	c.ProjectID = a.AccountID
 	c.SpotDataRegion = a.Region
 	c.SpotDataRegion = a.Region
+	c.SpotLabel = a.SpotLabel
+	c.SpotLabelValue = a.SpotLabelValue
 
 
 	cj, err := json.Marshal(c)
 	cj, err := json.Marshal(c)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
-
-	err = ioutil.WriteFile("/models/aws.json", cj, 0644)
+	path := os.Getenv("CONFIG_PATH")
+	if path == "" {
+		path = "/models/"
+	}
+	path += "aws.json"
+	err = ioutil.WriteFile(path, cj, 0644)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
@@ -460,7 +468,11 @@ func (aws *AWS) createNode(terms *AWSProductTerms, usageType string, k Key) (*No
 			UsageType:    usageType,
 			UsageType:    usageType,
 		}, nil
 		}, nil
 	}
 	}
-	cost := terms.OnDemand.PriceDimensions[terms.Sku+OnDemandRateCode+HourlyRateCode].PricePerUnit.USD
+	c, ok  := terms.OnDemand.PriceDimensions[terms.Sku+OnDemandRateCode+HourlyRateCode]
+	if !ok {
+		return nil, fmt.Errorf("Could not fetch data for \"%s\"", k.ID())
+	}
+	cost := c.PricePerUnit.USD
 	return &Node{
 	return &Node{
 		Cost:         cost,
 		Cost:         cost,
 		VCPU:         terms.VCpu,
 		VCPU:         terms.VCpu,

+ 40 - 12
cloud/provider.go

@@ -69,21 +69,49 @@ type Provider interface {
 
 
 // GetDefaultPricingData will search for a json file representing pricing data in /models/ and use it for base pricing info.
 // GetDefaultPricingData will search for a json file representing pricing data in /models/ and use it for base pricing info.
 func GetDefaultPricingData(fname string) (*CustomPricing, error) {
 func GetDefaultPricingData(fname string) (*CustomPricing, error) {
-	jsonFile, err := os.Open("/models/" + fname)
-	if err != nil {
-		return nil, err
+	path := os.Getenv("CONFIG_PATH")
+	if path == "" {
+		path = "/models/"
 	}
 	}
-	defer jsonFile.Close()
-	byteValue, err := ioutil.ReadAll(jsonFile)
-	if err != nil {
-		return nil, err
-	}
-	var customPricing = &CustomPricing{}
-	err = json.Unmarshal([]byte(byteValue), customPricing)
-	if err != nil {
+	path += fname
+	if _, err := os.Stat(path); err == nil {
+		jsonFile, err := os.Open(path)
+		if err != nil {
+			return nil, err
+		}
+		defer jsonFile.Close()
+		byteValue, err := ioutil.ReadAll(jsonFile)
+		if err != nil {
+			return nil, err
+		}
+		var customPricing = &CustomPricing{}
+		err = json.Unmarshal([]byte(byteValue), customPricing)
+		if err != nil {
+			return nil, err
+		}
+		return customPricing, nil
+	} else if os.IsNotExist(err) {
+		c := &CustomPricing{
+			Provider:    fname,
+			Description: "Default prices based on GCP us-central1",
+			CPU:         "0.031611",
+			SpotCPU:     "0.006655",
+			RAM:         "0.004237",
+			SpotRAM:     "0.000892",
+		}
+		cj, err := json.Marshal(c)
+		if err != nil {
+			return nil, err
+		}
+
+		err = ioutil.WriteFile(path, cj, 0644)
+		if err != nil {
+			return nil, err
+		}
+		return c, nil
+	} else {
 		return nil, err
 		return nil, err
 	}
 	}
-	return customPricing, nil
 }
 }
 
 
 type CustomPricing struct {
 type CustomPricing struct {

+ 13 - 13
costmodel/costmodel.go

@@ -286,27 +286,27 @@ func ComputeCostData(cli prometheusClient.Client, clientset kubernetes.Interface
 
 
 				RAMReqV, ok := RAMReqMap[newKey]
 				RAMReqV, ok := RAMReqMap[newKey]
 				if !ok {
 				if !ok {
-					klog.V(2).Info("no RAM requests for " + newKey)
+					klog.V(4).Info("no RAM requests for " + newKey)
 					RAMReqV = []*Vector{&Vector{}}
 					RAMReqV = []*Vector{&Vector{}}
 				}
 				}
 				RAMUsedV, ok := RAMUsedMap[newKey]
 				RAMUsedV, ok := RAMUsedMap[newKey]
 				if !ok {
 				if !ok {
-					klog.V(2).Info("no RAM usage for " + newKey)
+					klog.V(4).Info("no RAM usage for " + newKey)
 					RAMUsedV = []*Vector{&Vector{}}
 					RAMUsedV = []*Vector{&Vector{}}
 				}
 				}
 				CPUReqV, ok := CPUReqMap[newKey]
 				CPUReqV, ok := CPUReqMap[newKey]
 				if !ok {
 				if !ok {
-					klog.V(2).Info("no CPU requests for " + newKey)
+					klog.V(4).Info("no CPU requests for " + newKey)
 					CPUReqV = []*Vector{&Vector{}}
 					CPUReqV = []*Vector{&Vector{}}
 				}
 				}
 				GPUReqV, ok := GPUReqMap[newKey]
 				GPUReqV, ok := GPUReqMap[newKey]
 				if !ok {
 				if !ok {
-					klog.V(2).Info("no GPU requests for " + newKey)
+					klog.V(4).Info("no GPU requests for " + newKey)
 					GPUReqV = []*Vector{&Vector{}}
 					GPUReqV = []*Vector{&Vector{}}
 				}
 				}
 				CPUUsedV, ok := CPUUsedMap[newKey]
 				CPUUsedV, ok := CPUUsedMap[newKey]
 				if !ok {
 				if !ok {
-					klog.V(2).Info("no CPU usage for " + newKey)
+					klog.V(4).Info("no CPU usage for " + newKey)
 					CPUUsedV = []*Vector{&Vector{}}
 					CPUUsedV = []*Vector{&Vector{}}
 				}
 				}
 
 
@@ -342,34 +342,34 @@ func ComputeCostData(cli prometheusClient.Client, clientset kubernetes.Interface
 
 
 		} else {
 		} else {
 			// The container has been deleted. Not all information is sent to prometheus via ksm, so fill out what we can without k8s api
 			// The container has been deleted. Not all information is sent to prometheus via ksm, so fill out what we can without k8s api
-			klog.V(3).Info("The container " + key + " has been deleted. Calculating allocation but resulting object will be missing data.")
+			klog.V(4).Info("The container " + key + " has been deleted. Calculating allocation but resulting object will be missing data.")
 			c, err := newContainerMetricFromKey(key)
 			c, err := newContainerMetricFromKey(key)
 			if err != nil {
 			if err != nil {
 				return nil, err
 				return nil, err
 			}
 			}
 			RAMReqV, ok := RAMReqMap[key]
 			RAMReqV, ok := RAMReqMap[key]
 			if !ok {
 			if !ok {
-				klog.V(2).Info("no RAM requests for " + key)
+				klog.V(4).Info("no RAM requests for " + key)
 				RAMReqV = []*Vector{&Vector{}}
 				RAMReqV = []*Vector{&Vector{}}
 			}
 			}
 			RAMUsedV, ok := RAMUsedMap[key]
 			RAMUsedV, ok := RAMUsedMap[key]
 			if !ok {
 			if !ok {
-				klog.V(2).Info("no RAM usage for " + key)
+				klog.V(4).Info("no RAM usage for " + key)
 				RAMUsedV = []*Vector{&Vector{}}
 				RAMUsedV = []*Vector{&Vector{}}
 			}
 			}
 			CPUReqV, ok := CPUReqMap[key]
 			CPUReqV, ok := CPUReqMap[key]
 			if !ok {
 			if !ok {
-				klog.V(2).Info("no CPU requests for " + key)
+				klog.V(4).Info("no CPU requests for " + key)
 				CPUReqV = []*Vector{&Vector{}}
 				CPUReqV = []*Vector{&Vector{}}
 			}
 			}
 			GPUReqV, ok := GPUReqMap[key]
 			GPUReqV, ok := GPUReqMap[key]
 			if !ok {
 			if !ok {
-				klog.V(2).Info("no GPU requests for " + key)
+				klog.V(4).Info("no GPU requests for " + key)
 				GPUReqV = []*Vector{&Vector{}}
 				GPUReqV = []*Vector{&Vector{}}
 			}
 			}
 			CPUUsedV, ok := CPUUsedMap[key]
 			CPUUsedV, ok := CPUUsedMap[key]
 			if !ok {
 			if !ok {
-				klog.V(2).Info("no CPU usage for " + key)
+				klog.V(4).Info("no CPU usage for " + key)
 				CPUUsedV = []*Vector{&Vector{}}
 				CPUUsedV = []*Vector{&Vector{}}
 			}
 			}
 
 
@@ -889,7 +889,7 @@ func ComputeCostDataRange(cli prometheusClient.Client, clientset kubernetes.Inte
 
 
 		} else {
 		} else {
 			// The container has been deleted. Not all information is sent to prometheus via ksm, so fill out what we can without k8s api
 			// The container has been deleted. Not all information is sent to prometheus via ksm, so fill out what we can without k8s api
-			klog.V(3).Info("The container " + key + " has been deleted. Calculating allocation but resulting object will be missing data.")
+			klog.V(4).Info("The container " + key + " has been deleted. Calculating allocation but resulting object will be missing data.")
 			c, _ := newContainerMetricFromKey(key)
 			c, _ := newContainerMetricFromKey(key)
 			RAMReqV, ok := RAMReqMap[key]
 			RAMReqV, ok := RAMReqMap[key]
 			if !ok {
 			if !ok {
@@ -1279,7 +1279,7 @@ func getContainerMetricVector(qr interface{}, normalize bool, normalizationValue
 			Timestamp: dataPoint[0].(float64),
 			Timestamp: dataPoint[0].(float64),
 			Value:     v,
 			Value:     v,
 		}
 		}
-		klog.V(2).Info("key: " + containerMetric.Key())
+		klog.V(4).Info("key: " + containerMetric.Key())
 		containerData[containerMetric.Key()] = []*Vector{toReturn}
 		containerData[containerMetric.Key()] = []*Vector{toReturn}
 	}
 	}
 	return containerData, nil
 	return containerData, nil