-
Notifications
You must be signed in to change notification settings - Fork 967
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
368 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}) | ||
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()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.