From 6fc0e58e30b8f7b99ebdc0ace718e195aae3df91 Mon Sep 17 00:00:00 2001 From: Andrea Spagnolo Date: Fri, 26 Jun 2020 22:01:28 +0200 Subject: [PATCH] Add ObservedGeneration to MachinePool Controller Signed-off-by: Andrea Spagnolo --- exp/api/v1alpha3/machinepool_types.go | 4 ++++ exp/controllers/machinepool_controller.go | 7 ++++++- exp/controllers/machinepool_controller_test.go | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/exp/api/v1alpha3/machinepool_types.go b/exp/api/v1alpha3/machinepool_types.go index b7db1a28698b..9e392dd2f2f8 100644 --- a/exp/api/v1alpha3/machinepool_types.go +++ b/exp/api/v1alpha3/machinepool_types.go @@ -116,6 +116,10 @@ type MachinePoolStatus struct { // InfrastructureReady is the state of the infrastructure provider. // +optional InfrastructureReady bool `json:"infrastructureReady"` + + // ObservedGeneration is the latest generation observed by the controller. + // +optional + ObservedGeneration int64 `json:"observedGeneration,omitempty"` } // ANCHOR_END: MachinePoolStatus diff --git a/exp/controllers/machinepool_controller.go b/exp/controllers/machinepool_controller.go index 42cb90957d7a..263d6926a0f4 100644 --- a/exp/controllers/machinepool_controller.go +++ b/exp/controllers/machinepool_controller.go @@ -137,7 +137,12 @@ func (r *MachinePoolReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, rete // TODO(jpang): add support for metrics. // Always attempt to patch the object and status after each reconciliation. - if err := patchHelper.Patch(ctx, mp); err != nil { + // Patch ObservedGeneration only if the reconciliation completed successfully + patchOpts := []patch.Option{} + if reterr == nil { + patchOpts = append(patchOpts, patch.WithStatusObservedGeneration{}) + } + if err := patchHelper.Patch(ctx, mp, patchOpts...); err != nil { reterr = kerrors.NewAggregate([]error{reterr, err}) } }() diff --git a/exp/controllers/machinepool_controller_test.go b/exp/controllers/machinepool_controller_test.go index f81914b6ef17..991e402de755 100644 --- a/exp/controllers/machinepool_controller_test.go +++ b/exp/controllers/machinepool_controller_test.go @@ -337,6 +337,7 @@ func TestReconcileMachinePoolRequest(t *testing.T) { NodeRefs: []corev1.ObjectReference{ {Name: "test"}, }, + ObservedGeneration: 1, }, }, expected: expected{ @@ -372,6 +373,7 @@ func TestReconcileMachinePoolRequest(t *testing.T) { NodeRefs: []corev1.ObjectReference{ {Name: "test"}, }, + ObservedGeneration: 1, }, }, expected: expected{