Skip to content

Commit

Permalink
Add instancetype controller
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam committed Apr 17, 2024
1 parent 32c19de commit 97603c0
Show file tree
Hide file tree
Showing 12 changed files with 361 additions and 121 deletions.
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
2 changes: 1 addition & 1 deletion hack/code/prices_gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +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"
controllerspricing "github.com/aws/karpenter-provider-aws/pkg/controllers/providers/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
4 changes: 4 additions & 0 deletions pkg/cloudprovider/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ var _ = Describe("CloudProvider", func() {
},
},
})
Expect(awsEnv.InstanceTypesProvider.Update(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 +231,7 @@ var _ = Describe("CloudProvider", func() {
},
},
})
Expect(awsEnv.InstanceTypesProvider.Update(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 +326,7 @@ var _ = Describe("CloudProvider", func() {
},
},
})
Expect(awsEnv.InstanceTypesProvider.Update(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 +428,7 @@ var _ = Describe("CloudProvider", func() {
},
},
})
Expect(awsEnv.InstanceTypesProvider.Update(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"
"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
48 changes: 48 additions & 0 deletions pkg/controllers/providers/instancetype/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
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"
"time"

"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) {
return reconcile.Result{RequeueAfter: 6 * time.Hour}, c.instancetypeProvider.Update(ctx)
}

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

func (c *Controller) Builder(_ context.Context, m manager.Manager) controller.Builder {
return controller.NewSingletonManagedBy(m)
}
160 changes: 160 additions & 0 deletions pkg/controllers/providers/instancetype/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
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"
"reflect"
"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/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"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/samber/lo"

. "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, "Pricing")
}

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() {
awsEnv.EC2API.DescribeInstanceTypesOutput.Set(&ec2.DescribeInstanceTypesOutput{})
awsEnv.EC2API.DescribeInstanceTypeOfferingsOutput.Set(&ec2.DescribeInstanceTypeOfferingsOutput{})
ExpectReconcileSucceeded(ctx, controller, types.NamespacedName{})
instanceTypes, err := awsEnv.InstanceTypesProvider.GetInstanceTypes(ctx)
Expect(err).To(BeNil())
Expect(reflect.DeepEqual(instanceTypes, []*ec2.InstanceTypeInfo{})).To(BeTrue())

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.GetInstanceTypes(ctx)
Expect(err).To(BeNil())
Expect(reflect.DeepEqual(instanceTypes, ec2InstanceTypes)).To(BeTrue())
})
It("should 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())

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 on failure", 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.GetInstanceTypes(ctx)
Expect(err).To(BeNil())
Expect(reflect.DeepEqual(instanceTypes, ec2InstanceTypes)).To(BeFalse())

awsEnv.EC2API.DescribeInstanceTypesOutput.Set(&ec2.DescribeInstanceTypesOutput{
InstanceTypes: ec2InstanceTypes[:1],
})
awsEnv.EC2API.NextError.Set(awserr.New("InternalServerError", "", nil), fake.MaxCalls(1))

ExpectReconcileFailed(ctx, controller, types.NamespacedName{})
instanceTypes, err = awsEnv.InstanceTypesProvider.GetInstanceTypes(ctx)
Expect(err).To(BeNil())
Expect(reflect.DeepEqual(instanceTypes, ec2InstanceTypes[:1])).To(BeFalse())
})
})
1 change: 0 additions & 1 deletion pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont
)
instanceTypeProvider := instancetype.NewDefaultProvider(
*sess.Config.Region,
cache.New(awscache.InstanceTypesAndZonesTTL, awscache.DefaultCleanupInterval),
ec2api,
subnetProvider,
unavailableOfferingsCache,
Expand Down
1 change: 1 addition & 0 deletions pkg/providers/instance/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ var _ = Describe("InstanceProvider", func() {
},
},
})
Expect(awsEnv.InstanceTypesProvider.Update(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

0 comments on commit 97603c0

Please sign in to comment.