diff --git a/vendor/k8s.io/kubernetes/pkg/cloudprovider/providers/aws/aws.go b/vendor/k8s.io/kubernetes/pkg/cloudprovider/providers/aws/aws.go index e2890dedc3ce..120bbed27d9e 100644 --- a/vendor/k8s.io/kubernetes/pkg/cloudprovider/providers/aws/aws.go +++ b/vendor/k8s.io/kubernetes/pkg/cloudprovider/providers/aws/aws.go @@ -1324,7 +1324,7 @@ func (c *Cloud) NodeAddressesByProviderID(ctx context.Context, providerID string return extractNodeAddresses(instance) } -// InstanceExistsByProviderID returns true if the instance with the given provider id still exists and is running. +// InstanceExistsByProviderID returns true if the instance with the given provider id still exists. // If false is returned with no error, the instance will be immediately deleted by the cloud controller manager. func (c *Cloud) InstanceExistsByProviderID(ctx context.Context, providerID string) (bool, error) { instanceID, err := kubernetesInstanceID(providerID).mapToAWSInstanceID() @@ -1348,8 +1348,8 @@ func (c *Cloud) InstanceExistsByProviderID(ctx context.Context, providerID strin } state := instances[0].State.Name - if *state != "running" { - glog.Warningf("the instance %s is not running", instanceID) + if *state == "terminated" { + glog.Warningf("the instance %s is terminated", instanceID) return false, nil } @@ -4300,7 +4300,8 @@ func (c *Cloud) findInstanceByNodeName(nodeName types.NodeName) (*ec2.Instance, privateDNSName := mapNodeNameToPrivateDNSName(nodeName) filters := []*ec2.Filter{ newEc2Filter("private-dns-name", privateDNSName), - newEc2Filter("instance-state-name", "running"), + // exclude instances in "terminated" state + newEc2Filter("instance-state-name", "pending", "running", "shutting-down", "stopping", "stopped"), } instances, err := c.describeInstances(filters) diff --git a/vendor/k8s.io/kubernetes/pkg/cloudprovider/providers/aws/aws_test.go b/vendor/k8s.io/kubernetes/pkg/cloudprovider/providers/aws/aws_test.go index 770b908f0638..d25267b7244b 100644 --- a/vendor/k8s.io/kubernetes/pkg/cloudprovider/providers/aws/aws_test.go +++ b/vendor/k8s.io/kubernetes/pkg/cloudprovider/providers/aws/aws_test.go @@ -797,6 +797,18 @@ func TestIpPermissionExistsHandlesMultipleGroupIdsWithUserIds(t *testing.T) { } func TestFindInstanceByNodeNameExcludesTerminatedInstances(t *testing.T) { + awsStates := []struct { + id int64 + state string + expected bool + }{ + {0, "pending", true}, + {16, "running", true}, + {32, "shutting-down", true}, + {48, "terminated", false}, + {64, "stopping", true}, + {80, "stopped", true}, + } awsServices := newMockedFakeAWSServices(TestClusterId) nodeName := types.NodeName("my-dns.internal") @@ -806,36 +818,41 @@ func TestFindInstanceByNodeNameExcludesTerminatedInstances(t *testing.T) { tag.Value = aws.String(TestClusterId) tags := []*ec2.Tag{&tag} - var runningInstance ec2.Instance - runningInstance.InstanceId = aws.String("i-running") - runningInstance.PrivateDnsName = aws.String(string(nodeName)) - runningInstance.State = &ec2.InstanceState{Code: aws.Int64(16), Name: aws.String("running")} - runningInstance.Tags = tags - - var terminatedInstance ec2.Instance - terminatedInstance.InstanceId = aws.String("i-terminated") - terminatedInstance.PrivateDnsName = aws.String(string(nodeName)) - terminatedInstance.State = &ec2.InstanceState{Code: aws.Int64(48), Name: aws.String("terminated")} - terminatedInstance.Tags = tags + var testInstance ec2.Instance + testInstance.PrivateDnsName = aws.String(string(nodeName)) + testInstance.Tags = tags - instances := []*ec2.Instance{&terminatedInstance, &runningInstance} - awsServices.instances = append(awsServices.instances, instances...) + awsDefaultInstances := awsServices.instances + for _, awsState := range awsStates { + id := "i-" + awsState.state + testInstance.InstanceId = aws.String(id) + testInstance.State = &ec2.InstanceState{Code: aws.Int64(awsState.id), Name: aws.String(awsState.state)} - c, err := newAWSCloud(CloudConfig{}, awsServices) - if err != nil { - t.Errorf("Error building aws cloud: %v", err) - return - } + awsServices.instances = append(awsDefaultInstances, &testInstance) - instance, err := c.findInstanceByNodeName(nodeName) + c, err := newAWSCloud(CloudConfig{}, awsServices) + if err != nil { + t.Errorf("Error building aws cloud: %v", err) + return + } - if err != nil { - t.Errorf("Failed to find instance: %v", err) - return - } + resultInstance, err := c.findInstanceByNodeName(nodeName) - if *instance.InstanceId != "i-running" { - t.Errorf("Expected running instance but got %v", *instance.InstanceId) + if awsState.expected { + if err != nil || resultInstance == nil { + t.Errorf("Expected to find instance %v", *testInstance.InstanceId) + return + } + if *resultInstance.InstanceId != *testInstance.InstanceId { + t.Errorf("Wrong instance returned by findInstanceByNodeName() expected: %v, actual: %v", *testInstance.InstanceId, *resultInstance.InstanceId) + return + } + } else { + if err == nil && resultInstance != nil { + t.Errorf("Did not expect to find instance %v", *resultInstance.InstanceId) + return + } + } } }