Skip to content
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

chore: Add an Instancetype Controller to Asynchronously Hydrate InstanceType Data #6045

Merged
merged 1 commit into from
Apr 20, 2024
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 @@ -67,6 +67,7 @@ func main() {
op.PricingProvider,
op.AMIProvider,
op.LaunchTemplateProvider,
op.InstanceTypesProvider,
)...).
WithWebhooks(ctx, webhooks.NewWebhooks()...).
Start(ctx)
Expand Down
8 changes: 8 additions & 0 deletions pkg/cloudprovider/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ var _ = Describe("CloudProvider", func() {
},
},
})
Expect(awsEnv.InstanceTypesProvider.UpdateInstanceTypes(ctx)).To(Succeed())
Expect(awsEnv.InstanceTypesProvider.UpdateInstanceTypeOfferings(ctx)).To(Succeed())
})
It("should return an ICE error when there are no instance types to launch", func() {
// Specify no instance types and expect to receive a capacity error
Expand Down Expand Up @@ -230,6 +232,8 @@ var _ = Describe("CloudProvider", func() {
},
},
})
Expect(awsEnv.InstanceTypesProvider.UpdateInstanceTypes(ctx)).To(Succeed())
Expect(awsEnv.InstanceTypesProvider.UpdateInstanceTypeOfferings(ctx)).To(Succeed())
Expect(awsEnv.PricingProvider.UpdateSpotPricing(ctx)).To(Succeed())
instanceNames := lo.Map(instances, func(info *ec2.InstanceTypeInfo, _ int) string { return *info.InstanceType })

Expand Down Expand Up @@ -324,6 +328,8 @@ var _ = Describe("CloudProvider", func() {
},
},
})
Expect(awsEnv.InstanceTypesProvider.UpdateInstanceTypes(ctx)).To(Succeed())
Expect(awsEnv.InstanceTypesProvider.UpdateInstanceTypeOfferings(ctx)).To(Succeed())
Expect(awsEnv.PricingProvider.UpdateSpotPricing(ctx)).To(Succeed())
instanceNames := lo.Map(instances, func(info *ec2.InstanceTypeInfo, _ int) string { return *info.InstanceType })

Expand Down Expand Up @@ -425,6 +431,8 @@ var _ = Describe("CloudProvider", func() {
},
},
})
Expect(awsEnv.InstanceTypesProvider.UpdateInstanceTypes(ctx)).To(Succeed())
Expect(awsEnv.InstanceTypesProvider.UpdateInstanceTypeOfferings(ctx)).To(Succeed())
Expect(awsEnv.PricingProvider.UpdateSpotPricing(ctx)).To(Succeed())
instanceNames := lo.Map(uniqInstanceTypes, func(info *ec2.InstanceTypeInfo, _ int) string { return *info.InstanceType })

Expand Down
5 changes: 4 additions & 1 deletion pkg/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
nodeclasshash "github.com/aws/karpenter-provider-aws/pkg/controllers/nodeclass/hash"
nodeclassstatus "github.com/aws/karpenter-provider-aws/pkg/controllers/nodeclass/status"
nodeclasstermination "github.com/aws/karpenter-provider-aws/pkg/controllers/nodeclass/termination"
controllersinstancetype "github.com/aws/karpenter-provider-aws/pkg/controllers/providers/instancetype"
controllerspricing "github.com/aws/karpenter-provider-aws/pkg/controllers/providers/pricing"
"github.com/aws/karpenter-provider-aws/pkg/providers/launchtemplate"

