Skip to content

Commit

Permalink
Merge pull request kubernetes#4489 from airbnb/drmorr--early-abort-if…
Browse files Browse the repository at this point in the history
…-aws-node-group-no-capacity

Early abort if AWS node group has no capacity
  • Loading branch information
k8s-ci-robot authored and Anton Kirillov committed Oct 27, 2022
1 parent af8d362 commit e74e4f9
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Note: The keys for the tags that you entered don't have values. Cluster Autoscal
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:DescribeScalingActivities",
"autoscaling:DescribeTags",
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup"
Expand Down
36 changes: 36 additions & 0 deletions cluster-autoscaler/cloudprovider/aws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@ by specifying Auto Scaling Group ARNs in the `Resource` list of the policy. More
information can be found
[here](https://docs.aws.amazon.com/autoscaling/latest/userguide/control-access-using-iam.html#policy-auto-scaling-resources).

*NOTE:* The below policies/arguments to the Cluster Autoscaler need to be modified as appropriate
for the names of your ASGs, as well as account ID and AWS region before being used.

The following policy provides the minimum privileges necessary for Cluster Autoscaler to run.
When using this policy, you cannot use autodiscovery of ASGs. In addition, it restricts the
IAM permissions to the node groups the Cluster Autoscaler is configured to scale.

This in turn means that you must pass the following arguments to the Cluster Autoscaler
binary, replacing min and max node counts and the ASG:

```bash
--aws-use-static-instance-list=false
--nodes=1:100:exampleASG1
--nodes=1:100:exampleASG2
```

```json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:DescribeScalingActivities",
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup"
],
"Resource": ["arn:aws:autoscaling:${YOUR_CLUSTER_AWS_REGION}:${YOUR_AWS_ACCOUNT_ID}:autoScalingGroup:*:autoScalingGroupName/${YOUR_ASG_NAME}"]
}
]
}
```

### Using OIDC Federated Authentication
OIDC federated authentication allows your service to assume an IAM role and interact with AWS services without having to store credentials as environment variables. For an example of how to use AWS IAM OIDC with the Cluster Autoscaler please see [here](CA_with_AWS_IAM_OIDC.md).

Expand Down
134 changes: 92 additions & 42 deletions cluster-autoscaler/cloudprovider/aws/auto_scaling_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ import (
)

const (
scaleToZeroSupported = true
placeholderInstanceNamePrefix = "i-placeholder"
scaleToZeroSupported = true
placeholderInstanceNamePrefix = "i-placeholder"
placeholderUnfulfillableStatus = "placeholder-cannot-be-fulfilled"
)

type asgCache struct {
registeredAsgs []*asg
registeredAsgs map[AwsRef]*asg
asgToInstances map[AwsRef][]AwsInstanceRef
instanceToAsg map[AwsInstanceRef]*asg
instanceStatus map[AwsInstanceRef]*string
asgInstanceTypeCache *instanceTypeExpirationStore
mutex sync.Mutex
awsService *awsWrapper
Expand All @@ -62,9 +64,10 @@ type mixedInstancesPolicy struct {
type asg struct {
AwsRef

minSize int
maxSize int
curSize int
minSize int
maxSize int
curSize int
lastUpdateTime time.Time

AvailabilityZones []string
LaunchConfigurationName string
Expand All @@ -75,10 +78,11 @@ type asg struct {

func newASGCache(awsService *awsWrapper, explicitSpecs []string, autoDiscoverySpecs []asgAutoDiscoveryConfig) (*asgCache, error) {
registry := &asgCache{
registeredAsgs: make([]*asg, 0),
registeredAsgs: make(map[AwsRef]*asg, 0),
awsService: awsService,
asgToInstances: make(map[AwsRef][]AwsInstanceRef),
instanceToAsg: make(map[AwsInstanceRef]*asg),
instanceStatus: make(map[AwsInstanceRef]*string),
asgInstanceTypeCache: newAsgInstanceTypeCache(awsService),
interrupt: make(chan struct{}),
asgAutoDiscoverySpecs: autoDiscoverySpecs,
Expand Down Expand Up @@ -121,53 +125,44 @@ func (m *asgCache) parseExplicitAsgs(specs []string) error {

// Register ASG. Returns the registered ASG.
func (m *asgCache) register(asg *asg) *asg {
for i := range m.registeredAsgs {
if existing := m.registeredAsgs[i]; existing.AwsRef == asg.AwsRef {
if reflect.DeepEqual(existing, asg) {
return existing
}
if existing, asgExists := m.registeredAsgs[asg.AwsRef]; asgExists {
if reflect.DeepEqual(existing, asg) {
return existing
}

klog.V(4).Infof("Updating ASG %s", asg.AwsRef.Name)
klog.V(4).Infof("Updating ASG %s", asg.AwsRef.Name)

// Explicit registered groups should always use the manually provided min/max
// values and the not the ones returned by the API
if !m.explicitlyConfigured[asg.AwsRef] {
existing.minSize = asg.minSize
existing.maxSize = asg.maxSize
}
// Explicit registered groups should always use the manually provided min/max
// values and the not the ones returned by the API
if !m.explicitlyConfigured[asg.AwsRef] {
existing.minSize = asg.minSize
existing.maxSize = asg.maxSize
}

existing.curSize = asg.curSize
existing.curSize = asg.curSize

// Those information are mainly required to create templates when scaling
// from zero
existing.AvailabilityZones = asg.AvailabilityZones
existing.LaunchConfigurationName = asg.LaunchConfigurationName
existing.LaunchTemplate = asg.LaunchTemplate
existing.MixedInstancesPolicy = asg.MixedInstancesPolicy
existing.Tags = asg.Tags
// Those information are mainly required to create templates when scaling
// from zero
existing.AvailabilityZones = asg.AvailabilityZones
existing.LaunchConfigurationName = asg.LaunchConfigurationName
existing.LaunchTemplate = asg.LaunchTemplate
existing.MixedInstancesPolicy = asg.MixedInstancesPolicy
existing.Tags = asg.Tags

return existing
}
return existing
}
klog.V(1).Infof("Registering ASG %s", asg.AwsRef.Name)
m.registeredAsgs = append(m.registeredAsgs, asg)
m.registeredAsgs[asg.AwsRef] = asg
return asg
}

// Unregister ASG. Returns the unregistered ASG.
func (m *asgCache) unregister(a *asg) *asg {
updated := make([]*asg, 0, len(m.registeredAsgs))
var changed *asg
for _, existing := range m.registeredAsgs {
if existing.AwsRef == a.AwsRef {
klog.V(1).Infof("Unregistered ASG %s", a.AwsRef.Name)
changed = a
continue
}
updated = append(updated, existing)
if _, asgExists := m.registeredAsgs[a.AwsRef]; asgExists {
klog.V(1).Infof("Unregistered ASG %s", a.AwsRef.Name)
delete(m.registeredAsgs, a.AwsRef)
}
m.registeredAsgs = updated
return changed
return a
}

func (m *asgCache) buildAsgFromSpec(spec string) (*asg, error) {
Expand All @@ -184,7 +179,7 @@ func (m *asgCache) buildAsgFromSpec(spec string) (*asg, error) {
}

// Get returns the currently registered ASGs
func (m *asgCache) Get() []*asg {
func (m *asgCache) Get() map[AwsRef]*asg {
m.mutex.Lock()
defer m.mutex.Unlock()

Expand Down Expand Up @@ -226,6 +221,17 @@ func (m *asgCache) InstancesByAsg(ref AwsRef) ([]AwsInstanceRef, error) {
return nil, fmt.Errorf("error while looking for instances of ASG: %s", ref)
}

func (m *asgCache) InstanceStatus(ref AwsInstanceRef) (*string, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

if status, found := m.instanceStatus[ref]; found {
return status, nil
}

return nil, fmt.Errorf("could not find instance %v", ref)
}

func (m *asgCache) SetAsgSize(asg *asg, size int) error {
m.mutex.Lock()
defer m.mutex.Unlock()
Expand All @@ -248,6 +254,7 @@ func (m *asgCache) setAsgSizeNoLock(asg *asg, size int) error {
}

// Proactively set the ASG size so autoscaler makes better decisions
asg.lastUpdateTime = start
asg.curSize = size

return nil
Expand Down Expand Up @@ -367,6 +374,7 @@ func (m *asgCache) regenerate() error {

newInstanceToAsgCache := make(map[AwsInstanceRef]*asg)
newAsgToInstancesCache := make(map[AwsRef][]AwsInstanceRef)
newInstanceStatusMap := make(map[AwsInstanceRef]*string)

// Build list of known ASG names
refreshNames, err := m.buildAsgNames()
Expand Down Expand Up @@ -403,6 +411,7 @@ func (m *asgCache) regenerate() error {
ref := m.buildInstanceRefFromAWS(instance)
newInstanceToAsgCache[ref] = asg
newAsgToInstancesCache[asg.AwsRef][i] = ref
newInstanceStatusMap[ref] = instance.HealthStatus
}
}

Expand Down Expand Up @@ -431,6 +440,7 @@ func (m *asgCache) regenerate() error {
m.asgToInstances = newAsgToInstancesCache
m.instanceToAsg = newInstanceToAsgCache
m.autoscalingOptions = newAutoscalingOptions
m.instanceStatus = newInstanceStatusMap
return nil
}

Expand All @@ -444,17 +454,57 @@ func (m *asgCache) createPlaceholdersForDesiredNonStartedInstances(groups []*aut

klog.V(4).Infof("Instance group %s has only %d instances created while requested count is %d. "+
"Creating placeholder instances.", *g.AutoScalingGroupName, realInstances, desired)

healthStatus := ""
isAvailable, err := m.isNodeGroupAvailable(g)
if err != nil {
klog.V(4).Infof("Could not check instance availability, creating placeholder node anyways: %v", err)
} else if !isAvailable {
klog.Warningf("Instance group %s cannot provision any more nodes!", *g.AutoScalingGroupName)
healthStatus = placeholderUnfulfillableStatus
}

for i := realInstances; i < desired; i++ {
id := fmt.Sprintf("%s-%s-%d", placeholderInstanceNamePrefix, *g.AutoScalingGroupName, i)
g.Instances = append(g.Instances, &autoscaling.Instance{
InstanceId: &id,
AvailabilityZone: g.AvailabilityZones[0],
HealthStatus: &healthStatus,
})
}
}
return groups
}

func (m *asgCache) isNodeGroupAvailable(group *autoscaling.Group) (bool, error) {
input := &autoscaling.DescribeScalingActivitiesInput{
AutoScalingGroupName: group.AutoScalingGroupName,
}

start := time.Now()
response, err := m.awsService.DescribeScalingActivities(input)
observeAWSRequest("DescribeScalingActivities", err, start)
if err != nil {
return true, err // If we can't describe the scaling activities we assume the node group is available
}

for _, activity := range response.Activities {
asgRef := AwsRef{Name: *group.AutoScalingGroupName}
if a, ok := m.registeredAsgs[asgRef]; ok {
lut := a.lastUpdateTime
if activity.StartTime.Before(lut) {
break
} else if *activity.StatusCode == "Failed" {
klog.Warningf("ASG %s scaling failed with %s", asgRef.Name, *activity)
return false, nil
}
} else {
klog.V(4).Infof("asg %v is not registered yet, skipping DescribeScalingActivities check", asgRef.Name)
}
}
return true, nil
}

func (m *asgCache) buildAsgFromAWS(g *autoscaling.Group) (*asg, error) {
spec := dynamic.NodeGroupSpec{
Name: aws.StringValue(g.AutoScalingGroupName),
Expand Down
Loading

0 comments on commit e74e4f9

Please sign in to comment.