Skip to content

Commit

Permalink
Merge pull request #484 from saurav-agarwalla/master
Browse files Browse the repository at this point in the history
Handle wrapped error for InvalidInstanceID.NotFound in InstanceExistsByProviderID
  • Loading branch information
k8s-ci-robot authored Sep 1, 2022
2 parents 3722b6b + 210b6fc commit f18f5e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/providers/v1/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,10 @@ func isAWSErrorInstanceNotFound(err error) bool {
if awsError.Code() == ec2.UnsuccessfulInstanceCreditSpecificationErrorCodeInvalidInstanceIdNotFound {
return true
}
} else if strings.Contains(err.Error(), ec2.UnsuccessfulInstanceCreditSpecificationErrorCodeInvalidInstanceIdNotFound) {
// In places like https://github.com/kubernetes/cloud-provider-aws/blob/1c6194aad0122ab44504de64187e3d1a7415b198/pkg/providers/v1/aws.go#L1007,
// the error has been transformed into something else so check the error string to see if it contains the error code we're looking for.
return true
}

return false
Expand Down
11 changes: 11 additions & 0 deletions pkg/providers/v1/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3805,6 +3805,17 @@ func TestInstanceExistsByProviderIDWithNodeNameForFargate(t *testing.T) {
assert.True(t, instanceExist)
}

func TestInstanceExistsByProviderIDForInstanceNotFound(t *testing.T) {
mockedEC2API := newMockedEC2API()
c := &Cloud{ec2: &awsSdkEC2{ec2: mockedEC2API}}

mockedEC2API.On("DescribeInstances", mock.Anything).Return(&ec2.DescribeInstancesOutput{}, awserr.New("InvalidInstanceID.NotFound", "Instance not found", nil))

instanceExists, err := c.InstanceExistsByProviderID(context.TODO(), "aws:///us-west-2c/1abc-2def/i-not-found")
assert.Nil(t, err)
assert.False(t, instanceExists)
}

func TestInstanceNotExistsByProviderIDForFargate(t *testing.T) {
awsServices := newMockedFakeAWSServices(TestClusterID)
c, _ := newAWSCloud(CloudConfig{}, awsServices)
Expand Down

0 comments on commit f18f5e5

Please sign in to comment.