Skip to content

Commit

Permalink
chore: Shift pricing controller into controllers package (#5767)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Mar 4, 2024
1 parent cc5f383 commit 784dec0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
3 changes: 2 additions & 1 deletion hack/code/prices_gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

controllerspricing "github.com/aws/karpenter-provider-aws/pkg/controllers/pricing"
"github.com/aws/karpenter-provider-aws/pkg/operator/options"
"github.com/aws/karpenter-provider-aws/pkg/providers/pricing"
"github.com/aws/karpenter-provider-aws/pkg/test"
Expand Down Expand Up @@ -108,7 +109,7 @@ func main() {
for _, region := range getAWSRegions(opts.partition) {
log.Println("fetching for", region)
pricingProvider := pricing.NewProvider(ctx, pricing.NewAPI(sess, region), ec2, region)
controller := pricing.NewController(pricingProvider)
controller := controllerspricing.NewController(pricingProvider)
_, err := controller.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{}})
if err != nil {
log.Fatalf("failed to initialize pricing provider %s", err)
Expand Down
3 changes: 2 additions & 1 deletion pkg/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"sigs.k8s.io/karpenter/pkg/cloudprovider"

controllerspricing "github.com/aws/karpenter-provider-aws/pkg/controllers/pricing"
"github.com/aws/karpenter-provider-aws/pkg/providers/launchtemplate"

"github.com/aws/aws-sdk-go/aws/session"
Expand Down Expand Up @@ -54,7 +55,7 @@ func NewControllers(ctx context.Context, sess *session.Session, clk clock.Clock,
nodeclass.NewController(kubeClient, recorder, subnetProvider, securityGroupProvider, amiProvider, instanceProfileProvider, launchTemplateProvider),
nodeclaimgarbagecollection.NewController(kubeClient, cloudProvider),
nodeclaimtagging.NewController(kubeClient, instanceProvider),
pricing.NewController(pricingProvider),
controllerspricing.NewController(pricingProvider),
}
if options.FromContext(ctx).InterruptionQueue != "" {
controllers = append(controllers, interruption.NewController(kubeClient, clk, recorder, lo.Must(sqs.NewProvider(ctx, servicesqs.New(sess), options.FromContext(ctx).InterruptionQueue)), unavailableOfferings))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,50 @@ package pricing

import (
"context"
"fmt"
"time"

lop "github.com/samber/lo/parallel"
"go.uber.org/multierr"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

corecontroller "sigs.k8s.io/karpenter/pkg/operator/controller"
"sigs.k8s.io/karpenter/pkg/operator/controller"

"github.com/aws/karpenter-provider-aws/pkg/providers/pricing"
)

type Controller struct {
pricingProvider *Provider
pricingProvider *pricing.Provider
}

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

func (c *Controller) Reconcile(ctx context.Context, _ reconcile.Request) (reconcile.Result, error) {
return reconcile.Result{RequeueAfter: 12 * time.Hour}, c.updatePricing(ctx)
}

func (c *Controller) Name() string {
return "pricing"
}

func (c *Controller) Builder(_ context.Context, m manager.Manager) corecontroller.Builder {
return corecontroller.NewSingletonManagedBy(m)
}

func (c *Controller) updatePricing(ctx context.Context) error {
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) Name() string {
return "pricing"
}

return multierr.Combine(errs...)
func (c *Controller) Builder(_ context.Context, m manager.Manager) controller.Builder {
return controller.NewSingletonManagedBy(m)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
coretest "sigs.k8s.io/karpenter/pkg/test"

"github.com/aws/karpenter-provider-aws/pkg/apis"
controllerspricing "github.com/aws/karpenter-provider-aws/pkg/controllers/pricing"
"github.com/aws/karpenter-provider-aws/pkg/fake"
"github.com/aws/karpenter-provider-aws/pkg/operator/options"
"github.com/aws/karpenter-provider-aws/pkg/providers/pricing"
Expand All @@ -45,7 +46,7 @@ var ctx context.Context
var stop context.CancelFunc
var env *coretest.Environment
var awsEnv *test.Environment
var controller *pricing.Controller
var controller *controllerspricing.Controller

func TestAWS(t *testing.T) {
ctx = TestContextWithLogger(t)
Expand All @@ -59,7 +60,7 @@ var _ = BeforeSuite(func() {
ctx = options.ToContext(ctx, test.Options())
ctx, stop = context.WithCancel(ctx)
awsEnv = test.NewEnvironment(ctx, env)
controller = pricing.NewController(awsEnv.PricingProvider)
controller = controllerspricing.NewController(awsEnv.PricingProvider)
})

var _ = AfterSuite(func() {
Expand Down
4 changes: 3 additions & 1 deletion pkg/providers/pricing/pricing.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ func (p *Provider) UpdateOnDemandPricing(ctx context.Context) error {
// if we are in isolated vpc, skip updating on demand pricing
// as pricing api may not be available
if options.FromContext(ctx).IsolatedVPC {
logging.FromContext(ctx).Infof("assuming isolated VPC, on-demand pricing information will not be updated")
if p.cm.HasChanged("on-demand-prices", nil) {
logging.FromContext(ctx).Debug("running in an isolated VPC, on-demand pricing information will not be updated")
}
return nil
}

Expand Down

0 comments on commit 784dec0

Please sign in to comment.