Skip to content

Commit c23e955

Browse files
authored
Merge pull request #4808 from AndiDog/eks-tests
🌱 Add basic unit test for EKS `AWSManagedControlPlaneReconciler`
2 parents 4812465 + 4f35b73 commit c23e955

13 files changed

+1379
-23
lines changed

controlplane/eks/controllers/awsmanagedcontrolplane_controller.go

+77-7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2"
4141
"sigs.k8s.io/cluster-api-provider-aws/v2/feature"
4242
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
43+
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services"
4344
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services/awsnode"
4445
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services/ec2"
4546
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services/eks"
@@ -87,6 +88,14 @@ type AWSManagedControlPlaneReconciler struct {
8788
Recorder record.EventRecorder
8889
Endpoints []scope.ServiceEndpoint
8990

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+
9099
EnableIAM bool
91100
AllowAdditionalRoles bool
92101
WatchFilterValue string
@@ -96,6 +105,62 @@ type AWSManagedControlPlaneReconciler struct {
96105
TagUnmanagedNetworkResources bool
97106
}
98107

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+
99164
// SetupWithManager is used to setup the controller.
100165
func (r *AWSManagedControlPlaneReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
101166
log := logger.FromContext(ctx)
@@ -238,6 +303,11 @@ func (r *AWSManagedControlPlaneReconciler) Reconcile(ctx context.Context, req ct
238303
func (r *AWSManagedControlPlaneReconciler) reconcileNormal(ctx context.Context, managedScope *scope.ManagedControlPlaneScope) (res ctrl.Result, reterr error) {
239304
managedScope.Info("Reconciling AWSManagedControlPlane")
240305

306+
if managedScope.Cluster.Spec.InfrastructureRef == nil {
307+
managedScope.Info("InfrastructureRef not set, skipping reconciliation")
308+
return ctrl.Result{}, nil
309+
}
310+
241311
// TODO (richardcase): we can remove the if check here in the future when we have
242312
// allowed enough time for users to move away from using the single kind for
243313
// infrastructureRef and controlplaneRef.
@@ -257,13 +327,13 @@ func (r *AWSManagedControlPlaneReconciler) reconcileNormal(ctx context.Context,
257327
}
258328
}
259329

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)
267337

268338
if err := networkSvc.ReconcileNetwork(); err != nil {
269339
return reconcile.Result{}, fmt.Errorf("failed to reconcile network for AWSManagedControlPlane %s/%s: %w", awsManagedControlPlane.Namespace, awsManagedControlPlane.Name, err)

0 commit comments

Comments
 (0)