Skip to content

Commit 8da76c2

Browse files
authored
Merge pull request #3081 from Ankitasw/awsmachine-controller-coverage
Increase unit test coverage for AWSMachine controller
2 parents 369f376 + 2d7a57b commit 8da76c2

15 files changed

+1177
-156
lines changed

controllers/awscluster_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ var (
7070
type AWSClusterReconciler struct {
7171
client.Client
7272
Recorder record.EventRecorder
73-
ec2ServiceFactory func(scope.EC2Scope) services.EC2MachineInterface
73+
ec2ServiceFactory func(scope.EC2Scope) services.EC2Interface
7474
networkServiceFactory func(scope.ClusterScope) services.NetworkInterface
7575
elbServiceFactory func(scope.ELBScope) services.ELBInterface
7676
securityGroupFactory func(scope.ClusterScope) services.SecurityGroupInterface
@@ -79,7 +79,7 @@ type AWSClusterReconciler struct {
7979
}
8080

8181
// getEC2Service factory func is added for testing purpose so that we can inject mocked EC2Service to the AWSClusterReconciler.
82-
func (r *AWSClusterReconciler) getEC2Service(scope scope.EC2Scope) services.EC2MachineInterface {
82+
func (r *AWSClusterReconciler) getEC2Service(scope scope.EC2Scope) services.EC2Interface {
8383
if r.ec2ServiceFactory != nil {
8484
return r.ec2ServiceFactory(scope)
8585
}

controllers/awscluster_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func TestAWSClusterReconciler_IntegrationTests(t *testing.T) {
117117

118118
ec2Svc := ec2Service.NewService(cs)
119119
ec2Svc.EC2Client = ec2Mock
120-
reconciler.ec2ServiceFactory = func(scope scope.EC2Scope) services.EC2MachineInterface {
120+
reconciler.ec2ServiceFactory = func(scope scope.EC2Scope) services.EC2Interface {
121121
return ec2Svc
122122
}
123123
testSecurityGroupRoles := []infrav1.SecurityGroupRole{
@@ -259,7 +259,7 @@ func TestAWSClusterReconciler_IntegrationTests(t *testing.T) {
259259

260260
ec2Svc := ec2Service.NewService(cs)
261261
ec2Svc.EC2Client = ec2Mock
262-
reconciler.ec2ServiceFactory = func(ec2Scope scope.EC2Scope) services.EC2MachineInterface {
262+
reconciler.ec2ServiceFactory = func(ec2Scope scope.EC2Scope) services.EC2Interface {
263263
return ec2Svc
264264
}
265265

controllers/awscluster_controller_unit_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
133133
var (
134134
reconciler AWSClusterReconciler
135135
mockCtrl *gomock.Controller
136-
ec2Svc *mock_services.MockEC2MachineInterface
136+
ec2Svc *mock_services.MockEC2Interface
137137
elbSvc *mock_services.MockELBInterface
138138
networkSvc *mock_services.MockNetworkInterface
139139
sgSvc *mock_services.MockSecurityGroupInterface
@@ -156,7 +156,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
156156
csClient := fake.NewClientBuilder().WithObjects(awsCluster, secret).Build()
157157

158158
mockCtrl = gomock.NewController(t)
159-
ec2Svc = mock_services.NewMockEC2MachineInterface(mockCtrl)
159+
ec2Svc = mock_services.NewMockEC2Interface(mockCtrl)
160160
elbSvc = mock_services.NewMockELBInterface(mockCtrl)
161161
networkSvc = mock_services.NewMockNetworkInterface(mockCtrl)
162162
sgSvc = mock_services.NewMockSecurityGroupInterface(mockCtrl)
@@ -165,7 +165,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
165165

166166
reconciler = AWSClusterReconciler{
167167
Client: csClient,
168-
ec2ServiceFactory: func(scope.EC2Scope) services.EC2MachineInterface {
168+
ec2ServiceFactory: func(scope.EC2Scope) services.EC2Interface {
169169
return ec2Svc
170170
},
171171
elbServiceFactory: func(elbScope scope.ELBScope) services.ELBInterface {

controllers/awsmachine_controller.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ type AWSMachineReconciler struct {
6666
client.Client
6767
Log logr.Logger
6868
Recorder record.EventRecorder
69-
ec2ServiceFactory func(scope.EC2Scope) services.EC2MachineInterface
69+
ec2ServiceFactory func(scope.EC2Scope) services.EC2Interface
70+
elbServiceFactory func(scope.ELBScope) services.ELBInterface
7071
secretsManagerServiceFactory func(cloud.ClusterScoper) services.SecretInterface
7172
SSMServiceFactory func(cloud.ClusterScoper) services.SecretInterface
7273
Endpoints []scope.ServiceEndpoint
@@ -78,7 +79,7 @@ const (
7879
AWSManagedControlPlaneRefKind = "AWSManagedControlPlane"
7980
)
8081

81-
func (r *AWSMachineReconciler) getEC2Service(scope scope.EC2Scope) services.EC2MachineInterface {
82+
func (r *AWSMachineReconciler) getEC2Service(scope scope.EC2Scope) services.EC2Interface {
8283
if r.ec2ServiceFactory != nil {
8384
return r.ec2ServiceFactory(scope)
8485
}
@@ -111,6 +112,14 @@ func (r *AWSMachineReconciler) getSecretService(machineScope *scope.MachineScope
111112
return nil, errors.New("invalid secret backend")
112113
}
113114

115+
func (r *AWSMachineReconciler) getELBService(scope scope.ELBScope) services.ELBInterface {
116+
if r.elbServiceFactory != nil {
117+
return r.elbServiceFactory(scope)
118+
}
119+
120+
return elb.NewService(scope)
121+
}
122+
114123
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=awsmachines,verbs=get;list;watch;create;update;patch;delete
115124
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=awsmachines/status,verbs=get;update;patch
116125
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machines;machines/status,verbs=get;list;watch
@@ -382,7 +391,7 @@ func (r *AWSMachineReconciler) reconcileDelete(machineScope *scope.MachineScope,
382391
// findInstance queries the EC2 apis and retrieves the instance if it exists.
383392
// If providerID is empty, finds instance by tags and if it cannot be found, returns empty instance with nil error.
384393
// If providerID is set, either finds the instance by ID or returns error.
385-
func (r *AWSMachineReconciler) findInstance(scope *scope.MachineScope, ec2svc services.EC2MachineInterface) (*infrav1.Instance, error) {
394+
func (r *AWSMachineReconciler) findInstance(scope *scope.MachineScope, ec2svc services.EC2Interface) (*infrav1.Instance, error) {
386395
var instance *infrav1.Instance
387396

388397
// Parse the ProviderID.
@@ -525,7 +534,7 @@ func (r *AWSMachineReconciler) reconcileNormal(_ context.Context, machineScope *
525534
}
526535

527536
// reconcile the deletion of the bootstrap data secret now that we have updated instance state
528-
if deleteSecretErr := r.deleteEncryptedBootstrapDataSecret(machineScope, clusterScope); err != nil {
537+
if deleteSecretErr := r.deleteEncryptedBootstrapDataSecret(machineScope, clusterScope); deleteSecretErr != nil {
529538
r.Log.Error(deleteSecretErr, "unable to delete secrets")
530539
return ctrl.Result{}, deleteSecretErr
531540
}
@@ -613,7 +622,7 @@ func (r *AWSMachineReconciler) deleteEncryptedBootstrapDataSecret(machineScope *
613622
return nil
614623
}
615624

616-
func (r *AWSMachineReconciler) createInstance(ec2svc services.EC2MachineInterface, machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper) (*infrav1.Instance, error) {
625+
func (r *AWSMachineReconciler) createInstance(ec2svc services.EC2Interface, machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper) (*infrav1.Instance, error) {
617626
machineScope.Info("Creating EC2 instance")
618627

619628
userData, userDataErr := r.resolveUserData(machineScope, clusterScope)
@@ -678,7 +687,7 @@ func (r *AWSMachineReconciler) reconcileLBAttachment(machineScope *scope.Machine
678687
return nil
679688
}
680689

681-
elbsvc := elb.NewService(clusterScope)
690+
elbsvc := r.getELBService(clusterScope)
682691

683692
// In order to prevent sending request to a "not-ready" control plane machines, it is required to remove the machine
684693
// from the ELB as soon as the machine gets deleted or when the machine is in a not running state.
@@ -877,7 +886,7 @@ func (r *AWSMachineReconciler) indexAWSMachineByInstanceID(o client.Object) []st
877886
return nil
878887
}
879888

880-
func (r *AWSMachineReconciler) ensureStorageTags(ec2svc services.EC2MachineInterface, instance *infrav1.Instance, machine *infrav1.AWSMachine) {
889+
func (r *AWSMachineReconciler) ensureStorageTags(ec2svc services.EC2Interface, instance *infrav1.Instance, machine *infrav1.AWSMachine) {
881890
annotations, err := r.machineAnnotationJSON(machine, VolumeTagsLastAppliedAnnotation)
882891
if err != nil {
883892
r.Log.Error(err, "Failed to fetch the annotations for volume tags")

0 commit comments

Comments
 (0)