Repository: kubernetes-sigs/cluster-api-provider-aws
Pull request #2972 (2021-11-18 00:27:34)

Add v1.1 release to metadata file
/kind bug

**What this PR does / why we need it**:

<!-- Enter a description of the change and why this change is needed -->

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #

**Special notes for your reviewer**:

**Checklist**:
<!-- Put an "X" character inside the brackets of each completed task. Some may be optional depending on the PR in which case these can be deleted -->

- [ ] squashed commits
- [ ] includes documentation
- [ ] adds unit tests
- [ ] adds or updates e2e tests

**Release note**:
<!--  Write your release note:
1. Enter your extended release note in the below block. 
2. If the PR requires additional action from users switching to the new release, include the string "action required".
2. If no release note is required, just write "NONE"....however we encourage contributors to never use this as release notes are incredible useful.
-->
```release-note
NONE
```



Patch:
diff --git a/controllers/awsmachine_controller.go b/controllers/awsmachine_controller.go
index 2324797cf..1fd51a1ae 100644
--- a/controllers/awsmachine_controller.go
+++ b/controllers/awsmachine_controller.go
@@ -323,8 +323,8 @@ func (r *AWSMachineReconciler) reconcileDelete(machineScope *scope.MachineScope,
 	}
 
 	instance, err := r.findInstance(machineScope, ec2Service)
