Просмотр исходного кода

Merge pull request #1136 from kubecost/biancaburtoiu/validate-spot-configs-before-query

Query spot data under new aws.SpotRefreshAllowed condition
Bianca Burtoiu 4 лет назад
Родитель
Сommit
0646e64e95
5 измененных файлов с 29 добавлено и 11 удалено
  1. 0 1
      go.mod
  2. 0 2
      go.sum
  3. 27 8
      pkg/cloud/awsprovider.go
  4. 1 0
      pkg/cloud/azureprovider.go
  5. 1 0
      pkg/cloud/provider.go

+ 0 - 1
go.mod

@@ -87,7 +87,6 @@ require (
 	github.com/jstemmer/go-junit-report v0.9.1 // indirect
 	github.com/klauspost/compress v1.13.5 // indirect
 	github.com/klauspost/cpuid v1.3.1 // indirect
-	github.com/kubecost/events v0.0.1 // indirect
 	github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
 	github.com/minio/md5-simd v1.1.0 // indirect
 	github.com/minio/sha256-simd v0.1.1 // indirect

+ 0 - 2
go.sum

@@ -388,8 +388,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
 github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
 github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
 github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
-github.com/kubecost/events v0.0.1 h1:Xo+bPQMIuVSpy0D2QbvjAcyl85tZLUcTn//kMZHR8Dg=
-github.com/kubecost/events v0.0.1/go.mod h1:WtqcuJaHct1gZe+9oCJwHEW1YPaKcPFisDVZYon+lDs=
 github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g=
 github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
 github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=

+ 27 - 8
pkg/cloud/awsprovider.go

@@ -57,22 +57,31 @@ func (aws *AWS) PricingSourceStatus() map[string]*PricingSource {
 
 	sps := &PricingSource{
 		Name: SpotPricingSource,
+		Enabled: true,
 	}
-	sps.Error = ""
-	if aws.SpotPricingError != nil {
-		sps.Error = aws.SpotPricingError.Error()
-	}
-	if sps.Error != "" {
+
+	if !aws.SpotRefreshEnabled() {
 		sps.Available = false
-	} else if len(aws.SpotPricingByInstanceID) > 0 {
-		sps.Available = true
+		sps.Error = "Spot instances not set up"
+		sps.Enabled = false
 	} else {
-		sps.Error = "No spot instances detected"
+		sps.Error = ""
+		if aws.SpotPricingError != nil {
+			sps.Error = aws.SpotPricingError.Error()
+		}
+		if sps.Error != "" {
+			sps.Available = false
+		} else if len(aws.SpotPricingByInstanceID) > 0 {
+			sps.Available = true
+		} else {
+			sps.Error = "No spot instances detected"
+		}
 	}
 	sources[SpotPricingSource] = sps
 
 	rps := &PricingSource{
 		Name: ReservedInstancePricingSource,
+		Enabled: true,
 	}
 	rps.Error = ""
 	if aws.RIPricingError != nil {
@@ -751,6 +760,12 @@ func (aws *AWS) getRegionPricing(nodeList []*v1.Node) (*http.Response, string, e
 	return resp, pricingURL, err
 }
 
+// SpotRefreshEnabled determines whether the required configs to run the spot feed query have been set up
+func (aws *AWS) SpotRefreshEnabled() bool {
+	// Need a valid value for at least one of these fields to consider spot pricing as enabled
+	return len(aws.SpotDataBucket) != 0 || len(aws.SpotDataRegion) != 0 || len(aws.ProjectID) != 0
+}
+
 // DownloadPricingData fetches data from the AWS Pricing API
 func (aws *AWS) DownloadPricingData() error {
 	aws.DownloadPricingDataLock.Lock()
@@ -1011,6 +1026,10 @@ func (aws *AWS) DownloadPricingData() error {
 	}
 	klog.V(2).Infof("Finished downloading \"%s\"", pricingURL)
 
+	if !aws.SpotRefreshEnabled() {
+		return nil
+	}
+
 	// Always run spot pricing refresh when performing download
 	aws.refreshSpotPricing(true)
 

+ 1 - 0
pkg/cloud/azureprovider.go

@@ -1269,6 +1269,7 @@ func (az *Azure) PricingSourceStatus() map[string]*PricingSource {
 	}
 	rcps := &PricingSource{
 		Name:  rateCardPricingSource,
+		Enabled: true,
 		Error: errMsg,
 	}
 	if rcps.Error != "" {

+ 1 - 0
pkg/cloud/provider.go

@@ -257,6 +257,7 @@ type PricingSources struct {
 
 type PricingSource struct {
 	Name      string `json:"name"`
+	Enabled   bool   `json:"enabled"`
 	Available bool   `json:"available"`
 	Error     string `json:"error"`
 }