Skip to content

Commit

Permalink
Add instance type and instance type offering generator
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis committed Sep 29, 2024
1 parent 8676ebf commit 027ed24
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 267 deletions.
3 changes: 0 additions & 3 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ limitations under the License.
package main

import (
"github.com/samber/lo"

"github.com/aws/karpenter-provider-aws/pkg/cloudprovider"
"github.com/aws/karpenter-provider-aws/pkg/controllers"
"github.com/aws/karpenter-provider-aws/pkg/operator"
Expand All @@ -37,7 +35,6 @@ func main() {
op.AMIProvider,
op.SecurityGroupProvider,
)
lo.Must0(op.AddHealthzCheck("cloud-provider", awsCloudProvider.LivenessProbe))
cloudProvider := metrics.Decorate(awsCloudProvider)

op.
Expand Down
18 changes: 10 additions & 8 deletions hack/docs/instancetypes_gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,18 @@ below are the resources available with some assumptions and after the instance o
ec2api := ec2.New(sess)
subnetProvider := subnet.NewDefaultProvider(ec2api, cache.New(awscache.DefaultTTL, awscache.DefaultCleanupInterval), cache.New(awscache.AvailableIPAddressTTL, awscache.DefaultCleanupInterval), cache.New(awscache.AssociatePublicIPAddressTTL, awscache.DefaultCleanupInterval))
instanceTypeProvider := instancetype.NewDefaultProvider(
region,
cache.New(awscache.InstanceTypesAndZonesTTL, awscache.DefaultCleanupInterval),
ec2api,
subnetProvider,
awscache.NewUnavailableOfferings(),
pricing.NewDefaultProvider(
ctx,
pricing.NewAPI(sess, *sess.Config.Region),
ec2api,
*sess.Config.Region,
instancetype.NewDefaultResolver(
region,
pricing.NewDefaultProvider(
ctx,
pricing.NewAPI(sess, *sess.Config.Region),
ec2api,
*sess.Config.Region,
),
awscache.NewUnavailableOfferings(),
),
)
if err = instanceTypeProvider.UpdateInstanceTypes(ctx); err != nil {
Expand Down Expand Up @@ -172,7 +174,7 @@ below are the resources available with some assumptions and after the instance o
Zone: *ec2subnet.AvailabilityZone,
}
})
instanceTypes, err := instanceTypeProvider.List(ctx, &v1.KubeletConfiguration{}, nodeClass)
instanceTypes, err := instanceTypeProvider.List(ctx, nodeClass)
if err != nil {
log.Fatalf("listing instance types, %s", err)
}
Expand Down
9 changes: 2 additions & 7 deletions pkg/cloudprovider/cloudprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
stderrors "errors"
"fmt"
"net/http"
"time"

"github.com/aws/aws-sdk-go/service/ec2"
Expand Down Expand Up @@ -159,10 +158,6 @@ func (c *CloudProvider) Get(ctx context.Context, providerID string) (*karpv1.Nod
return c.instanceToNodeClaim(instance, instanceType, nc), nil
}

func (c *CloudProvider) LivenessProbe(req *http.Request) error {
return c.instanceTypeProvider.LivenessProbe(req)
}

