From 8591410f5ea1e82e941550785db457b7bb056ede Mon Sep 17 00:00:00 2001 From: Jason DeTiberus Date: Fri, 14 Jun 2019 16:08:22 -0400 Subject: [PATCH] Add events to MachineDeployment operations (#1014) (cherry picked from commit a5e5fdb36d5e3ce77e03a1cd5ad6af3728976c82) --- .../machinedeployment_controller.go | 2 ++ .../machinedeployment_controller_test.go | 16 ++++++++++------ pkg/controller/machinedeployment/sync.go | 10 +++++++++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/pkg/controller/machinedeployment/machinedeployment_controller.go b/pkg/controller/machinedeployment/machinedeployment_controller.go index 529ea489e128..46b2f1747f54 100644 --- a/pkg/controller/machinedeployment/machinedeployment_controller.go +++ b/pkg/controller/machinedeployment/machinedeployment_controller.go @@ -276,9 +276,11 @@ func (r *ReconcileMachineDeployment) getMachineSetsForDeployment(d *v1alpha1.Mac // Attempt to adopt machine if it meets previous conditions and it has no controller references. if metav1.GetControllerOf(ms) == nil { if err := r.adoptOrphan(d, ms); err != nil { + r.recorder.Eventf(d, corev1.EventTypeWarning, "FailedAdopt", "Failed to adopt MachineSet %q: %v", ms.Name, err) klog.Warningf("Failed to adopt MachineSet %q into MachineDeployment %q: %v", ms.Name, d.Name, err) continue } + r.recorder.Eventf(d, corev1.EventTypeNormal, "SuccessfulAdopt", "Adopted MachineSet %q", ms.Name) } if !metav1.IsControlledBy(ms, d) { diff --git a/pkg/controller/machinedeployment/machinedeployment_controller_test.go b/pkg/controller/machinedeployment/machinedeployment_controller_test.go index 87065adc3f24..03063bdc4734 100644 --- a/pkg/controller/machinedeployment/machinedeployment_controller_test.go +++ b/pkg/controller/machinedeployment/machinedeployment_controller_test.go @@ -22,6 +22,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/tools/record" "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" @@ -132,8 +133,9 @@ func TestMachineSetToDeployments(t *testing.T) { v1alpha1.AddToScheme(scheme.Scheme) r := &ReconcileMachineDeployment{ - Client: fake.NewFakeClient(&ms1, &ms2, &ms3, machineDeplopymentList), - scheme: scheme.Scheme, + Client: fake.NewFakeClient(&ms1, &ms2, &ms3, machineDeplopymentList), + scheme: scheme.Scheme, + recorder: record.NewFakeRecorder(32), } for _, tc := range testsCases { @@ -206,8 +208,9 @@ func TestGetMachineDeploymentsForMachineSet(t *testing.T) { } v1alpha1.AddToScheme(scheme.Scheme) r := &ReconcileMachineDeployment{ - Client: fake.NewFakeClient(&ms1, &ms2, machineDeplopymentList), - scheme: scheme.Scheme, + Client: fake.NewFakeClient(&ms1, &ms2, machineDeplopymentList), + scheme: scheme.Scheme, + recorder: record.NewFakeRecorder(32), } for _, tc := range testCases { @@ -327,8 +330,9 @@ func TestGetMachineSetsForDeployment(t *testing.T) { v1alpha1.AddToScheme(scheme.Scheme) r := &ReconcileMachineDeployment{ - Client: fake.NewFakeClient(machineSetList), - scheme: scheme.Scheme, + Client: fake.NewFakeClient(machineSetList), + scheme: scheme.Scheme, + recorder: record.NewFakeRecorder(32), } for _, tc := range testCases { got, err := r.getMachineSetsForDeployment(&tc.machineDeployment) diff --git a/pkg/controller/machinedeployment/sync.go b/pkg/controller/machinedeployment/sync.go index e003eff84067..6fe0be776028 100644 --- a/pkg/controller/machinedeployment/sync.go +++ b/pkg/controller/machinedeployment/sync.go @@ -24,6 +24,7 @@ import ( "strconv" "github.com/pkg/errors" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/equality" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -200,11 +201,13 @@ func (r *ReconcileMachineDeployment) getNewMachineSet(d *clusterv1alpha1.Machine return nil, err case err != nil: klog.V(4).Infof("Failed to create new machine set %q: %v", newMS.Name, err) + r.recorder.Eventf(d, corev1.EventTypeWarning, "FailedCreate", "Failed to create MachineSet %q: %v", newMS.Name, err) return nil, err } if !alreadyExists { klog.V(4).Infof("Created new machine set %q", createdMS.Name) + r.recorder.Eventf(d, corev1.EventTypeNormal, "SuccessfulCreate", "Created MachineSet %q", newMS.Name) } err = r.updateMachineDeployment(d, func(innerDeployment *clusterv1alpha1.MachineDeployment) { @@ -408,8 +411,11 @@ func (r *ReconcileMachineDeployment) scaleMachineSetOperation(ms *clusterv1alpha dutil.SetReplicasAnnotations(ms, *(deployment.Spec.Replicas), *(deployment.Spec.Replicas)+dutil.MaxSurge(*deployment)) err = r.Update(context.Background(), ms) - if err == nil && sizeNeedsUpdate { + if err != nil { + r.recorder.Eventf(deployment, corev1.EventTypeWarning, "FailedScale", "Failed to scale MachineSet %q: %v", ms.Name, err) + } else if sizeNeedsUpdate { scaled = true + r.recorder.Eventf(deployment, corev1.EventTypeNormal, "SuccessfulScale", "Scaled %d MachineSet %q to %d", scaleOperation, ms.Name, newScale) } } @@ -454,8 +460,10 @@ func (r *ReconcileMachineDeployment) cleanupDeployment(oldMSs []*clusterv1alpha1 if err := r.Delete(context.Background(), ms); err != nil && !apierrors.IsNotFound(err) { // Return error instead of aggregating and continuing DELETEs on the theory // that we may be overloading the api server. + r.recorder.Eventf(deployment, corev1.EventTypeWarning, "FailedDelete", "Failed to delete MachineSet %q: %v", ms.Name, err) return err } + r.recorder.Eventf(deployment, corev1.EventTypeNormal, "SuccessfulDelete", "Deleted MachineSet %q", ms.Name) } return nil