Skip to content

Commit

Permalink
chore: Add an amifamily.Resolver interface (#7064)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Sep 25, 2024
1 parent d3b06cc commit dde5589
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type Operator struct {
SecurityGroupProvider securitygroup.Provider
InstanceProfileProvider instanceprofile.Provider
AMIProvider amifamily.Provider
AMIResolver *amifamily.Resolver
AMIResolver amifamily.Resolver
LaunchTemplateProvider launchtemplate.Provider
PricingProvider pricing.Provider
VersionProvider version.Provider
Expand Down Expand Up @@ -143,7 +143,7 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont
versionProvider := version.NewDefaultProvider(operator.KubernetesInterface, cache.New(awscache.DefaultTTL, awscache.DefaultCleanupInterval))
ssmProvider := ssmp.NewDefaultProvider(ssm.New(sess), cache.New(awscache.SSMGetParametersByPathTTL, awscache.DefaultCleanupInterval))
amiProvider := amifamily.NewDefaultProvider(versionProvider, ssmProvider, ec2api, cache.New(awscache.DefaultTTL, awscache.DefaultCleanupInterval))
amiResolver := amifamily.NewResolver(amiProvider)
amiResolver := amifamily.NewDefaultResolver()
launchTemplateProvider := launchtemplate.NewDefaultProvider(
ctx,
cache.New(awscache.DefaultTTL, awscache.DefaultCleanupInterval),
Expand Down
22 changes: 11 additions & 11 deletions pkg/providers/amifamily/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ var DefaultEBS = v1.BlockDevice{
VolumeSize: lo.ToPtr(resource.MustParse("20Gi")),
}

// Resolver is able to fill-in dynamic launch template parameters
type Resolver struct {
amiProvider Provider
type Resolver interface {
Resolve(*v1.EC2NodeClass, *karpv1.NodeClaim, []*cloudprovider.InstanceType, string, *Options) ([]*LaunchTemplate, error)
}

// DefaultResolver is able to fill-in dynamic launch template parameters
type DefaultResolver struct{}

// Options define the static launch template parameters
type Options struct {
ClusterName string
Expand Down Expand Up @@ -111,16 +113,14 @@ func (d DefaultFamily) FeatureFlags() FeatureFlags {
}
}

// NewResolver constructs a new launch template Resolver
func NewResolver(amiProvider Provider) *Resolver {
return &Resolver{
amiProvider: amiProvider,
}
// NewDefaultResolver constructs a new launch template DefaultResolver
func NewDefaultResolver() *DefaultResolver {
return &DefaultResolver{}
}

// Resolve generates launch templates using the static options and dynamically generates launch template parameters.
// Multiple ResolvedTemplates are returned based on the instanceTypes passed in to support special AMIs for certain instance types like GPUs.
func (r Resolver) Resolve(nodeClass *v1.EC2NodeClass, nodeClaim *karpv1.NodeClaim, instanceTypes []*cloudprovider.InstanceType, capacityType string, options *Options) ([]*LaunchTemplate, error) {
func (r DefaultResolver) Resolve(nodeClass *v1.EC2NodeClass, nodeClaim *karpv1.NodeClaim, instanceTypes []*cloudprovider.InstanceType, capacityType string, options *Options) ([]*LaunchTemplate, error) {
amiFamily := GetAMIFamily(nodeClass.AMIFamily(), options)
if len(nodeClass.Status.AMIs) == 0 {
return nil, fmt.Errorf("no amis exist given constraints")
Expand Down Expand Up @@ -184,7 +184,7 @@ func (o Options) DefaultMetadataOptions() *v1.MetadataOptions {
}
}

func (r Resolver) defaultClusterDNS(opts *Options, kubeletConfig *v1.KubeletConfiguration) *v1.KubeletConfiguration {
func (r DefaultResolver) defaultClusterDNS(opts *Options, kubeletConfig *v1.KubeletConfiguration) *v1.KubeletConfiguration {
if opts.KubeDNSIP == nil {
return kubeletConfig
}
Expand All @@ -201,7 +201,7 @@ func (r Resolver) defaultClusterDNS(opts *Options, kubeletConfig *v1.KubeletConf
return newKubeletConfig
}

func (r Resolver) resolveLaunchTemplate(nodeClass *v1.EC2NodeClass, nodeClaim *karpv1.NodeClaim, instanceTypes []*cloudprovider.InstanceType, capacityType string,
func (r DefaultResolver) resolveLaunchTemplate(nodeClass *v1.EC2NodeClass, nodeClaim *karpv1.NodeClaim, instanceTypes []*cloudprovider.InstanceType, capacityType string,
amiFamily AMIFamily, amiID string, maxPods int, efaCount int, options *Options) *LaunchTemplate {
kubeletConfig := &v1.KubeletConfiguration{}
if nodeClass.Spec.Kubelet != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/providers/launchtemplate/launchtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type DefaultProvider struct {
sync.Mutex
ec2api ec2iface.EC2API
eksapi eksiface.EKSAPI
amiFamily *amifamily.Resolver
amiFamily amifamily.Resolver
securityGroupProvider securitygroup.Provider
subnetProvider subnet.Provider
cache *cache.Cache
Expand All @@ -81,7 +81,7 @@ type DefaultProvider struct {
ClusterCIDR atomic.Pointer[string]
}

func NewDefaultProvider(ctx context.Context, cache *cache.Cache, ec2api ec2iface.EC2API, eksapi eksiface.EKSAPI, amiFamily *amifamily.Resolver,
func NewDefaultProvider(ctx context.Context, cache *cache.Cache, ec2api ec2iface.EC2API, eksapi eksiface.EKSAPI, amiFamily amifamily.Resolver,
securityGroupProvider securitygroup.Provider, subnetProvider subnet.Provider,
caBundle *string, startAsync <-chan struct{}, kubeDNSIP net.IP, clusterEndpoint string) *DefaultProvider {
l := &DefaultProvider{
Expand Down
4 changes: 2 additions & 2 deletions pkg/test/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type Environment struct {
InstanceProfileProvider *instanceprofile.DefaultProvider
PricingProvider *pricing.DefaultProvider
AMIProvider *amifamily.DefaultProvider
AMIResolver *amifamily.Resolver
AMIResolver *amifamily.DefaultResolver
VersionProvider *version.DefaultProvider
LaunchTemplateProvider *launchtemplate.DefaultProvider
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func NewEnvironment(ctx context.Context, env *coretest.Environment) *Environment
instanceProfileProvider := instanceprofile.NewDefaultProvider(fake.DefaultRegion, iamapi, instanceProfileCache)
ssmProvider := ssmp.NewDefaultProvider(ssmapi, ssmCache)
amiProvider := amifamily.NewDefaultProvider(versionProvider, ssmProvider, ec2api, ec2Cache)
amiResolver := amifamily.NewResolver(amiProvider)
amiResolver := amifamily.NewDefaultResolver()
instanceTypesProvider := instancetype.NewDefaultProvider(fake.DefaultRegion, instanceTypeCache, ec2api, subnetProvider, unavailableOfferingsCache, pricingProvider)
launchTemplateProvider :=
launchtemplate.NewDefaultProvider(
Expand Down

0 comments on commit dde5589

Please sign in to comment.