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

MachineSet controller: Expose errors #798

Merged
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
1 change: 1 addition & 0 deletions pkg/controller/machinedeployment/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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/apimachinery/pkg/api/equality:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
Expand Down
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