Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AzureMachineReconciler to inject AzuremachineService as dependency #1053

Merged
merged 1 commit into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions controllers/azuremachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,26 @@ import (
// AzureMachineReconciler reconciles a AzureMachine object
type AzureMachineReconciler struct {
client.Client
Log logr.Logger
Recorder record.EventRecorder
ReconcileTimeout time.Duration
Log logr.Logger
Recorder record.EventRecorder
ReconcileTimeout time.Duration
createAzureMachineService azureMachineServiceCreator
}

type azureMachineServiceCreator func(machineScope *scope.MachineScope, clusterScope *scope.ClusterScope) *azureMachineService

// NewAzureMachineReconciler returns a new AzureMachineReconciler instance
func NewAzureMachineReconciler(client client.Client, log logr.Logger, recorder record.EventRecorder, reconcileTimeout time.Duration) *AzureMachineReconciler {
amr := &AzureMachineReconciler{
Client: client,
Log: log,
Recorder: recorder,
ReconcileTimeout: reconcileTimeout,
}

amr.createAzureMachineService = newAzureMachineService

return amr
}

// SetupWithManager initializes this controller with a manager.
Expand Down Expand Up @@ -263,7 +280,7 @@ func (r *AzureMachineReconciler) reconcileNormal(ctx context.Context, machineSco
}
}

ams := newAzureMachineService(machineScope, clusterScope)
ams := r.createAzureMachineService(machineScope, clusterScope)

err := ams.Reconcile(ctx)
if err != nil {
Expand Down Expand Up @@ -337,7 +354,7 @@ func (r *AzureMachineReconciler) reconcileDelete(ctx context.Context, machineSco

if ShouldDeleteIndividualResources(ctx, clusterScope) {
machineScope.Info("Deleting AzureMachine")
if err := newAzureMachineService(machineScope, clusterScope).Delete(ctx); err != nil {
if err := r.createAzureMachineService(machineScope, clusterScope).Delete(ctx); err != nil {
r.Recorder.Eventf(machineScope.AzureMachine, corev1.EventTypeWarning, "Error deleting AzureMachine", errors.Wrapf(err, "error deleting AzureMachine %s/%s", clusterScope.Namespace(), clusterScope.ClusterName()).Error())
conditions.MarkFalse(machineScope.AzureMachine, infrav1.VMRunningCondition, clusterv1.DeletionFailedReason, clusterv1.ConditionSeverityWarning, err.Error())
return reconcile.Result{}, errors.Wrapf(err, "error deleting AzureMachine %s/%s", clusterScope.Namespace(), clusterScope.ClusterName())
Expand Down
13 changes: 5 additions & 8 deletions controllers/azuremachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
"k8s.io/klog/klogr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -35,6 +36,7 @@ import (
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
"sigs.k8s.io/cluster-api-provider-azure/cloud/scope"
"sigs.k8s.io/cluster-api-provider-azure/internal/test"
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
)

var _ = Describe("AzureMachineReconciler", func() {
Expand All @@ -43,10 +45,7 @@ var _ = Describe("AzureMachineReconciler", func() {

Context("Reconcile an AzureMachine", func() {
It("should not error with minimal set up", func() {
reconciler := &AzureMachineReconciler{
Client: testEnv,
Log: testEnv.Log,
}
reconciler := NewAzureMachineReconciler(testEnv, testEnv.Log, testEnv.GetEventRecorderFor("azuremachine-reconciler"), reconciler.DefaultLoopTimeout)

By("Calling reconcile")
name := test.RandomName("foo", 10)
Expand Down Expand Up @@ -160,11 +159,9 @@ func TestConditions(t *testing.T) {
tc.azureMachine,
}
client := fake.NewFakeClientWithScheme(scheme, initObjects...)
recorder := record.NewFakeRecorder(10)

reconciler := &AzureMachineReconciler{
Client: client,
Log: klogr.New(),
}
reconciler := NewAzureMachineReconciler(client, klogr.New(), recorder, reconciler.DefaultLoopTimeout)

clusterScope, err := scope.NewClusterScope(scope.ClusterScopeParams{
AzureClients: scope.AzureClients{
Expand Down
2 changes: 2 additions & 0 deletions controllers/azuremachine_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type azureMachineService struct {
skuCache *resourceskus.Cache
}

var _ azure.Service = (*azureMachineService)(nil)

// newAzureMachineService populates all the services based on input scope.
func newAzureMachineService(machineScope *scope.MachineScope, clusterScope *scope.ClusterScope) *azureMachineService {
cache := resourceskus.NewCache(clusterScope, clusterScope.Location())
Expand Down
8 changes: 3 additions & 5 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"

"sigs.k8s.io/cluster-api-provider-azure/internal/test/env"
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
// +kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -55,11 +56,8 @@ var _ = BeforeSuite(func(done Done) {
Recorder: testEnv.GetEventRecorderFor("azurecluster-reconciler"),
}).SetupWithManager(testEnv.Manager, controller.Options{MaxConcurrentReconciles: 1})).To(Succeed())

Expect((&AzureMachineReconciler{
Client: testEnv,
Log: testEnv.Log,
Recorder: testEnv.GetEventRecorderFor("azuremachine-reconciler"),
}).SetupWithManager(testEnv.Manager, controller.Options{MaxConcurrentReconciles: 1})).To(Succeed())
Expect(NewAzureMachineReconciler(testEnv, testEnv.Log, testEnv.GetEventRecorderFor("azuremachine-reconciler"), reconciler.DefaultLoopTimeout).
SetupWithManager(testEnv.Manager, controller.Options{MaxConcurrentReconciles: 1})).To(Succeed())

// +kubebuilder:scaffold:scheme

Expand Down
8 changes: 2 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,8 @@ func main() {
record.InitFromRecorder(mgr.GetEventRecorderFor("azure-controller"))

if webhookPort == 0 {
if err = (&controllers.AzureMachineReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("AzureMachine"),
Recorder: mgr.GetEventRecorderFor("azuremachine-reconciler"),
ReconcileTimeout: reconcileTimeout,
}).SetupWithManager(mgr, controller.Options{MaxConcurrentReconciles: azureMachineConcurrency}); err != nil {
if err = controllers.NewAzureMachineReconciler(mgr.GetClient(), ctrl.Log.WithName("controllers").WithName("AzureMachine"),
mgr.GetEventRecorderFor("azuremachine-reconciler"), reconcileTimeout).SetupWithManager(mgr, controller.Options{MaxConcurrentReconciles: azureMachineConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AzureMachine")
os.Exit(1)
}
Expand Down