Преглед на файлове

add custom spot label and fixes

AjayTripathy преди 7 години
родител
ревизия
605f200c2e
променени са 4 файла, в които са добавени 34 реда и са изтрити 2 реда
  1. 1 0
      Dockerfile
  2. 10 0
      cloud/aws.json
  3. 14 2
      cloud/awsprovider.go
  4. 9 0
      main.go

+ 1 - 0
Dockerfile

@@ -17,4 +17,5 @@ RUN apk add --update --no-cache ca-certificates git
 COPY --from=build-env /go/bin/app /go/bin/app
 ADD ./cloud/default.json /models/default.json
 ADD ./cloud/azure.json /models/azure.json
+ADD ./cloud/aws.json /models/aws.json
 ENTRYPOINT ["/go/bin/app"]

+ 10 - 0
cloud/aws.json

@@ -0,0 +1,10 @@
+{
+    "provider": "custom",
+    "description": "Default prices used to compute allocation between RAM and CPU. AWS pricing API data still used for total node cost.",
+    "CPU": "0.031611",
+    "spotCPU": "0.006655",
+    "RAM": "0.004237",
+    "spotRAM": "0.000892",
+    "spotLabel": "kops.k8s.io/instancegroup",
+    "spotLabelValue": "spotinstance-nodes"
+}

+ 14 - 2
cloud/awsprovider.go

@@ -31,6 +31,8 @@ type AWS struct {
 	BaseCPUPrice     string
 	BaseSpotCPUPrice string
 	BaseSpotRAMPrice string
+	SpotLabelName    string
+	SpotLabelValue   string
 }
 
 // AWSPricing maps a k8s node to an AWS Pricing "product"
@@ -143,6 +145,10 @@ func (aws *AWS) GetKey(labels map[string]string) string {
 		usageType := "preemptible"
 		return region + "," + instanceType + "," + operatingSystem + "," + usageType
 	}
+	if l, ok := labels[aws.SpotLabelName]; ok && l == aws.SpotLabelValue {
+		usageType := "preemptible"
+		return region + "," + instanceType + "," + operatingSystem + "," + usageType
+	}
 	return region + "," + instanceType + "," + operatingSystem
 }
 
@@ -203,12 +209,14 @@ func (aws *AWS) DownloadPricingData() error {
 					key := aws.KubeAttrConversion(product.Attributes.Location, product.Attributes.InstanceType, product.Attributes.OperatingSystem)
 					spotKey := key + ",preemptible"
 					if inputkeys[key] || inputkeys[spotKey] { // Just grab the sku even if spot, and change the price later.
-						aws.Pricing[key] = &AWSProductTerms{
+						productTerms := &AWSProductTerms{
 							Sku:     product.Sku,
 							Memory:  product.Attributes.Memory,
 							Storage: product.Attributes.Storage,
 							VCpu:    product.Attributes.VCpu,
 						}
+						aws.Pricing[key] = productTerms
+						aws.Pricing[spotKey] = productTerms
 						skusToKeys[product.Sku] = key
 					}
 					aws.ValidPricingKeys[key] = true
@@ -232,8 +240,10 @@ func (aws *AWS) DownloadPricingData() error {
 					}
 					if sku.(string)+OnDemandRateCode == skuOnDemand {
 						key, ok := skusToKeys[sku.(string)]
+						spotKey := key + ",preemptible"
 						if ok {
 							aws.Pricing[key].OnDemand = offerTerm
+							aws.Pricing[spotKey].OnDemand = offerTerm
 						}
 					}
 					dec.Token()
@@ -246,13 +256,15 @@ func (aws *AWS) DownloadPricingData() error {
 	if err != nil {
 		return err
 	}
-	c, err := GetDefaultPricingData("default.json")
+	c, err := GetDefaultPricingData("aws.json")
 	if err != nil {
 		log.Printf("Error downloading default pricing data: %s", err.Error())
 	}
 	aws.BaseCPUPrice = c.CPU
 	aws.BaseSpotCPUPrice = c.SpotCPU
 	aws.BaseSpotRAMPrice = c.SpotRAM
+	aws.SpotLabelName = c.SpotLabel
+	aws.SpotLabelValue = c.SpotLabelValue
 	return nil
 }
 

+ 9 - 0
main.go

@@ -91,6 +91,14 @@ func (a *Accesses) CostDataModelRange(w http.ResponseWriter, r *http.Request, ps
 	w.Write(wrapData(data, err))
 }
 
+func (a *Accesses) ClusterName(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
+	w.Header().Set("Content-Type", "application/json")
+	w.Header().Set("Access-Control-Allow-Origin", "*")
+
+	data, err := a.Cloud.ClusterName()
+	w.Write(wrapData(data, err))
+}
+
 func Healthz(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
 	w.WriteHeader(200)
 	w.Header().Set("Content-Length", "0")
@@ -204,6 +212,7 @@ func main() {
 	router.GET("/costDataModel", a.CostDataModel)
 	router.GET("/costDataModelRange", a.CostDataModelRange)
 	router.GET("/healthz", Healthz)
+	router.GET("/clusterName", a.ClusterName)
 	router.POST("/refreshPricing", a.RefreshPricingData)
 
 	rootMux := http.NewServeMux()