Skip to content

Commit

Permalink
Add events to MachineDeployment operations (kubernetes-sigs#1014)
Browse files Browse the repository at this point in the history
(cherry picked from commit a5e5fdb)
  • Loading branch information
detiber authored and ncdc committed Jun 25, 2019
1 parent 245a447 commit 8591410
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion pkg/controller/machinedeployment/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8591410

Please sign in to comment.