Skip to content

Commit cfdefff

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#59930 from zetaab/shutdoaw
Automatic merge from submit-queue (batch tested with PRs 67368, 59930, 68074). If you want to cherry-pick this change to another branch, please follow the instructions here: https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md. implement InstanceShutdownByProviderID to aws cloudprovider **What this PR does / why we need it**: implement InstanceShutdownByProviderID to aws cloudprovider **Which issue(s) this PR fixes**: Fixes kubernetes#59925 **Special notes for your reviewer**: **Release note**: ```release-note NONE ```
2 parents 144fa4e + a68cbd6 commit cfdefff

File tree

1 file changed

+31
-8
lines changed
  • pkg/cloudprovider/providers/aws

1 file changed

+31
-8
lines changed

pkg/cloudprovider/providers/aws/aws.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,7 @@ func (c *Cloud) NodeAddressesByProviderID(ctx context.Context, providerID string
13361336
return extractNodeAddresses(instance)
13371337
}
13381338

1339-
// InstanceExistsByProviderID returns true if the instance with the given provider id still exists and is running.
1339+
// InstanceExistsByProviderID returns true if the instance with the given provider id still exists.
13401340
// If false is returned with no error, the instance will be immediately deleted by the cloud controller manager.
13411341
func (c *Cloud) InstanceExistsByProviderID(ctx context.Context, providerID string) (bool, error) {
13421342
instanceID, err := kubernetesInstanceID(providerID).mapToAWSInstanceID()
@@ -1359,18 +1359,41 @@ func (c *Cloud) InstanceExistsByProviderID(ctx context.Context, providerID strin
13591359
return false, fmt.Errorf("multiple instances found for instance: %s", instanceID)
13601360
}
13611361

1362-
state := instances[0].State.Name
1363-
if *state != "running" {
1364-
glog.Warningf("the instance %s is not running", instanceID)
1365-
return false, nil
1366-
}
1367-
13681362
return true, nil
13691363
}
13701364

13711365
// InstanceShutdownByProviderID returns true if the instance is in safe state to detach volumes
13721366
func (c *Cloud) InstanceShutdownByProviderID(ctx context.Context, providerID string) (bool, error) {
1373-
return false, cloudprovider.NotImplemented
1367+
instanceID, err := kubernetesInstanceID(providerID).mapToAWSInstanceID()
1368+
if err != nil {
1369+
return false, err
1370+
}
1371+
1372+
request := &ec2.DescribeInstancesInput{
1373+
InstanceIds: []*string{instanceID.awsString()},
1374+
}
1375+
1376+
instances, err := c.ec2.DescribeInstances(request)
1377+
if err != nil {
1378+
return false, err
1379+
}
1380+
if len(instances) == 0 {
1381+
glog.Warningf("the instance %s does not exist anymore", providerID)
1382+
return true, nil
1383+
}
1384+
if len(instances) > 1 {
1385+
return false, fmt.Errorf("multiple instances found for instance: %s", instanceID)
1386+
}
1387+
1388+
instance := instances[0]
1389+
if instance.State != nil {
1390+
state := aws.StringValue(instance.State.Name)
1391+
// valid state for detaching volumes
1392+
if state == ec2.InstanceStateNameStopped || state == ec2.InstanceStateNameTerminated {
1393+
return true, nil
1394+
}
1395+
}
1396+
return false, nil
13741397
}
13751398

13761399
// InstanceID returns the cloud provider ID of the node with the specified nodeName.

0 commit comments

Comments
 (0)