Skip to content

Commit

Permalink
MachineDeployment controller: expose errors (kubernetes-sigs#797)
Browse files Browse the repository at this point in the history
* MachineDeployment controller: expose errors

* Update pkg/controller/machinedeployment/controller.go

Co-Authored-By: alvaroaleman <[email protected]>

* Update pkg/controller/machinedeployment/controller.go

Co-Authored-By: alvaroaleman <[email protected]>

* Update pkg/controller/machinedeployment/controller.go

Co-Authored-By: alvaroaleman <[email protected]>
  • Loading branch information
alvaroaleman authored and k8s-ci-robot committed Mar 1, 2019
1 parent b6e166e commit 163dc4c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pkg/controller/machinedeployment/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ 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",
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_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",
Expand Down
22 changes: 19 additions & 3 deletions pkg/controller/machinedeployment/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand All @@ -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
}
Expand Down Expand Up @@ -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.
Expand All @@ -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{}
Expand Down

0 comments on commit 163dc4c

Please sign in to comment.