Skip to content

Commit

Permalink
cluster-autoscaler: use generated instance types
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Balestra <[email protected]>
  • Loading branch information
JulienBalestra authored and gjtempleton committed Jul 21, 2020
1 parent 86d70ea commit c10b201
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
26 changes: 16 additions & 10 deletions cluster-autoscaler/cloudprovider/aws/aws_cloud_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,13 @@ var (
type awsCloudProvider struct {
awsManager *AwsManager
resourceLimiter *cloudprovider.ResourceLimiter
// InstanceTypes is a map of ec2 resources
instanceTypes map[string]*InstanceType
}

// BuildAwsCloudProvider builds CloudProvider implementation for AWS.
func BuildAwsCloudProvider(awsManager *AwsManager, instanceTypes map[string]*InstanceType, resourceLimiter *cloudprovider.ResourceLimiter) (cloudprovider.CloudProvider, error) {
func BuildAwsCloudProvider(awsManager *AwsManager, resourceLimiter *cloudprovider.ResourceLimiter) (cloudprovider.CloudProvider, error) {
aws := &awsCloudProvider{
awsManager: awsManager,
resourceLimiter: resourceLimiter,
instanceTypes: instanceTypes,
}
return aws, nil
}
Expand Down Expand Up @@ -347,21 +344,30 @@ func BuildAWS(opts config.AutoscalingOptions, do cloudprovider.NodeGroupDiscover
}

// Generate EC2 list
var instanceTypes map[string]*InstanceType
var lastUpdateTime string
instanceTypes, lastUpdateTime := GetStaticEC2InstanceTypes()
if opts.AWSUseStaticInstanceList {
instanceTypes, lastUpdateTime = GetStaticEC2InstanceTypes()
klog.Warningf("Use static EC2 Instance Types and list could be outdated. Last update time: %s", lastUpdateTime)
} else {
region, err := GetCurrentAwsRegion()
if err != nil {
klog.Fatalf("Failed to get AWS Region: %v", err)
}

instanceTypes, err = GenerateEC2InstanceTypes(region)
generatedInstanceTypes, err := GenerateEC2InstanceTypes(region)
if err != nil {
klog.Fatalf("Failed to generate AWS EC2 Instance Types: %v", err)
}
// fallback on the static list if we miss any instance types in the generated output
// credits to: https://github.com/lyft/cni-ipvlan-vpc-k8s/pull/80
for k, v := range instanceTypes {
_, ok := generatedInstanceTypes[k]
if ok {
continue
}
klog.Infof("Using static instance type %s", k)
generatedInstanceTypes[k] = v
}
instanceTypes = generatedInstanceTypes

keys := make([]string, 0, len(instanceTypes))
for key := range instanceTypes {
Expand All @@ -371,12 +377,12 @@ func BuildAWS(opts config.AutoscalingOptions, do cloudprovider.NodeGroupDiscover
klog.Infof("Successfully load %d EC2 Instance Types %s", len(keys), keys)
}

manager, err := CreateAwsManager(config, do)
manager, err := CreateAwsManager(config, do, instanceTypes)
if err != nil {
klog.Fatalf("Failed to create AWS Manager: %v", err)
}

provider, err := BuildAwsCloudProvider(manager, instanceTypes, rl)
provider, err := BuildAwsCloudProvider(manager, rl)
if err != nil {
klog.Fatalf("Failed to create AWS cloud provider: %v", err)
}
Expand Down
9 changes: 6 additions & 3 deletions cluster-autoscaler/cloudprovider/aws/aws_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type AwsManager struct {
ec2Service ec2Wrapper
asgCache *asgCache
lastRefresh time.Time
instanceTypes map[string]*InstanceType
}

type asgTemplate struct {
Expand Down Expand Up @@ -174,6 +175,7 @@ func createAWSManagerInternal(
discoveryOpts cloudprovider.NodeGroupDiscoveryOptions,
autoScalingService *autoScalingWrapper,
ec2Service *ec2Wrapper,
instanceTypes map[string]*InstanceType,
) (*AwsManager, error) {

cfg, err := readAWSCloudConfig(configReader)
Expand Down Expand Up @@ -219,6 +221,7 @@ func createAWSManagerInternal(
autoScalingService: *autoScalingService,
ec2Service: *ec2Service,
asgCache: cache,
instanceTypes: instanceTypes,
}

if err := manager.forceRefresh(); err != nil {
Expand All @@ -244,8 +247,8 @@ func readAWSCloudConfig(config io.Reader) (*provider_aws.CloudConfig, error) {
}

// CreateAwsManager constructs awsManager object.
func CreateAwsManager(configReader io.Reader, discoveryOpts cloudprovider.NodeGroupDiscoveryOptions) (*AwsManager, error) {
return createAWSManagerInternal(configReader, discoveryOpts, nil, nil)
func CreateAwsManager(configReader io.Reader, discoveryOpts cloudprovider.NodeGroupDiscoveryOptions, instanceTypes map[string]*InstanceType) (*AwsManager, error) {
return createAWSManagerInternal(configReader, discoveryOpts, nil, nil, instanceTypes)
}

// Refresh is called before every main loop and can be used to dynamically update cloud provider state.
Expand Down Expand Up @@ -317,7 +320,7 @@ func (m *AwsManager) getAsgTemplate(asg *asg) (*asgTemplate, error) {
return nil, err
}

if t, ok := InstanceTypes[instanceTypeName]; ok {
if t, ok := m.instanceTypes[instanceTypeName]; ok {
return &asgTemplate{
InstanceType: t,
Region: region,
Expand Down

0 comments on commit c10b201

Please sign in to comment.