Skip to content

Commit e989255

Browse files
committed
Added check to filter supported nodes by amis
Signed-off-by: gnana997 <[email protected]>
1 parent 0a8251d commit e989255

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

pkg/providers/amifamily/ami.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,28 @@ func MapToInstanceTypes(instanceTypes []*cloudprovider.InstanceType, amis []v1.A
204204
return amiIDs
205205
}
206206

207+
// FilterInstanceTypesByAMICompatibility filters instance types to only include those that are compatible with at least one AMI
208+
// This prevents launching instances that don't have compatible AMIs available
209+
func FilterInstanceTypesByAMICompatibility(instanceTypes []*cloudprovider.InstanceType, amis []v1.AMI) []*cloudprovider.InstanceType {
210+
if len(amis) == 0 {
211+
return []*cloudprovider.InstanceType{}
212+
}
213+
214+
compatible := []*cloudprovider.InstanceType{}
215+
for _, instanceType := range instanceTypes {
216+
for _, ami := range amis {
217+
if err := instanceType.Requirements.Compatible(
218+
scheduling.NewNodeSelectorRequirements(ami.Requirements...),
219+
scheduling.AllowUndefinedWellKnownLabels,
220+
); err == nil {
221+
compatible = append(compatible, instanceType)
222+
break
223+
}
224+
}
225+
}
226+
return compatible
227+
}
228+
207229
// Compare two AMI's based on their deprecation status, creation time or name
208230
// If both AMIs are deprecated, compare creation time and return the one with the newer creation time
209231
// If both AMIs are non-deprecated, compare creation time and return the one with the newer creation time

pkg/providers/instancetype/instancetype.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"sync/atomic"
2222

2323
"k8s.io/apimachinery/pkg/api/resource"
24+
"k8s.io/apimachinery/pkg/util/sets"
25+
"sigs.k8s.io/controller-runtime/pkg/log"
2426
"sigs.k8s.io/karpenter/pkg/scheduling"
2527

2628
awscache "github.com/aws/karpenter-provider-aws/pkg/cache"
@@ -32,14 +34,12 @@ import (
3234
"github.com/mitchellh/hashstructure/v2"
3335
"github.com/patrickmn/go-cache"
3436
corev1 "k8s.io/api/core/v1"
35-
"sigs.k8s.io/controller-runtime/pkg/log"
3637
karpv1 "sigs.k8s.io/karpenter/pkg/apis/v1"
3738

3839
"github.com/aws/aws-sdk-go-v2/aws"
3940
"github.com/aws/aws-sdk-go-v2/service/ec2"
4041
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
4142
"github.com/samber/lo"
42-
"k8s.io/apimachinery/pkg/util/sets"
4343

4444
v1 "github.com/aws/karpenter-provider-aws/pkg/apis/v1"
4545
sdk "github.com/aws/karpenter-provider-aws/pkg/aws"
@@ -157,12 +157,24 @@ func (p *DefaultProvider) List(ctx context.Context, nodeClass *v1.EC2NodeClass)
157157
// date capacity information. Rather than incurring a cache miss each time an instance is launched into a reserved
158158
// offering (or terminated), offerings are injected to the cached instance types on each call. Note that on-demand and
159159
// spot offerings are still cached - only reserved offerings are generated each time.
160-
return p.offeringProvider.InjectOfferings(
160+
instanceTypesWithOfferings := p.offeringProvider.InjectOfferings(
161161
ctx,
162162
instanceTypes,
163163
nodeClass,
164164
p.allZones,
165-
), nil
165+
)
166+
167+
// Filter instance types that don't have compatible AMIs to prevent architecture mismatches
168+
filteredInstanceTypes := amifamily.FilterInstanceTypesByAMICompatibility(instanceTypesWithOfferings, nodeClass.Status.AMIs)
169+
if len(filteredInstanceTypes) == 0 {
170+
log.FromContext(ctx).WithValues(
171+
"nodeclass", nodeClass.Name,
172+
"ami-count", len(nodeClass.Status.AMIs),
173+
"instance-type-count", len(instanceTypesWithOfferings),
174+
).V(4).Info("no instance types are compatible with available AMIs, check NodePool and NodeClass architecture requirements")
175+
}
176+
177+
return filteredInstanceTypes, nil
166178
}
167179

168180
func (p *DefaultProvider) resolveInstanceTypes(

0 commit comments

Comments
 (0)