Skip to content

feature: pricing provider #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func main() {
op.ImagesProvider,
op.NodePoolTemplateProvider,
op.InstanceTypeProvider,
op.PricingProvider,
)...).
Start(ctx)
}
2 changes: 1 addition & 1 deletion hack/boilerplate.go.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2024 The CloudPilot AI Authors.
Copyright 2025 The CloudPilot AI Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
6 changes: 6 additions & 0 deletions hack/codegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

set -euo pipefail

echo "Updating pricing..."
go run hack/tools/price_gen/price_gen.go
119 changes: 119 additions & 0 deletions hack/tools/price_gen/price_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
Copyright 2025 The CloudPilot AI Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strconv"
)

func main() {
// Download the CSV file
resp, err := http.Get("https://gcloud-compute.com/machine-types-regions.csv")
if err != nil {
fmt.Printf("Error downloading CSV: %v\n", err)
os.Exit(1)
}
defer resp.Body.Close()

// Read the CSV data
reader := csv.NewReader(resp.Body)
reader.Comma = ','

// Read header
header, err := reader.Read()
if err != nil {
fmt.Printf("Error reading CSV header: %v\n", err)
os.Exit(1)
}

// Find the required column indices
priceColIndex := -1
regionColIndex := -1
machineTypeColIndex := -1

for i, col := range header {
switch col {
case "hour":
priceColIndex = i
case "region":
regionColIndex = i
case "name":
machineTypeColIndex = i
}
}

if priceColIndex == -1 || regionColIndex == -1 || machineTypeColIndex == -1 {
fmt.Println("Could not find required columns in CSV")
os.Exit(1)
}

// Process the data
allPrice := make(map[string]map[string]float64)

for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
fmt.Printf("Error reading CSV record: %v\n", err)
continue
}

// Get machine type, region and price
machineType := record[machineTypeColIndex]
region := record[regionColIndex]
priceStr := record[priceColIndex]

// Parse price
price, err := strconv.ParseFloat(priceStr, 64)
if err != nil {
fmt.Printf("Error parsing price for %s in region %s: %v\n", machineType, region, err)
continue
}

// Initialize region map if it doesn't exist
if _, exists := allPrice[region]; !exists {
allPrice[region] = make(map[string]float64)
}

// Store the price
allPrice[region][machineType] = price
}

// Marshal to JSON
jsonData, err := json.MarshalIndent(allPrice, "", " ")
if err != nil {
fmt.Printf("Error marshaling JSON: %v\n", err)
os.Exit(1)
}

// Write to file
err = os.WriteFile("pkg/providers/pricing/initial-on-demand-prices.json", jsonData, 0644)
if err != nil {
fmt.Printf("Error writing JSON file: %v\n", err)
os.Exit(1)
}

fmt.Println("Successfully generated pricing data")
}
6 changes: 5 additions & 1 deletion pkg/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ import (
nodeclassstatus "github.com/cloudpilot-ai/karpenter-provider-gcp/pkg/controllers/nodeclass/status"
"github.com/cloudpilot-ai/karpenter-provider-gcp/pkg/controllers/nodepooltemplate"
"github.com/cloudpilot-ai/karpenter-provider-gcp/pkg/controllers/providers/instancetype"
controllerspricing "github.com/cloudpilot-ai/karpenter-provider-gcp/pkg/controllers/providers/pricing"
"github.com/cloudpilot-ai/karpenter-provider-gcp/pkg/providers/imagefamily"
providerinstancetype "github.com/cloudpilot-ai/karpenter-provider-gcp/pkg/providers/instancetype"
providernodepooltemplate "github.com/cloudpilot-ai/karpenter-provider-gcp/pkg/providers/nodepooltemplate"
"github.com/cloudpilot-ai/karpenter-provider-gcp/pkg/providers/pricing"
)

func NewController(ctx context.Context, kubeClient client.Client, imageProvider imagefamily.Provider,
nodePoolTemplateProvider providernodepooltemplate.Provider, instanceTypeProvider providerinstancetype.Provider) []controller.Controller {
nodePoolTemplateProvider providernodepooltemplate.Provider, instanceTypeProvider providerinstancetype.Provider,
pricingProvider pricing.Provider) []controller.Controller {
controllers := []controller.Controller{
nodeclassstatus.NewController(kubeClient, imageProvider),
nodepooltemplate.NewController(nodePoolTemplateProvider),
instancetype.NewController(instanceTypeProvider),
controllerspricing.NewController(pricingProvider),
}

return controllers
Expand Down
69 changes: 69 additions & 0 deletions pkg/controllers/providers/pricing/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright 2024 The CloudPilot AI Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pricing

import (
"context"
"fmt"
"time"

"github.com/awslabs/operatorpkg/singleton"
lop "github.com/samber/lo/parallel"
"go.uber.org/multierr"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/karpenter/pkg/operator/injection"

"github.com/cloudpilot-ai/karpenter-provider-gcp/pkg/providers/pricing"
)

type Controller struct {
pricingProvider pricing.Provider
}

func NewController(pricingProvider pricing.Provider) *Controller {
return &Controller{
pricingProvider: pricingProvider,
}
}

func (c *Controller) Reconcile(ctx context.Context) (reconcile.Result, error) {
ctx = injection.WithControllerName(ctx, "providers.pricing")

work := []func(ctx context.Context) error{
c.pricingProvider.UpdateSpotPricing,
c.pricingProvider.UpdateOnDemandPricing,
}
errs := make([]error, len(work))
lop.ForEach(work, func(f func(ctx context.Context) error, i int) {
if err := f(ctx); err != nil {
errs[i] = err
}
})
if err := multierr.Combine(errs...); err != nil {
return reconcile.Result{}, fmt.Errorf("updating pricing, %w", err)
}
return reconcile.Result{RequeueAfter: 12 * time.Hour}, nil
}

func (c *Controller) Register(_ context.Context, m manager.Manager) error {
return controllerruntime.NewControllerManagedBy(m).
Named("providers.pricing").
WatchesRawSource(singleton.Source()).
Complete(singleton.AsReconciler(c))
}
6 changes: 5 additions & 1 deletion pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont
options.FromContext(ctx).ProjectID,
)
imageProvider := imagefamily.NewDefaultProvider(versionProvider, nodeTemplateProvider)
pricingProvider := pricing.NewDefaultProvider(ctx, options.FromContext(ctx).Region)
pricingProvider, err := pricing.NewDefaultProvider(ctx, options.FromContext(ctx).Region)
if err != nil {
log.FromContext(ctx).Error(err, "Failed to create pricing provider")
os.Exit(1)
}
instanceTypeProvider := instancetype.NewDefaultProvider(ctx, &auth)

return ctx, &Operator{
Expand Down
Loading