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

Merge branch 'develop' into dependabot/go_modules/modules/collector-source/k8s.io/api-0.36.3

Warwick 3 недель назад
Родитель
Сommit
ec6b69b93c

+ 1 - 1
.github/workflows/compare-pricing-data.yaml

@@ -2,7 +2,7 @@ name: Compare Pricing Data
 
 on:
   schedule:
-    - cron: '0 10 * * *'
+    - cron: '0 18 * * *'
 
 permissions:
   contents: read

+ 27 - 1
CONTRIBUTING.md

@@ -28,12 +28,14 @@ Follow these steps to build the OpenCost cost-model and UI from source and
 deploy. The provided build tooling is natively multi-architecture (built images
 will run on both AMD64 and ARM64 clusters).
 
+### Using Docker
+
 Dependencies:
 1. Docker (with `buildx`)
 2. [just](https://github.com/casey/just) (if you don't want to install it , Just read the `justfile` and run the commands manually)
 3. Multi-arch `buildx` builders set up via https://github.com/tonistiigi/binfmt
 4. `manifest-tool` via https://github.com/estesp/manifest-tool
-4. `npm` (if you want to build the UI)
+5. `npm` (if you want to build the UI)
 
 ### Build the backend
 
@@ -41,6 +43,30 @@ Dependencies:
 2. Edit the [pulled image](https://github.com/opencost/opencost/blob/develop/kubernetes/opencost.yaml#L145) in the `kubernetes/opencost.yaml` to `<repo>/opencost:<tag>`
 3. Set [this environment variable](https://github.com/opencost/opencost/blob/develop/kubernetes/opencost.yaml#L155) to the address of your Prometheus server
 
+### Using Podman
+
+If you prefer Podman over Docker, use the `build-podman` recipe instead. Podman's
+multi-arch manifest support is built in — no `manifest-tool` required.
+
+Dependencies:
+1. [Podman](https://podman.io/docs/installation) (v4+)
+2. [just](https://github.com/casey/just)
+
+Build and push a multi-arch image:
+
+```bash
+just build-podman "<repo>/opencost:<tag>" "<release-version>"
+# e.g.
+just build-podman myregistry.io/opencost:latest 1.0.0
+```
+
+This will:
+1. Compile `costmodel-amd64` and `costmodel-arm64` binaries with version metadata embedded
+2. Build and push `<IMAGE_TAG>-amd64` and `<IMAGE_TAG>-arm64` images using `Dockerfile.cross`
+3. Create a multi-arch manifest at `<IMAGE_TAG>` combining both and push it to the registry
+
+> **Note:** make sure you are logged in to your registry (`podman login <registry>`) before running the build.
+
 ### Build the frontend
 1. `cd ui && just build "<repo>/opencost-ui:<tag>"`
 2. Edit the [pulled image](https://github.com/opencost/opencost/blob/develop/kubernetes/opencost.yaml#L162) in the `kubernetes/opencost.yaml` to `<repo>/opencost-ui:<tag>`

+ 4 - 0
core/pkg/opencost/allocation.go

@@ -1376,6 +1376,10 @@ func (a *Allocation) add(that *Allocation) {
 		a.End = that.End
 	}
 
+	if a.GPUAllocation == nil && that.GPUAllocation != nil {
+		a.GPUAllocation = that.GPUAllocation.Clone()
+	}
+
 	// Convert cumulative request and usage back into rates
 	// TODO:TEST write a unit test that fails if this is done incorrectly
 	if a.Minutes() > 0 {

+ 38 - 0
core/pkg/opencost/allocation_test.go

@@ -234,6 +234,44 @@ func TestAllocation_Add(t *testing.T) {
 	if act.RawAllocationOnly != nil {
 		t.Errorf("Allocation.Add: Raw only data must be nil after an add")
 	}
+
+	// Test GPUAllocation merging edge cases:
+	// Case A: Receiver has nil GPUAllocation, incoming has non-nil GPUAllocation
+	g1 := &Allocation{
+		Start:      s1,
+		End:        e1,
+		Window:     NewWindow(&s1, &e1),
+		Properties: &AllocationProperties{},
+	}
+	gpuReqVal := 1.0
+	gpuUseVal := 0.5
+	g2 := &Allocation{
+		Start:      s2,
+		End:        e2,
+		Window:     NewWindow(&s2, &e2),
+		Properties: &AllocationProperties{},
+		GPUAllocation: &GPUAllocation{
+			GPUDevice:         "nvidia-tesla-t4",
+			GPURequestAverage: &gpuReqVal,
+			GPUUsageAverage:   &gpuUseVal,
+		},
+	}
+	actG, err := g1.Add(g2)
+	if err != nil {
+		t.Fatalf("Allocation.Add: unexpected error: %s", err)
+	}
+	if actG.GPUAllocation == nil {
+		t.Fatalf("Allocation.Add: expected non-nil GPUAllocation from merge")
+	}
+	if actG.GPUAllocation.GPUDevice != "nvidia-tesla-t4" {
+		t.Errorf("Allocation.Add: expected GPUDevice 'nvidia-tesla-t4', got %s", actG.GPUAllocation.GPUDevice)
+	}
+	if actG.GPUAllocation.GPURequestAverage == nil || !util.IsApproximately(0.75, *actG.GPUAllocation.GPURequestAverage) {
+		t.Errorf("Allocation.Add: expected GPURequestAverage 0.75, got %v", actG.GPUAllocation.GPURequestAverage)
+	}
+	if actG.GPUAllocation.GPUUsageAverage == nil || !util.IsApproximately(0.375, *actG.GPUAllocation.GPUUsageAverage) {
+		t.Errorf("Allocation.Add: expected GPUUsageAverage 0.375, got %v", actG.GPUAllocation.GPUUsageAverage)
+	}
 }
 
 func TestAllocation_Share(t *testing.T) {

+ 31 - 1
justfile

@@ -85,7 +85,7 @@ build-binary VERSION=version:
            -X github.com/opencost/opencost/core/pkg/version.GitCommit={{commit}}" \
         -o ./costmodel-arm64
 
-# Build and push a multi-arch Docker image
+# Build and push a multi-arch image using Docker
 build IMAGE_TAG RELEASE_VERSION: (build-binary RELEASE_VERSION)
     docker buildx build \
         --rm \
@@ -116,6 +116,36 @@ build IMAGE_TAG RELEASE_VERSION: (build-binary RELEASE_VERSION)
         --template {{IMAGE_TAG}}-ARCH \
         --target {{IMAGE_TAG}}
 
+# Build and push a multi-arch image using Podman
+build-podman IMAGE_TAG RELEASE_VERSION: (build-binary RELEASE_VERSION)
+    podman build \
+        --rm \
+        --platform "linux/amd64" \
+        -f 'Dockerfile.cross' \
+        --build-arg binarypath=./cmd/costmodel/costmodel-amd64 \
+        --build-arg version={{RELEASE_VERSION}} \
+        --build-arg commit={{commit}} \
+        -t {{IMAGE_TAG}}-amd64 \
+        .
+    podman push {{IMAGE_TAG}}-amd64
+
+    podman build \
+        --rm \
+        --platform "linux/arm64" \
+        -f 'Dockerfile.cross' \
+        --build-arg binarypath=./cmd/costmodel/costmodel-arm64 \
+        --build-arg version={{RELEASE_VERSION}} \
+        --build-arg commit={{commit}} \
+        -t {{IMAGE_TAG}}-arm64 \
+        .
+    podman push {{IMAGE_TAG}}-arm64
+
+    podman manifest create {{IMAGE_TAG}} \
+        {{IMAGE_TAG}}-amd64 \
+        {{IMAGE_TAG}}-arm64
+    podman manifest push --all {{IMAGE_TAG}}
+    podman manifest rm {{IMAGE_TAG}}
+
 validate-protobuf:
     ./generate.sh
     git diff --exit-code

+ 4 - 1
modules/pricing/public/aws/pricelistapi.go

@@ -9,8 +9,11 @@ import (
 	"github.com/opencost/opencost/core/pkg/log"
 	"github.com/opencost/opencost/core/pkg/util/json"
 	"github.com/opencost/opencost/modules/pricing/public/env"
+	"github.com/opencost/opencost/modules/pricing/public/httpclient"
 )
 
+var awsHTTPClient = httpclient.NewClient(0) // no timeout
+
 const (
 	awsPricingBaseURL      = "https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/"
 	awsChinaPricingBaseURL = "https://pricing.cn-north-1.amazonaws.com.cn/offers/v1.0/cn/"
@@ -62,7 +65,7 @@ func QueryEC2PriceList(
 	pricingURL := getListPriceURL("AmazonEC2", region)
 
 	log.Infof("starting download of \"%s\", which is quite large ...", pricingURL)
-	resp, err := http.Get(pricingURL)
+	resp, err := awsHTTPClient.Get(pricingURL)
 	if err != nil {
 		return fmt.Errorf("bogus fetch of \"%s\": %w", pricingURL, err)
 	}

+ 3 - 6
modules/pricing/public/azure/azurepricingsource.go

@@ -13,6 +13,7 @@ import (
 	"github.com/opencost/opencost/core/pkg/log"
 	"github.com/opencost/opencost/core/pkg/pricing"
 	"github.com/opencost/opencost/core/pkg/unit"
+	"github.com/opencost/opencost/modules/pricing/public/httpclient"
 )
 
 const (
@@ -26,7 +27,7 @@ type AzurePricingSourceConfig struct {
 	CurrencyCode string
 }
 
-var azureHTTPClient = &http.Client{Timeout: 60 * time.Second}
+var azureHTTPClient = httpclient.NewClient(120 * time.Second)
 
 // AzurePricingSource implements the PricingSource interface using the
 // Azure Retail Prices API (no auth required).
@@ -232,11 +233,7 @@ func (a *AzurePricingSource) includeItem(item AzurePricingAttributes) bool {
 	}
 
 	skuLower := strings.ToLower(item.SkuName)
-	if strings.Contains(skuLower, "low priority") {
-		return false
-	}
-
-	return true
+	return !strings.Contains(skuLower, "low priority")
 }
 
 // includeDiskItem filters disk items to include only managed disks.

+ 2 - 1
modules/pricing/public/gcp/gcppricingsource.go

@@ -15,11 +15,12 @@ import (
 	"github.com/opencost/opencost/core/pkg/log"
 	"github.com/opencost/opencost/core/pkg/pricing"
 	"github.com/opencost/opencost/core/pkg/unit"
+	"github.com/opencost/opencost/modules/pricing/public/httpclient"
 )
 
 var BillingAPIBaseURL = "https://cloudbilling.googleapis.com/v1/services/6F81-5844-456A/skus"
 
-var gcpHTTPClient = &http.Client{Timeout: 120 * time.Second}
+var gcpHTTPClient = httpclient.NewClient(120 * time.Second)
 
 type GCPPricingSourceConfig struct {
 	APIKey       string

+ 79 - 0
modules/pricing/public/httpclient/httpclient.go

@@ -0,0 +1,79 @@
+package httpclient
+
+import (
+	"io"
+	"net/http"
+	"strconv"
+	"time"
+
+	"github.com/opencost/opencost/core/pkg/log"
+)
+
+const (
+	defaultMaxRetries    = 5
+	defaultRetryBaseWait = 2 * time.Second
+	defaultRetryMaxWait  = 60 * time.Second
+)
+
+// retryTransport is an http.RoundTripper that retries requests on 429 and 503s
+type retryTransport struct {
+	wrapped     http.RoundTripper
+	maxRetries  int
+	baseWait    time.Duration
+	maxWait     time.Duration
+}
+
+func (t *retryTransport) RoundTrip(req *http.Request) (*http.Response, error) {
+	wait := t.baseWait
+	for attempt := 0; attempt <= t.maxRetries; attempt++ {
+		resp, err := t.wrapped.RoundTrip(req)
+		if err != nil {
+			return nil, err
+		}
+
+		if resp.StatusCode != http.StatusTooManyRequests && resp.StatusCode != http.StatusServiceUnavailable {
+			return resp, nil
+		}
+
+		if attempt == t.maxRetries {
+			// Return the final error response untouched so the caller can
+			// read the body and status code.
+			return resp, nil
+		}
+
+		// Consume and discard the error body so the connection can be reused,
+		// then close it before sleeping.
+		_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 4096))
+		_ = resp.Body.Close()
+
+		delay := wait
+		if ra := resp.Header.Get("Retry-After"); ra != "" {
+			if secs, err := strconv.Atoi(ra); err == nil {
+				delay = time.Duration(secs) * time.Second
+			}
+		}
+		if delay > t.maxWait {
+			delay = t.maxWait
+		}
+
+		log.Warnf("pricing httpclient: HTTP %d, retrying in %s (attempt %d/%d)",
+			resp.StatusCode, delay, attempt+1, t.maxRetries)
+		time.Sleep(delay)
+		wait *= 2
+	}
+	return nil, nil
+}
+
+// NewClient returns an *http.Client whose transport automatically retries
+// on HTTP 429 / 503 with exponential backoff
+func NewClient(timeout time.Duration) *http.Client {
+	return &http.Client{
+		Timeout: timeout,
+		Transport: &retryTransport{
+			wrapped:    http.DefaultTransport,
+			maxRetries: defaultMaxRetries,
+			baseWait:   defaultRetryBaseWait,
+			maxWait:    defaultRetryMaxWait,
+		},
+	}
+}