Expand All @@ -42,6 +43,7 @@ import (
"github.com/aws/karpenter-provider-aws/pkg/providers/amifamily"
"github.com/aws/karpenter-provider-aws/pkg/providers/instance"
"github.com/aws/karpenter-provider-aws/pkg/providers/instanceprofile"
"github.com/aws/karpenter-provider-aws/pkg/providers/instancetype"
engedaam marked this conversation as resolved.
Show resolved Hide resolved
"github.com/aws/karpenter-provider-aws/pkg/providers/pricing"
"github.com/aws/karpenter-provider-aws/pkg/providers/securitygroup"
"github.com/aws/karpenter-provider-aws/pkg/providers/sqs"
Expand All @@ -51,7 +53,7 @@ import (
func NewControllers(ctx context.Context, sess *session.Session, clk clock.Clock, kubeClient client.Client, recorder events.Recorder,
unavailableOfferings *cache.UnavailableOfferings, cloudProvider cloudprovider.CloudProvider, subnetProvider subnet.Provider,
securityGroupProvider securitygroup.Provider, instanceProfileProvider instanceprofile.Provider, instanceProvider instance.Provider,
pricingProvider pricing.Provider, amiProvider amifamily.Provider, launchTemplateProvider launchtemplate.Provider) []controller.Controller {
pricingProvider pricing.Provider, amiProvider amifamily.Provider, launchTemplateProvider launchtemplate.Provider, instanceTypeProvider instancetype.Provider) []controller.Controller {

controllers := []controller.Controller{
nodeclasshash.NewController(kubeClient),
Expand All @@ -60,6 +62,7 @@ func NewControllers(ctx context.Context, sess *session.Session, clk clock.Clock,
nodeclaimgarbagecollection.NewController(kubeClient, cloudProvider),
nodeclaimtagging.NewController(kubeClient, instanceProvider),
controllerspricing.NewController(pricingProvider),
controllersinstancetype.NewController(instanceTypeProvider),
}
if options.FromContext(ctx).InterruptionQueue != "" {
sqsapi := servicesqs.New(sess)
Expand Down
65 changes: 65 additions & 0 deletions pkg/controllers/providers/instancetype/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
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 instancetype

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"
"sigs.k8s.io/karpenter/pkg/operator/controller"

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

type Controller struct {
instancetypeProvider instancetype.Provider
}

func NewController(instancetypeProvider instancetype.Provider) *Controller {
return &Controller{
instancetypeProvider: instancetypeProvider,
}
}

func (c *Controller) Reconcile(ctx context.Context, _ reconcile.Request) (reconcile.Result, error) {
work := []func(ctx context.Context) error{
c.instancetypeProvider.UpdateInstanceTypes,
c.instancetypeProvider.UpdateInstanceTypeOfferings,
}
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 instancetype, %w", err)
}
return reconcile.Result{RequeueAfter: 12 * time.Hour}, nil
}

func (c *Controller) Name() string {
return "providers.instancetype"
}

func (c *Controller) Builder(_ context.Context, m manager.Manager) controller.Builder {
// Includes a default exponential failure rate limiter of base: time.Millisecond, and max: 1000*time.Second
return controller.NewSingletonManagedBy(m)
}
137 changes: 137 additions & 0 deletions pkg/controllers/providers/instancetype/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
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 instancetype_test

import (
"context"
"testing"

"k8s.io/apimachinery/pkg/types"
corev1beta1 "sigs.k8s.io/karpenter/pkg/apis/v1beta1"
coreoptions "sigs.k8s.io/karpenter/pkg/operator/options"
"sigs.k8s.io/karpenter/pkg/operator/scheme"
coretest "sigs.k8s.io/karpenter/pkg/test"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/samber/lo"

"github.com/aws/karpenter-provider-aws/pkg/apis"
"github.com/aws/karpenter-provider-aws/pkg/apis/v1beta1"
controllersinstancetype "github.com/aws/karpenter-provider-aws/pkg/controllers/providers/instancetype"
"github.com/aws/karpenter-provider-aws/pkg/fake"
"github.com/aws/karpenter-provider-aws/pkg/operator/options"
"github.com/aws/karpenter-provider-aws/pkg/test"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "knative.dev/pkg/logging/testing"
. "sigs.k8s.io/karpenter/pkg/test/expectations"
)

var ctx context.Context
var stop context.CancelFunc
var env *coretest.Environment
var awsEnv *test.Environment
var controller *controllersinstancetype.Controller

func TestAWS(t *testing.T) {
ctx = TestContextWithLogger(t)
RegisterFailHandler(Fail)
RunSpecs(t, "InstanceType")
}

var _ = BeforeSuite(func() {
env = coretest.NewEnvironment(scheme.Scheme, coretest.WithCRDs(apis.CRDs...))
ctx = coreoptions.ToContext(ctx, coretest.Options())
ctx = options.ToContext(ctx, test.Options())
ctx, stop = context.WithCancel(ctx)
awsEnv = test.NewEnvironment(ctx, env)
controller = controllersinstancetype.NewController(awsEnv.InstanceTypesProvider)
})

var _ = AfterSuite(func() {
stop()
Expect(env.Stop()).To(Succeed(), "Failed to stop environment")
})

var _ = BeforeEach(func() {
ctx = coreoptions.ToContext(ctx, coretest.Options())
ctx = options.ToContext(ctx, test.Options())

awsEnv.Reset()
})

var _ = AfterEach(func() {
ExpectCleanedUp(ctx, env.Client)
})

var _ = Describe("InstanceType", func() {
It("should update instance type date with response from the DescribeInstanceTypes API", func() {
ec2InstanceTypes := fake.MakeInstances()
ec2Offerings := fake.MakeInstanceOfferings(ec2InstanceTypes)
awsEnv.EC2API.DescribeInstanceTypesOutput.Set(&ec2.DescribeInstanceTypesOutput{
InstanceTypes: ec2InstanceTypes,
})
awsEnv.EC2API.DescribeInstanceTypeOfferingsOutput.Set(&ec2.DescribeInstanceTypeOfferingsOutput{
InstanceTypeOfferings: ec2Offerings,
})

ExpectReconcileSucceeded(ctx, controller, types.NamespacedName{})
instanceTypes, err := awsEnv.InstanceTypesProvider.List(ctx, &corev1beta1.KubeletConfiguration{}, &v1beta1.EC2NodeClass{})
Expect(err).To(BeNil())
for i := range instanceTypes {
Expect(instanceTypes[i].Name).To(Equal(lo.FromPtr(ec2InstanceTypes[i].InstanceType)))
}
})
It("should update instance type offering date with response from the DescribeInstanceTypesOfferings API", func() {
ec2InstanceTypes := fake.MakeInstances()
ec2Offerings := fake.MakeInstanceOfferings(ec2InstanceTypes)
awsEnv.EC2API.DescribeInstanceTypesOutput.Set(&ec2.DescribeInstanceTypesOutput{
InstanceTypes: ec2InstanceTypes,
})
awsEnv.EC2API.DescribeInstanceTypeOfferingsOutput.Set(&ec2.DescribeInstanceTypeOfferingsOutput{
InstanceTypeOfferings: ec2Offerings,
})

ExpectReconcileSucceeded(ctx, controller, types.NamespacedName{})
instanceTypes, err := awsEnv.InstanceTypesProvider.List(ctx, &corev1beta1.KubeletConfiguration{}, &v1beta1.EC2NodeClass{})
Expect(err).To(BeNil())

Expect(len(instanceTypes)).To(BeNumerically("==", len(ec2InstanceTypes)))
for x := range instanceTypes {
offering, found := lo.Find(ec2Offerings, func(off *ec2.InstanceTypeOffering) bool {
return instanceTypes[x].Name == lo.FromPtr(off.InstanceType)
})
Expect(found).To(BeTrue())
for y := range instanceTypes[x].Offerings {
Expect(instanceTypes[x].Offerings[y].Zone).To(Equal(lo.FromPtr(offering.Location)))
}
}
})
It("should not update instance type date with response from the DescribeInstanceTypes API is empty", func() {
awsEnv.EC2API.DescribeInstanceTypesOutput.Set(&ec2.DescribeInstanceTypesOutput{})
engedaam marked this conversation as resolved.
Show resolved Hide resolved
awsEnv.EC2API.DescribeInstanceTypeOfferingsOutput.Set(&ec2.DescribeInstanceTypeOfferingsOutput{})
ExpectReconcileSucceeded(ctx, controller, types.NamespacedName{})
_, err := awsEnv.InstanceTypesProvider.List(ctx, &corev1beta1.KubeletConfiguration{}, &v1beta1.EC2NodeClass{})
Expect(err).ToNot(BeNil())
})
It("should not update instance type offering date with response from the DescribeInstanceTypesOfferings API", func() {
awsEnv.EC2API.DescribeInstanceTypesOutput.Set(&ec2.DescribeInstanceTypesOutput{})
awsEnv.EC2API.DescribeInstanceTypeOfferingsOutput.Set(&ec2.DescribeInstanceTypeOfferingsOutput{})
ExpectReconcileSucceeded(ctx, controller, types.NamespacedName{})
_, err := awsEnv.InstanceTypesProvider.List(ctx, &corev1beta1.KubeletConfiguration{}, &v1beta1.EC2NodeClass{})
Expect(err).ToNot(BeNil())
})
})
2 changes: 1 addition & 1 deletion pkg/controllers/providers/pricing/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c *Controller) Reconcile(ctx context.Context, _ reconcile.Request) (reconc
}

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

func (c *Controller) Builder(_ context.Context, m manager.Manager) controller.Builder {
Expand Down
2 changes: 2 additions & 0 deletions pkg/providers/instance/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ var _ = Describe("InstanceProvider", func() {
},
},
})
Expect(awsEnv.InstanceTypesProvider.UpdateInstanceTypes(ctx)).To(Succeed())
Expect(awsEnv.InstanceTypesProvider.UpdateInstanceTypeOfferings(ctx)).To(Succeed())
})
It("should return an ICE error when all attempted instance types return an ICE error", func() {
ExpectApplied(ctx, env.Client, nodeClaim, nodePool, nodeClass)
Expand Down
Loading
Loading