Skip to content

Commit

Permalink
Merge pull request #3073 from Ankitasw/awscluster-controller-coverage
Browse files Browse the repository at this point in the history
Increase unit test coverage for AWSCluster controller
  • Loading branch information
k8s-ci-robot authored Feb 11, 2022
2 parents d4e16e4 + d0d508b commit 8f04b85
Show file tree
Hide file tree
Showing 10 changed files with 1,012 additions and 12 deletions.
59 changes: 48 additions & 11 deletions controllers/awscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-aws/feature"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/scope"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/services"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/services/ec2"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/services/elb"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/services/instancestate"
Expand Down Expand Up @@ -68,9 +69,45 @@ var (
// AWSClusterReconciler reconciles a AwsCluster object.
type AWSClusterReconciler struct {
client.Client
Recorder record.EventRecorder
Endpoints []scope.ServiceEndpoint
WatchFilterValue string
Recorder record.EventRecorder
ec2ServiceFactory func(scope.EC2Scope) services.EC2MachineInterface
networkServiceFactory func(scope.ClusterScope) services.NetworkInterface
elbServiceFactory func(scope.ELBScope) services.ELBInterface
securityGroupFactory func(scope.ClusterScope) services.SecurityGroupInterface
Endpoints []scope.ServiceEndpoint
WatchFilterValue string
}

// getEC2Service factory func is added for testing purpose so that we can inject mocked EC2Service to the AWSClusterReconciler.
func (r *AWSClusterReconciler) getEC2Service(scope scope.EC2Scope) services.EC2MachineInterface {
if r.ec2ServiceFactory != nil {
return r.ec2ServiceFactory(scope)
}
return ec2.NewService(scope)
}

// getELBService factory func is added for testing purpose so that we can inject mocked ELBService to the AWSClusterReconciler.
func (r *AWSClusterReconciler) getELBService(scope scope.ELBScope) services.ELBInterface {
if r.elbServiceFactory != nil {
return r.elbServiceFactory(scope)
}
return elb.NewService(scope)
}

// getNetworkService factory func is added for testing purpose so that we can inject mocked NetworkService to the AWSClusterReconciler.
func (r *AWSClusterReconciler) getNetworkService(scope scope.ClusterScope) services.NetworkInterface {
if r.networkServiceFactory != nil {
return r.networkServiceFactory(scope)
}
return network.NewService(&scope)
}

// getSecurityGroupService factory func is added for testing purpose so that we can inject mocked SecurityGroupService to the AWSClusterReconciler.
func (r *AWSClusterReconciler) getSecurityGroupService(scope scope.ClusterScope) services.SecurityGroupInterface {
if r.securityGroupFactory != nil {
return r.securityGroupFactory(scope)
}
return securitygroup.NewService(&scope, awsSecurityGroupRoles)
}

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=awsclusters,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -159,10 +196,10 @@ func (r *AWSClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
func (r *AWSClusterReconciler) reconcileDelete(clusterScope *scope.ClusterScope) (reconcile.Result, error) {
clusterScope.Info("Reconciling AWSCluster delete")

ec2svc := ec2.NewService(clusterScope)
elbsvc := elb.NewService(clusterScope)
networkSvc := network.NewService(clusterScope)
sgService := securitygroup.NewService(clusterScope, awsSecurityGroupRoles)
ec2svc := r.getEC2Service(clusterScope)
elbsvc := r.getELBService(clusterScope)
networkSvc := r.getNetworkService(*clusterScope)
sgService := r.getSecurityGroupService(*clusterScope)

if feature.Gates.Enabled(feature.EventBridgeInstanceState) {
instancestateSvc := instancestate.NewService(clusterScope)
Expand Down Expand Up @@ -210,10 +247,10 @@ func (r *AWSClusterReconciler) reconcileNormal(clusterScope *scope.ClusterScope)
return reconcile.Result{}, err
}

ec2Service := ec2.NewService(clusterScope)
elbService := elb.NewService(clusterScope)
networkSvc := network.NewService(clusterScope)
sgService := securitygroup.NewService(clusterScope, awsSecurityGroupRoles)
ec2Service := r.getEC2Service(clusterScope)
elbService := r.getELBService(clusterScope)
networkSvc := r.getNetworkService(*clusterScope)
sgService := r.getSecurityGroupService(*clusterScope)

if err := networkSvc.ReconcileNetwork(); err != nil {
clusterScope.Error(err, "failed to reconcile network")
Expand Down
1 change: 0 additions & 1 deletion controllers/awscluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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 controllers

import (
Expand Down
Loading

0 comments on commit 8f04b85

Please sign in to comment.