Skip to content

Commit

Permalink
MachineSet controller: Expose errors (kubernetes-sigs#798)
Browse files Browse the repository at this point in the history
* MachineSet controller: Expose errors

* Feedback from md pull

* s/controler/controller
  • Loading branch information
alvaroaleman authored and k8s-ci-robot committed Mar 1, 2019
1 parent 163dc4c commit 2e84891
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/controller/machineset/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 20 additions & 4 deletions pkg/controller/machineset/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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{}

Expand Down

0 comments on commit 2e84891

Please sign in to comment.