From 2e84891d7f7b45bd3ef91357ec1cc13605ab86d3 Mon Sep 17 00:00:00 2001 From: Alvaro Aleman Date: Fri, 1 Mar 2019 22:31:39 +0100 Subject: [PATCH] MachineSet controller: Expose errors (#798) * MachineSet controller: Expose errors * Feedback from md pull * s/controler/controller --- pkg/controller/machineset/BUILD.bazel | 1 + pkg/controller/machineset/controller.go | 24 ++++++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/pkg/controller/machineset/BUILD.bazel b/pkg/controller/machineset/BUILD.bazel index ea0b6ee38b77..61918f353248 100644 --- a/pkg/controller/machineset/BUILD.bazel +++ b/pkg/controller/machineset/BUILD.bazel @@ -20,6 +20,7 @@ go_library( "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/labels:go_default_library", "//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library", + "//vendor/k8s.io/client-go/tools/record:go_default_library", "//vendor/k8s.io/klog:go_default_library", "//vendor/sigs.k8s.io/controller-runtime/pkg/client:go_default_library", "//vendor/sigs.k8s.io/controller-runtime/pkg/controller:go_default_library", diff --git a/pkg/controller/machineset/controller.go b/pkg/controller/machineset/controller.go index 3dfbede7f930..96bcd57063e4 100644 --- a/pkg/controller/machineset/controller.go +++ b/pkg/controller/machineset/controller.go @@ -24,10 +24,12 @@ import ( "time" "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/client-go/tools/record" "k8s.io/klog" clusterv1alpha1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1" "sigs.k8s.io/cluster-api/pkg/util" @@ -48,6 +50,9 @@ var ( // stateConfirmationInterval is the amount of time between polling for the desired state. // The polling is against a local memory cache. stateConfirmationInterval = 100 * time.Millisecond + + // controllerName is the name of this controller + controllerName = "machineset-controller" ) // Add creates a new MachineSet Controller and adds it to the Manager with default RBAC. @@ -59,13 +64,13 @@ func Add(mgr manager.Manager) error { // newReconciler returns a new reconcile.Reconciler. func newReconciler(mgr manager.Manager) *ReconcileMachineSet { - return &ReconcileMachineSet{Client: mgr.GetClient(), scheme: mgr.GetScheme()} + return &ReconcileMachineSet{Client: mgr.GetClient(), scheme: mgr.GetScheme(), recorder: mgr.GetRecorder(controllerName)} } // 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("machineset-controller", mgr, controller.Options{Reconciler: r}) + c, err := controller.New(controllerName, mgr, controller.Options{Reconciler: r}) if err != nil { return err } @@ -98,7 +103,8 @@ func add(mgr manager.Manager, r reconcile.Reconciler, mapFn handler.ToRequestsFu // ReconcileMachineSet reconciles a MachineSet object type ReconcileMachineSet struct { client.Client - scheme *runtime.Scheme + scheme *runtime.Scheme + recorder record.EventRecorder } func (r *ReconcileMachineSet) MachineToMachineSets(o handler.MapObject) []reconcile.Request { @@ -138,8 +144,9 @@ func (r *ReconcileMachineSet) MachineToMachineSets(o handler.MapObject) []reconc // +kubebuilder:rbac:groups=cluster.k8s.io,resources=machines,verbs=get;list;watch;create;update;patch;delete func (r *ReconcileMachineSet) Reconcile(request reconcile.Request) (reconcile.Result, error) { // Fetch the MachineSet instance + ctx := context.TODO() machineSet := &clusterv1alpha1.MachineSet{} - if err := r.Get(context.TODO(), request.NamespacedName, machineSet); err != nil { + if err := r.Get(ctx, request.NamespacedName, machineSet); err != nil { if apierrors.IsNotFound(err) { // Object not found, return. Created objects are automatically garbage collected. // For additional cleanup logic use finalizers. @@ -149,6 +156,15 @@ func (r *ReconcileMachineSet) Reconcile(request reconcile.Request) (reconcile.Re return reconcile.Result{}, err } + result, err := r.reconcile(ctx, machineSet) + if err != nil { + klog.Errorf("Failed to reconcile MachineSet %q: %v", request.NamespacedName, err) + r.recorder.Eventf(machineSet, corev1.EventTypeWarning, "ReconcileError", "%v", err) + } + return result, err +} + +func (r *ReconcileMachineSet) reconcile(ctx context.Context, machineSet *clusterv1alpha1.MachineSet) (reconcile.Result, error) { klog.V(4).Infof("Reconcile machineset %v", machineSet.Name) allMachines := &clusterv1alpha1.MachineList{}