Skip to content

Commit

Permalink
Merge pull request #487 from saurav-agarwalla/automated-cherry-pick-o…
Browse files Browse the repository at this point in the history
…f-#484-release-1.22

Automated cherry pick of #484: Handle wrapped error for InvalidInstanceID.NotFound in
  • Loading branch information
k8s-ci-robot authored Sep 1, 2022
2 parents 5934e50 + 4f98337 commit c797128
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 @@ -2139,6 +2139,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 @@ -3766,6 +3766,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 c797128

Please sign in to comment.