// GetInstanceTypes returns all available InstanceTypes
func (c *CloudProvider) GetInstanceTypes(ctx context.Context, nodePool *karpv1.NodePool) ([]*cloudprovider.InstanceType, error) {
nodeClass, err := c.resolveNodeClassFromNodePool(ctx, nodePool)
Expand All @@ -176,7 +171,7 @@ func (c *CloudProvider) GetInstanceTypes(ctx context.Context, nodePool *karpv1.N
return nil, fmt.Errorf("resolving node class, %w", err)
}
// TODO, break this coupling
instanceTypes, err := c.instanceTypeProvider.List(ctx, nodeClass.Spec.Kubelet, nodeClass)
instanceTypes, err := c.instanceTypeProvider.List(ctx, nodeClass)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -260,7 +255,7 @@ func (c *CloudProvider) resolveNodeClassFromNodePool(ctx context.Context, nodePo
}

func (c *CloudProvider) resolveInstanceTypes(ctx context.Context, nodeClaim *karpv1.NodeClaim, nodeClass *v1.EC2NodeClass) ([]*cloudprovider.InstanceType, error) {
instanceTypes, err := c.instanceTypeProvider.List(ctx, nodeClass.Spec.Kubelet, nodeClass)
instanceTypes, err := c.instanceTypeProvider.List(ctx, nodeClass)
if err != nil {
return nil, fmt.Errorf("getting instance types, %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import (
func NewControllers(ctx context.Context, mgr manager.Manager, 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, instanceTypeProvider instancetype.Provider) []controller.Controller {
pricingProvider pricing.Provider, amiProvider amifamily.Provider, launchTemplateProvider launchtemplate.Provider, instanceTypeProvider *instancetype.DefaultProvider) []controller.Controller {

controllers := []controller.Controller{
nodeclasshash.NewController(kubeClient),
Expand Down
10 changes: 5 additions & 5 deletions pkg/controllers/providers/instancetype/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ import (
)

type Controller struct {
instancetypeProvider instancetype.Provider
instanceTypeProvider *instancetype.DefaultProvider
}

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

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

work := []func(ctx context.Context) error{
c.instancetypeProvider.UpdateInstanceTypes,
c.instancetypeProvider.UpdateInstanceTypeOfferings,
c.instanceTypeProvider.UpdateInstanceTypes,
c.instanceTypeProvider.UpdateInstanceTypeOfferings,
}
errs := make([]error, len(work))
lop.ForEach(work, func(f func(ctx context.Context) error, i int) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/controllers/providers/instancetype/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var _ = Describe("InstanceType", func() {
})

ExpectSingletonReconciled(ctx, controller)
instanceTypes, err := awsEnv.InstanceTypesProvider.List(ctx, &v1.KubeletConfiguration{}, &v1.EC2NodeClass{
instanceTypes, err := awsEnv.InstanceTypesProvider.List(ctx, &v1.EC2NodeClass{
Status: v1.EC2NodeClassStatus{
Subnets: []v1.Subnet{
{
Expand Down Expand Up @@ -123,7 +123,7 @@ var _ = Describe("InstanceType", func() {
})

ExpectSingletonReconciled(ctx, controller)
instanceTypes, err := awsEnv.InstanceTypesProvider.List(ctx, &v1.KubeletConfiguration{}, &v1.EC2NodeClass{
instanceTypes, err := awsEnv.InstanceTypesProvider.List(ctx, &v1.EC2NodeClass{
Status: v1.EC2NodeClassStatus{
Subnets: []v1.Subnet{
{
Expand Down Expand Up @@ -158,14 +158,14 @@ var _ = Describe("InstanceType", func() {
awsEnv.EC2API.DescribeInstanceTypesOutput.Set(&ec2.DescribeInstanceTypesOutput{})
awsEnv.EC2API.DescribeInstanceTypeOfferingsOutput.Set(&ec2.DescribeInstanceTypeOfferingsOutput{})
ExpectSingletonReconciled(ctx, controller)
_, err := awsEnv.InstanceTypesProvider.List(ctx, &v1.KubeletConfiguration{}, &v1.EC2NodeClass{})
_, err := awsEnv.InstanceTypesProvider.List(ctx, &v1.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{})
ExpectSingletonReconciled(ctx, controller)
_, err := awsEnv.InstanceTypesProvider.List(ctx, &v1.KubeletConfiguration{}, &v1.EC2NodeClass{})
_, err := awsEnv.InstanceTypesProvider.List(ctx, &v1.EC2NodeClass{})
Expect(err).ToNot(BeNil())
})
})
6 changes: 2 additions & 4 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type Operator struct {
LaunchTemplateProvider launchtemplate.Provider
PricingProvider pricing.Provider
VersionProvider version.Provider
InstanceTypesProvider instancetype.Provider
InstanceTypesProvider *instancetype.DefaultProvider
InstanceProvider instance.Provider
SSMProvider ssmp.Provider
}
Expand Down Expand Up @@ -158,12 +158,10 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont
clusterEndpoint,
)
instanceTypeProvider := instancetype.NewDefaultProvider(
*sess.Config.Region,
cache.New(awscache.InstanceTypesAndZonesTTL, awscache.DefaultCleanupInterval),
ec2api,
subnetProvider,
unavailableOfferingsCache,
pricingProvider,
instancetype.NewDefaultResolver(*sess.Config.Region, pricingProvider, unavailableOfferingsCache),
)
instanceProvider := instance.NewDefaultProvider(
ctx,
Expand Down
Loading

0 comments on commit 027ed24

Please sign in to comment.