Skip to content

Commit 6c6ccb6

Browse files
authored
cleanup persistent spot instances when machine is deleted (kubermatic#1030)
1 parent 8848df0 commit 6c6ccb6

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

pkg/cloudprovider/provider/aws/provider.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -655,15 +655,17 @@ func (p *provider) Create(machine *v1alpha1.Machine, data *cloudprovidertypes.Pr
655655
}
656656

657657
func (p *provider) Cleanup(machine *v1alpha1.Machine, _ *cloudprovidertypes.ProviderData) (bool, error) {
658-
instance, err := p.get(machine)
658+
ec2instance, err := p.get(machine)
659659
if err != nil {
660660
if err == cloudprovidererrors.ErrInstanceNotFound {
661661
return true, nil
662662
}
663663
return false, err
664664
}
665665

666+
//(*Config, *providerconfigtypes.Config, *awstypes.RawConfig, error)
666667
config, _, _, err := p.getConfig(machine.Spec.ProviderSpec)
668+
667669
if err != nil {
668670
return false, cloudprovidererrors.TerminalError{
669671
Reason: common.InvalidConfigurationMachineError,
@@ -676,15 +678,31 @@ func (p *provider) Cleanup(machine *v1alpha1.Machine, _ *cloudprovidertypes.Prov
676678
return false, err
677679
}
678680

681+
if config.IsSpotInstance != nil && *config.IsSpotInstance &&
682+
config.SpotPersistentRequest != nil && *config.SpotPersistentRequest {
683+
684+
cOut, err := ec2Client.CancelSpotInstanceRequests(&ec2.CancelSpotInstanceRequestsInput{
685+
SpotInstanceRequestIds: aws.StringSlice([]string{*ec2instance.instance.SpotInstanceRequestId}),
686+
})
687+
688+
if err != nil {
689+
return false, awsErrorToTerminalError(err, "failed to cancel spot instance request")
690+
}
691+
692+
if *cOut.CancelledSpotInstanceRequests[0].State == ec2.CancelSpotInstanceRequestStateCancelled {
693+
klog.V(3).Infof("successfully canceled spot instance request %s at aws", *ec2instance.instance.SpotInstanceRequestId)
694+
}
695+
}
696+
679697
tOut, err := ec2Client.TerminateInstances(&ec2.TerminateInstancesInput{
680-
InstanceIds: aws.StringSlice([]string{instance.ID()}),
698+
InstanceIds: aws.StringSlice([]string{ec2instance.ID()}),
681699
})
682700
if err != nil {
683701
return false, awsErrorToTerminalError(err, "failed to terminate instance")
684702
}
685703

686704
if *tOut.TerminatingInstances[0].PreviousState.Name != *tOut.TerminatingInstances[0].CurrentState.Name {
687-
klog.V(3).Infof("successfully triggered termination of instance %s at aws", instance.ID())
705+
klog.V(3).Infof("successfully triggered termination of instance %s at aws", ec2instance.ID())
688706
}
689707

690708
return false, nil

0 commit comments

Comments
 (0)