Skip to content

Commit

Permalink
Refactor instance.isProtectedFromTermination (LeanerCloud#393)
Browse files Browse the repository at this point in the history
Split off from LeanerCloud#354 with some small tweaks to get tests to pass.
  • Loading branch information
gabegorelick authored and cristim committed Jan 28, 2020
1 parent 33a444c commit 57a1c06
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
17 changes: 14 additions & 3 deletions core/autoscaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,20 @@ func (a *autoScalingGroup) getInstance(
continue
}

if considerInstanceProtection && (i.isProtectedFromScaleIn() || i.isProtectedFromTermination()) {
debug.Println(a.name, "skipping protected instance", *i.InstanceId)
continue
if considerInstanceProtection {
protected := i.isProtectedFromScaleIn()
if !protected {
protectedT, err := i.isProtectedFromTermination()
if err != nil {
debug.Println(a.name, "failed to determine termination protection for", *i.InstanceId)
}
protected = protectedT
}

if protected {
debug.Println(a.name, "skipping protected instance", *i.InstanceId)
continue
}
}

if (availabilityZone != nil) && (*availabilityZone != *i.Placement.AvailabilityZone) {
Expand Down
18 changes: 2 additions & 16 deletions core/autoscaling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1210,22 +1210,8 @@ func TestGetOnDemandInstanceInAZ(t *testing.T) {
},
},
),
az: aws.String("1b"),
expected: &instance{
Instance: &ec2.Instance{
InstanceId: aws.String("ondemand-running"),
State: &ec2.InstanceState{Name: aws.String(ec2.InstanceStateNameRunning)},
Placement: &ec2.Placement{AvailabilityZone: aws.String("1b")},
InstanceLifecycle: aws.String(""),
},
region: &region{
services: connections{
ec2: mockEC2{
diaerr: errors.New("error when determining instance termination protection"),
},
},
},
},
az: aws.String("1b"),
expected: nil,
},

{
Expand Down
16 changes: 12 additions & 4 deletions core/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,24 +146,32 @@ func (i *instance) isSpot() bool {
*i.InstanceLifecycle == "spot"
}

func (i *instance) isProtectedFromTermination() bool {
func (i *instance) isProtectedFromTermination() (bool, error) {

debug.Println("\tChecking termination protection for instance: ", *i.InstanceId)
// determine and set the API termination protection field
diaRes, err := i.region.services.ec2.DescribeInstanceAttribute(
&ec2.DescribeInstanceAttributeInput{
Attribute: aws.String("disableApiTermination"),
InstanceId: i.InstanceId,
})

if err == nil &&
if err != nil {
// better safe than sorry!
logger.Printf("Couldn't describe instance attributes, assuming instance %v is protected: %v\n",
*i.InstanceId, err.Error())
return true, err
}

if diaRes != nil &&
diaRes.DisableApiTermination != nil &&
diaRes.DisableApiTermination.Value != nil &&
*diaRes.DisableApiTermination.Value {
logger.Printf("\t: %v Instance, %v is protected from termination\n",
*i.Placement.AvailabilityZone, *i.InstanceId)
return true
return true, nil
}
return false
return false, nil
}

func (i *instance) isProtectedFromScaleIn() bool {
Expand Down

0 comments on commit 57a1c06

Please sign in to comment.