@@ -41,6 +41,7 @@ import (
41
41
infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1"
42
42
"sigs.k8s.io/cluster-api-provider-aws/feature"
43
43
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/scope"
44
+ "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/services"
44
45
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/services/ec2"
45
46
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/services/elb"
46
47
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/services/instancestate"
68
69
// AWSClusterReconciler reconciles a AwsCluster object.
69
70
type AWSClusterReconciler struct {
70
71
client.Client
71
- Recorder record.EventRecorder
72
- Endpoints []scope.ServiceEndpoint
73
- WatchFilterValue string
72
+ Recorder record.EventRecorder
73
+ ec2ServiceFactory func (scope.EC2Scope ) services.EC2MachineInterface
74
+ networkServiceFactory func (scope.ClusterScope ) services.NetworkInterface
75
+ elbServiceFactory func (scope.ELBScope ) services.ELBInterface
76
+ securityGroupFactory func (scope.ClusterScope ) services.SecurityGroupInterface
77
+ Endpoints []scope.ServiceEndpoint
78
+ WatchFilterValue string
79
+ }
80
+
81
+ // 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 {
83
+ if r .ec2ServiceFactory != nil {
84
+ return r .ec2ServiceFactory (scope )
85
+ }
86
+ return ec2 .NewService (scope )
87
+ }
88
+
89
+ // getELBService factory func is added for testing purpose so that we can inject mocked ELBService to the AWSClusterReconciler.
90
+ func (r * AWSClusterReconciler ) getELBService (scope scope.ELBScope ) services.ELBInterface {
91
+ if r .elbServiceFactory != nil {
92
+ return r .elbServiceFactory (scope )
93
+ }
94
+ return elb .NewService (scope )
95
+ }
96
+
97
+ // getNetworkService factory func is added for testing purpose so that we can inject mocked NetworkService to the AWSClusterReconciler.
98
+ func (r * AWSClusterReconciler ) getNetworkService (scope scope.ClusterScope ) services.NetworkInterface {
99
+ if r .networkServiceFactory != nil {
100
+ return r .networkServiceFactory (scope )
101
+ }
102
+ return network .NewService (& scope )
103
+ }
104
+
105
+ // getSecurityGroupService factory func is added for testing purpose so that we can inject mocked SecurityGroupService to the AWSClusterReconciler.
106
+ func (r * AWSClusterReconciler ) getSecurityGroupService (scope scope.ClusterScope ) services.SecurityGroupInterface {
107
+ if r .securityGroupFactory != nil {
108
+ return r .securityGroupFactory (scope )
109
+ }
110
+ return securitygroup .NewService (& scope , awsSecurityGroupRoles )
74
111
}
75
112
76
113
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=awsclusters,verbs=get;list;watch;create;update;patch;delete
@@ -159,10 +196,10 @@ func (r *AWSClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
159
196
func (r * AWSClusterReconciler ) reconcileDelete (clusterScope * scope.ClusterScope ) (reconcile.Result , error ) {
160
197
clusterScope .Info ("Reconciling AWSCluster delete" )
161
198
162
- ec2svc := ec2 . NewService (clusterScope )
163
- elbsvc := elb . NewService (clusterScope )
164
- networkSvc := network . NewService ( clusterScope )
165
- sgService := securitygroup . NewService ( clusterScope , awsSecurityGroupRoles )
199
+ ec2svc := r . getEC2Service (clusterScope )
200
+ elbsvc := r . getELBService (clusterScope )
201
+ networkSvc := r . getNetworkService ( * clusterScope )
202
+ sgService := r . getSecurityGroupService ( * clusterScope )
166
203
167
204
if feature .Gates .Enabled (feature .EventBridgeInstanceState ) {
168
205
instancestateSvc := instancestate .NewService (clusterScope )
@@ -210,10 +247,10 @@ func (r *AWSClusterReconciler) reconcileNormal(clusterScope *scope.ClusterScope)
210
247
return reconcile.Result {}, err
211
248
}
212
249
213
- ec2Service := ec2 . NewService (clusterScope )
214
- elbService := elb . NewService (clusterScope )
215
- networkSvc := network . NewService ( clusterScope )
216
- sgService := securitygroup . NewService ( clusterScope , awsSecurityGroupRoles )
250
+ ec2Service := r . getEC2Service (clusterScope )
251
+ elbService := r . getELBService (clusterScope )
252
+ networkSvc := r . getNetworkService ( * clusterScope )
253
+ sgService := r . getSecurityGroupService ( * clusterScope )
217
254
218
255
if err := networkSvc .ReconcileNetwork (); err != nil {
219
256
clusterScope .Error (err , "failed to reconcile network" )
0 commit comments