@@ -40,6 +40,7 @@ import (
40
40
expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2"
41
41
"sigs.k8s.io/cluster-api-provider-aws/v2/feature"
42
42
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
43
+ "sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services"
43
44
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services/awsnode"
44
45
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services/ec2"
45
46
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services/eks"
@@ -87,6 +88,14 @@ type AWSManagedControlPlaneReconciler struct {
87
88
Recorder record.EventRecorder
88
89
Endpoints []scope.ServiceEndpoint
89
90
91
+ awsNodeServiceFactory func (scope.AWSNodeScope ) services.AWSNodeInterface
92
+ ec2ServiceFactory func (scope.EC2Scope ) services.EC2Interface
93
+ eksServiceFactory func (* scope.ManagedControlPlaneScope ) * eks.Service
94
+ iamAuthenticatorServiceFactory func (scope.IAMAuthScope , iamauth.BackendType , client.Client ) services.IAMAuthenticatorInterface
95
+ kubeProxyServiceFactory func (scope.KubeProxyScope ) services.KubeProxyInterface
96
+ networkServiceFactory func (scope.NetworkScope ) services.NetworkInterface
97
+ securityGroupServiceFactory func (* scope.ManagedControlPlaneScope ) services.SecurityGroupInterface
98
+
90
99
EnableIAM bool
91
100
AllowAdditionalRoles bool
92
101
WatchFilterValue string
@@ -96,6 +105,62 @@ type AWSManagedControlPlaneReconciler struct {
96
105
TagUnmanagedNetworkResources bool
97
106
}
98
107
108
+ // getAWSNodeService factory func is added for testing purpose so that we can inject mocked AWSNodeInterface to the AWSManagedControlPlaneReconciler.
109
+ func (r * AWSManagedControlPlaneReconciler ) getAWSNodeService (scope scope.AWSNodeScope ) services.AWSNodeInterface {
110
+ if r .awsNodeServiceFactory != nil {
111
+ return r .awsNodeServiceFactory (scope )
112
+ }
113
+ return awsnode .NewService (scope )
114
+ }
115
+
116
+ // getEC2Service factory func is added for testing purpose so that we can inject mocked EC2Service to the AWSManagedControlPlaneReconciler.
117
+ func (r * AWSManagedControlPlaneReconciler ) getEC2Service (scope scope.EC2Scope ) services.EC2Interface {
118
+ if r .ec2ServiceFactory != nil {
119
+ return r .ec2ServiceFactory (scope )
120
+ }
121
+ return ec2 .NewService (scope )
122
+ }
123
+
124
+ // getEC2Service factory func is added for testing purpose so that we can inject mocked EC2Service to the AWSManagedControlPlaneReconciler.
125
+ func (r * AWSManagedControlPlaneReconciler ) getEKSService (scope * scope.ManagedControlPlaneScope ) * eks.Service {
126
+ if r .ec2ServiceFactory != nil {
127
+ return r .eksServiceFactory (scope )
128
+ }
129
+ return eks .NewService (scope )
130
+ }
131
+
132
+ // getIAMAuthenticatorService factory func is added for testing purpose so that we can inject mocked IAMAuthenticatorInterface to the AWSManagedControlPlaneReconciler.
133
+ func (r * AWSManagedControlPlaneReconciler ) getIAMAuthenticatorService (scope scope.IAMAuthScope , backend iamauth.BackendType , client client.Client ) services.IAMAuthenticatorInterface {
134
+ if r .iamAuthenticatorServiceFactory != nil {
135
+ return r .iamAuthenticatorServiceFactory (scope , backend , client )
136
+ }
137
+ return iamauth .NewService (scope , backend , client )
138
+ }
139
+
140
+ // getKubeProxyService factory func is added for testing purpose so that we can inject mocked KubeProxyInterface to the AWSManagedControlPlaneReconciler.
141
+ func (r * AWSManagedControlPlaneReconciler ) getKubeProxyService (scope scope.KubeProxyScope ) services.KubeProxyInterface {
142
+ if r .kubeProxyServiceFactory != nil {
143
+ return r .kubeProxyServiceFactory (scope )
144
+ }
145
+ return kubeproxy .NewService (scope )
146
+ }
147
+
148
+ // getNetworkService factory func is added for testing purpose so that we can inject mocked NetworkService to the AWSManagedControlPlaneReconciler.
149
+ func (r * AWSManagedControlPlaneReconciler ) getNetworkService (scope scope.NetworkScope ) services.NetworkInterface {
150
+ if r .networkServiceFactory != nil {
151
+ return r .networkServiceFactory (scope )
152
+ }
153
+ return network .NewService (scope )
154
+ }
155
+
156
+ // getSecurityGroupService factory func is added for testing purpose so that we can inject mocked SecurityGroupService to the AWSClusterReconciler.
157
+ func (r * AWSManagedControlPlaneReconciler ) getSecurityGroupService (scope * scope.ManagedControlPlaneScope ) services.SecurityGroupInterface {
158
+ if r .securityGroupServiceFactory != nil {
159
+ return r .securityGroupServiceFactory (scope )
160
+ }
161
+ return securitygroup .NewService (scope , securityGroupRolesForControlPlane (scope ))
162
+ }
163
+
99
164
// SetupWithManager is used to setup the controller.
100
165
func (r * AWSManagedControlPlaneReconciler ) SetupWithManager (ctx context.Context , mgr ctrl.Manager , options controller.Options ) error {
101
166
log := logger .FromContext (ctx )
@@ -238,6 +303,11 @@ func (r *AWSManagedControlPlaneReconciler) Reconcile(ctx context.Context, req ct
238
303
func (r * AWSManagedControlPlaneReconciler ) reconcileNormal (ctx context.Context , managedScope * scope.ManagedControlPlaneScope ) (res ctrl.Result , reterr error ) {
239
304
managedScope .Info ("Reconciling AWSManagedControlPlane" )
240
305
306
+ if managedScope .Cluster .Spec .InfrastructureRef == nil {
307
+ managedScope .Info ("InfrastructureRef not set, skipping reconciliation" )
308
+ return ctrl.Result {}, nil
309
+ }
310
+
241
311
// TODO (richardcase): we can remove the if check here in the future when we have
242
312
// allowed enough time for users to move away from using the single kind for
243
313
// infrastructureRef and controlplaneRef.
@@ -257,13 +327,13 @@ func (r *AWSManagedControlPlaneReconciler) reconcileNormal(ctx context.Context,
257
327
}
258
328
}
259
329
260
- ec2Service := ec2 . NewService (managedScope )
261
- networkSvc := network . NewService (managedScope )
262
- ekssvc := eks . NewService (managedScope )
263
- sgService := securitygroup . NewService (managedScope , securityGroupRolesForControlPlane ( managedScope ) )
264
- authService := iamauth . NewService (managedScope , iamauth .BackendTypeConfigMap , managedScope .Client )
265
- awsnodeService := awsnode . NewService (managedScope )
266
- kubeproxyService := kubeproxy . NewService (managedScope )
330
+ ec2Service := r . getEC2Service (managedScope )
331
+ networkSvc := r . getNetworkService (managedScope )
332
+ ekssvc := r . getEKSService (managedScope )
333
+ sgService := r . getSecurityGroupService (managedScope )
334
+ authService := r . getIAMAuthenticatorService (managedScope , iamauth .BackendTypeConfigMap , managedScope .Client )
335
+ awsnodeService := r . getAWSNodeService (managedScope )
336
+ kubeproxyService := r . getKubeProxyService (managedScope )
267
337
268
338
if err := networkSvc .ReconcileNetwork (); err != nil {
269
339
return reconcile.Result {}, fmt .Errorf ("failed to reconcile network for AWSManagedControlPlane %s/%s: %w" , awsManagedControlPlane .Namespace , awsManagedControlPlane .Name , err )
0 commit comments