-	if err != nil && err != ec2.ErrInstanceNotFoundByID {
-		machineScope.Error(err, "query to find instance failed")
+	if err != nil {
+		machineScope.Error(err, "unable to find instance")
 		return ctrl.Result{}, err
 	}
 
@@ -426,34 +426,29 @@ func (r *AWSMachineReconciler) reconcileDelete(machineScope *scope.MachineScope,
 	return ctrl.Result{}, nil
 }
 
-// findInstance queries the EC2 apis and retrieves the instance if it exists.
-// If providerID is empty, finds instance by tags and if it cannot be found, returns empty instance with nil error.
-// If providerID is set, either finds the instance by ID or returns error.
+// findInstance queries the EC2 apis and retrieves the instance if it exists, returns nil otherwise.
 func (r *AWSMachineReconciler) findInstance(scope *scope.MachineScope, ec2svc services.EC2MachineInterface) (*infrav1.Instance, error) {
-	var instance *infrav1.Instance
-
 	// Parse the ProviderID.
 	pid, err := noderefutil.NewProviderID(scope.GetProviderID())
-	if err != nil {
-		if !errors.Is(err, noderefutil.ErrEmptyProviderID) {
-			return nil, errors.Wrapf(err, "failed to parse Spec.ProviderID")
-		}
-		// If the ProviderID is empty, try to query the instance using tags.
-		// If an instance cannot be found, GetRunningInstanceByTags returns empty instance with nil error.
-		instance, err = ec2svc.GetRunningInstanceByTags(scope)
-		if err != nil {
-			return nil, errors.Wrapf(err, "failed to query AWSMachine instance by tags")
-		}
-	} else {
-		// If the ProviderID is populated, describe the instance using the ID.
-		// InstanceIfExists() returns error (ErrInstanceNotFoundByID or ErrDescribeInstance) if the instance could not be found.
-		instance, err = ec2svc.InstanceIfExists(pointer.StringPtr(pid.ID()))
+	if err != nil && !errors.Is(err, noderefutil.ErrEmptyProviderID) {
+		return nil, errors.Wrapf(err, "failed to parse Spec.ProviderID")
+	}
+
+	// If the ProviderID is populated, describe the instance using the ID.
+	if err == nil {
+		instance, err := ec2svc.InstanceIfExists(pointer.StringPtr(pid.ID()))
 		if err != nil {
-			return nil, err
+			return nil, errors.Wrapf(err, "failed to query AWSMachine instance")
 		}
+		return instance, nil
+	}
+
+	// If the ProviderID is empty, try to query the instance using tags.
+	instance, err := ec2svc.GetRunningInstanceByTags(scope)
+	if err != nil {
+		return nil, errors.Wrapf(err, "failed to query AWSMachine instance by tags")
 	}
 
-	// The only case where the instance is nil here is when the providerId is empty and instance could not be found by tags.
 	return instance, nil
 }
 
@@ -506,7 +501,7 @@ func (r *AWSMachineReconciler) reconcileNormal(_ context.Context, machineScope *
 		return ctrl.Result{}, err
 	}
 
-	// Create new instance since providerId is nil and instance could not be found by tags.
+	// Create new instance
 	if instance == nil {
 		// Avoid a flickering condition between InstanceProvisionStarted and InstanceProvisionFailed if there's a persistent failure with createInstance
 		if conditions.GetReason(machineScope.AWSMachine, infrav1.InstanceReadyCondition) != infrav1.InstanceProvisionFailedReason {
diff --git a/metadata.yaml b/metadata.yaml
index f49ec7d2c..c6a302edb 100644
--- a/metadata.yaml
+++ b/metadata.yaml
@@ -20,3 +20,6 @@ releaseSeries:
   - major: 1
     minor: 0
     contract: v1beta1
+  - major: 1
+    minor: 1
+    contract: v1beta1
diff --git a/pkg/cloud/services/ec2/errors.go b/pkg/cloud/services/ec2/errors.go
deleted file mode 100644
index 3d008e1c9..000000000
--- a/pkg/cloud/services/ec2/errors.go
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
-Copyright 2021 The Kubernetes Authors.
-
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
-    http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/
-
-package ec2
-
-import "errors"
-
-var (
-	// ErrInstanceNotFoundByID defines an error for when the instance with the provided provider ID is missing.
-	ErrInstanceNotFoundByID = errors.New("failed to find instance by id")
-
-	// ErrDescribeInstance defines an error for when AWS SDK returns error when describing instances.
-	ErrDescribeInstance = errors.New("failed to describe instance by id")
-)
diff --git a/pkg/cloud/services/ec2/instances.go b/pkg/cloud/services/ec2/instances.go
index 894adb485..7037e6475 100644
--- a/pkg/cloud/services/ec2/instances.go
+++ b/pkg/cloud/services/ec2/instances.go
@@ -75,8 +75,7 @@ func (s *Service) GetRunningInstanceByTags(scope *scope.MachineScope) (*infrav1.
 	return nil, nil
 }
 
-// InstanceIfExists returns the existing instance by id and errors if it cannot find the instance(ErrInstanceNotFoundByID) or API call fails (ErrDescribeInstance).
-// Returns empty instance with nil error, only when providerID is nil.
+// InstanceIfExists returns the existing instance or nothing if it doesn't exist.
 func (s *Service) InstanceIfExists(id *string) (*infrav1.Instance, error) {
 	if id == nil {
 		s.scope.Info("Instance does not have an instance id")
@@ -92,20 +91,17 @@ func (s *Service) InstanceIfExists(id *string) (*infrav1.Instance, error) {
 	out, err := s.EC2Client.DescribeInstances(input)
 	switch {
 	case awserrors.IsNotFound(err):
-		record.Eventf(s.scope.InfraCluster(), "FailedFindInstances", "failed to find instance by providerId %q: %v", *id, err)
-		return nil, ErrInstanceNotFoundByID
+		return nil, nil
 	case err != nil:
 		record.Eventf(s.scope.InfraCluster(), "FailedDescribeInstances", "failed to describe instance %q: %v", *id, err)
-		return nil, ErrDescribeInstance
+		return nil, errors.Wrapf(err, "failed to describe instance: %q", *id)
 	}
 
 	if len(out.Reservations) > 0 && len(out.Reservations[0].Instances) > 0 {
 		return s.SDKToInstance(out.Reservations[0].Instances[0])
-	} else {
-		// Failed to find instance with provider id.
-		record.Eventf(s.scope.InfraCluster(), "FailedFindInstances", "failed to find instance by providerId %q: %v", *id, err)
-		return nil, ErrInstanceNotFoundByID
 	}
+
+	return nil, nil
 }
 
 // CreateInstance runs an ec2 instance.


Test patch:
diff --git a/pkg/cloud/services/ec2/instances_test.go b/pkg/cloud/services/ec2/instances_test.go
index 934b692a1..99432542a 100644
--- a/pkg/cloud/services/ec2/instances_test.go
+++ b/pkg/cloud/services/ec2/instances_test.go
@@ -61,8 +61,8 @@ func TestInstanceIfExists(t *testing.T) {
 					Return(nil, awserrors.NewNotFound("not found"))
 			},
 			check: func(instance *infrav1.Instance, err error) {
-				if err == nil {
-					t.Fatalf("expects error when instance could not be found: %v", err)
+				if err != nil {
+					t.Fatalf("did not expect error: %v", err)
 				}
 
 				if instance != nil {
@@ -80,8 +80,8 @@ func TestInstanceIfExists(t *testing.T) {
 					Return(nil, awserr.New(awserrors.InvalidInstanceID, "does not exist", nil))
 			},
 			check: func(instance *infrav1.Instance, err error) {
-				if err == nil {
-					t.Fatalf("expects error when DescribeInstances returns error: %v", err)
+				if err != nil {
+					t.Fatalf("did not expect error: %v", err)
 				}
 
 				if instance != nil {