Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check instance state on termination failure #2253

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/cloudprovider/aws/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ var (

type SpotFallbackError error

type InstanceTerminatedError struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you just make this an error instead of a struct like SpotFallbackError?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually started with that, but I think the InstanceTerminatedError error ends up being the *errors.errorString type. Which means that the IsTerminatedError would return true for errors when we expect them to return false. Now that I think of it, I'm not 100% sure the isSpotFallback method is doing what we intend either. Will have to test it further.

Here's an example, btw.
https://go.dev/play/p/cswSjwGQFL2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

o wow, good catch!

error
}

func isSpotFallback(err error) bool {
if err == nil {
return false
Expand All @@ -51,6 +55,14 @@ func isSpotFallback(err error) bool {
return errors.As(err, &sfbErr)
}

func isInstanceTerminated(err error) bool {
if err == nil {
return false
}
var itErr InstanceTerminatedError
return errors.As(err, &itErr)
}

// isNotFound returns true if the err is an AWS error (even if it's
// wrapped) and is a known to mean "not found" (as opposed to a more
// serious or unexpected error)
Expand Down
4 changes: 4 additions & 0 deletions pkg/cloudprovider/aws/fake/ec2api.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,17 @@ func (e *EC2API) CreateFleetWithContext(_ context.Context, input *ec2.CreateFlee
if skipInstance {
continue
}
instanceState := ec2.InstanceStateNameRunning
for i := 0; i < int(*input.TargetCapacitySpecification.TotalTargetCapacity); i++ {
instance := &ec2.Instance{
InstanceId: aws.String(test.RandomName()),
Placement: &ec2.Placement{AvailabilityZone: input.LaunchTemplateConfigs[0].Overrides[0].AvailabilityZone},
PrivateDnsName: aws.String(randomdata.IpV4Address()),
InstanceType: input.LaunchTemplateConfigs[0].Overrides[0].InstanceType,
SpotInstanceRequestId: spotInstanceRequestID,
State: &ec2.InstanceState{
Name: &instanceState,
},
}
e.Instances.Store(*instance.InstanceId, instance)
instanceIds = append(instanceIds, instance.InstanceId)
Expand Down
13 changes: 12 additions & 1 deletion pkg/cloudprovider/aws/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ func (p *InstanceProvider) Terminate(ctx context.Context, node *v1.Node) error {
if isNotFound(err) {
return nil
}
if _, errMsg := p.getInstance(ctx, aws.StringValue(id)); err != nil {
if isInstanceTerminated(errMsg) || isNotFound(errMsg) {
logging.FromContext(ctx).Debugf("Instance already terminated, %s", node.Name)
return nil
}
err = multierr.Append(err, errMsg)
}

return fmt.Errorf("terminating instance %s, %w", node.Name, err)
}
return nil
Expand Down Expand Up @@ -286,9 +294,12 @@ func (p *InstanceProvider) getInstance(ctx context.Context, id string) (*ec2.Ins
return nil, fmt.Errorf("failed to describe ec2 instances, %w", err)
}
if len(describeInstancesOutput.Reservations) != 1 || len(describeInstancesOutput.Reservations[0].Instances) != 1 {
return nil, fmt.Errorf("expected instance but got 0")
return nil, InstanceTerminatedError{fmt.Errorf("expected instance but got 0")}
}
instance := describeInstancesOutput.Reservations[0].Instances[0]
if *instance.State.Name == ec2.InstanceStateNameTerminated {
return nil, InstanceTerminatedError{fmt.Errorf("instance is in terminated state")}
}
if injection.GetOptions(ctx).GetAWSNodeNameConvention() == options.ResourceName {
return instance, nil
}
Expand Down