diff --git a/pkg/controller/machinedeployment/BUILD.bazel b/pkg/controller/machinedeployment/BUILD.bazel index 6901a9849cd5..44b7489926d8 100644 --- a/pkg/controller/machinedeployment/BUILD.bazel +++ b/pkg/controller/machinedeployment/BUILD.bazel @@ -13,7 +13,9 @@ go_library( "//pkg/apis/cluster/common:go_default_library", "//pkg/apis/cluster/v1alpha1:go_default_library", "//pkg/controller/machinedeployment/util:go_default_library", + "//pkg/util:go_default_library", "//vendor/github.com/pkg/errors:go_default_library", + "//vendor/k8s.io/api/core/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/api/equality:go_default_library", "//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", @@ -21,6 +23,7 @@ go_library( "//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library", "//vendor/k8s.io/apimachinery/pkg/types:go_default_library", "//vendor/k8s.io/apimachinery/pkg/util/rand:go_default_library", + "//vendor/k8s.io/client-go/tools/record:go_default_library", "//vendor/k8s.io/client-go/util/integer:go_default_library", "//vendor/k8s.io/client-go/util/retry:go_default_library", "//vendor/k8s.io/klog:go_default_library", diff --git a/pkg/controller/machinedeployment/controller.go b/pkg/controller/machinedeployment/controller.go index e49ee4c7cfe5..7c97791f0910 100644 --- a/pkg/controller/machinedeployment/controller.go +++ b/pkg/controller/machinedeployment/controller.go @@ -21,11 +21,13 @@ import ( "reflect" "github.com/pkg/errors" + corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" + "k8s.io/client-go/tools/record" "k8s.io/klog" "sigs.k8s.io/cluster-api/pkg/apis/cluster/common" "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1" @@ -43,15 +45,19 @@ var ( controllerKind = v1alpha1.SchemeGroupVersion.WithKind("MachineDeployment") ) +// controllerName is the name of this controller +const controllerName = "machinedeployment-controller" + // ReconcileMachineDeployment reconciles a MachineDeployment object. type ReconcileMachineDeployment struct { client.Client - scheme *runtime.Scheme + scheme *runtime.Scheme + recorder record.EventRecorder } // newReconciler returns a new reconcile.Reconciler. func newReconciler(mgr manager.Manager) *ReconcileMachineDeployment { - return &ReconcileMachineDeployment{Client: mgr.GetClient(), scheme: mgr.GetScheme()} + return &ReconcileMachineDeployment{Client: mgr.GetClient(), scheme: mgr.GetScheme(), recorder: mgr.GetRecorder(controllerName)} } // Add creates a new MachineDeployment Controller and adds it to the Manager with default RBAC. @@ -63,7 +69,7 @@ func Add(mgr manager.Manager) error { // add adds a new Controller to mgr with r as the reconcile.Reconciler. func add(mgr manager.Manager, r reconcile.Reconciler, mapFn handler.ToRequestsFunc) error { // Create a new controller. - c, err := controller.New("machinedeployment-controller", mgr, controller.Options{Reconciler: r}) + c, err := controller.New(controllerName, mgr, controller.Options{Reconciler: r}) if err != nil { return err } @@ -146,6 +152,7 @@ func (r *ReconcileMachineDeployment) getMachineSetsForDeployment(d *v1alpha1.Mac func (r *ReconcileMachineDeployment) Reconcile(request reconcile.Request) (reconcile.Result, error) { // Fetch the MachineDeployment instance d := &v1alpha1.MachineDeployment{} + ctx := context.TODO() if err := r.Get(context.TODO(), request.NamespacedName, d); err != nil { if apierrors.IsNotFound(err) { // Object not found, return. Created objects are automatically garbage collected. @@ -155,7 +162,16 @@ func (r *ReconcileMachineDeployment) Reconcile(request reconcile.Request) (recon // Error reading the object - requeue the request. return reconcile.Result{}, err } + result, err := r.reconcile(ctx, d) + if err != nil { + klog.Errorf("Failed to reconcile MachineDeployment %q: %v", request.NamespacedName, err) + r.recorder.Eventf(d, corev1.EventTypeWarning, "ReconcileError", "%v", err) + } + + return result, err +} +func (r *ReconcileMachineDeployment) reconcile(ctx context.Context, d *v1alpha1.MachineDeployment) (reconcile.Result, error) { v1alpha1.PopulateDefaultsMachineDeployment(d) everything := metav1.LabelSelector{}