diff --git a/pkg/operator/controllers/machinehealthcheck/machinehealthcheck_controller.go b/pkg/operator/controllers/machinehealthcheck/machinehealthcheck_controller.go index 3f5be5c3c9c..68e3e5974a0 100644 --- a/pkg/operator/controllers/machinehealthcheck/machinehealthcheck_controller.go +++ b/pkg/operator/controllers/machinehealthcheck/machinehealthcheck_controller.go @@ -70,12 +70,13 @@ func (r *MachineHealthCheckReconciler) Reconcile(ctx context.Context, request ct r.Log.Debug("running") if !instance.Spec.OperatorFlags.GetSimpleBoolean(MHCManaged) { - r.SetProgressing(ctx, "We are disabling the MHCHealthCheck because this cluster at current moment is NOT MHC Managed.") + r.SetProgressing(ctx, "MHC and it's alerts are disabled because this cluster is not MHC managed.") err := r.dh.EnsureDeleted(ctx, "MachineHealthCheck", "openshift-machine-api", "aro-machinehealthcheck") if err != nil { r.Log.Error(err) r.SetDegraded(ctx, err) + r.ClearProgressing(ctx) return reconcile.Result{RequeueAfter: time.Hour}, err } @@ -84,6 +85,7 @@ func (r *MachineHealthCheckReconciler) Reconcile(ctx context.Context, request ct if err != nil { r.Log.Error(err) r.SetDegraded(ctx, err) + r.ClearProgressing(ctx) return reconcile.Result{RequeueAfter: time.Hour}, err } diff --git a/pkg/operator/controllers/machinehealthcheck/machinehealthcheck_controller_test.go b/pkg/operator/controllers/machinehealthcheck/machinehealthcheck_controller_test.go index 0c21580b36a..9395425b35a 100644 --- a/pkg/operator/controllers/machinehealthcheck/machinehealthcheck_controller_test.go +++ b/pkg/operator/controllers/machinehealthcheck/machinehealthcheck_controller_test.go @@ -19,24 +19,36 @@ import ( arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" mock_dynamichelper "github.com/Azure/ARO-RP/pkg/util/mocks/dynamichelper" _ "github.com/Azure/ARO-RP/pkg/util/scheme" + utilconditions "github.com/Azure/ARO-RP/test/util/conditions" utilerror "github.com/Azure/ARO-RP/test/util/error" + operatorv1 "github.com/openshift/api/operator/v1" ) // Test reconcile function func TestMachineHealthCheckReconciler(t *testing.T) { + + transitionTime := metav1.Time{Time: time.Now()} + defaultAvailable := utilconditions.ControllerDefaultAvailable(MHCControllerName) + defaultProgressing := utilconditions.ControllerDefaultProgressing(MHCControllerName) + defaultDegraded := utilconditions.ControllerDefaultDegraded(MHCControllerName) + + defaultConditions := []operatorv1.OperatorCondition{defaultAvailable, defaultProgressing, defaultDegraded} + type test struct { name string instance *arov1alpha1.Cluster mocks func(mdh *mock_dynamichelper.MockInterface) + wantConditions []operatorv1.OperatorCondition wantErr string wantRequeueAfter time.Duration } for _, tt := range []*test{ { - name: "Failure to get instance", - mocks: func(mdh *mock_dynamichelper.MockInterface) {}, - wantErr: `clusters.aro.openshift.io "cluster" not found`, + name: "Failure to get instance", + mocks: func(mdh *mock_dynamichelper.MockInterface) {}, + wantConditions: defaultConditions, + wantErr: `clusters.aro.openshift.io "cluster" not found`, }, { name: "Enabled Feature Flag is false", @@ -49,12 +61,16 @@ func TestMachineHealthCheckReconciler(t *testing.T) { MHCEnabled: strconv.FormatBool(false), }, }, + Status: arov1alpha1.ClusterStatus{ + Conditions: defaultConditions, + }, }, mocks: func(mdh *mock_dynamichelper.MockInterface) { mdh.EXPECT().EnsureDeleted(gomock.Any(), "MachineHealthCheck", "openshift-machine-api", "aro-machinehealthcheck").Times(0) mdh.EXPECT().Ensure(gomock.Any(), gomock.Any()).Return(nil).Times(0) }, - wantErr: "", + wantConditions: defaultConditions, + wantErr: "", }, { name: "Managed Feature Flag is false: ensure mhc and its alert are deleted", @@ -68,12 +84,16 @@ func TestMachineHealthCheckReconciler(t *testing.T) { MHCManaged: strconv.FormatBool(false), }, }, + Status: arov1alpha1.ClusterStatus{ + Conditions: defaultConditions, + }, }, mocks: func(mdh *mock_dynamichelper.MockInterface) { mdh.EXPECT().EnsureDeleted(gomock.Any(), "MachineHealthCheck", "openshift-machine-api", "aro-machinehealthcheck").Times(1) mdh.EXPECT().EnsureDeleted(gomock.Any(), "PrometheusRule", "openshift-machine-api", "mhc-remediation-alert").Times(1) }, - wantErr: "", + wantConditions: defaultConditions, + wantErr: "", }, { name: "Managed Feature Flag is false: mhc fails to delete, an error is returned", @@ -87,11 +107,24 @@ func TestMachineHealthCheckReconciler(t *testing.T) { MHCManaged: strconv.FormatBool(false), }, }, + Status: arov1alpha1.ClusterStatus{ + Conditions: defaultConditions, + }, }, mocks: func(mdh *mock_dynamichelper.MockInterface) { mdh.EXPECT().EnsureDeleted(gomock.Any(), "MachineHealthCheck", "openshift-machine-api", "aro-machinehealthcheck").Return(errors.New("Could not delete mhc")) }, - wantErr: "Could not delete mhc", + wantErr: "Could not delete mhc", + wantConditions: []operatorv1.OperatorCondition{ + defaultAvailable, + defaultProgressing, + { + Type: MHCControllerName + "Controller" + operatorv1.OperatorStatusTypeDegraded, + Status: operatorv1.ConditionTrue, + LastTransitionTime: transitionTime, + Message: "Could not delete mhc", + }, + }, wantRequeueAfter: time.Hour, }, { @@ -106,12 +139,25 @@ func TestMachineHealthCheckReconciler(t *testing.T) { MHCManaged: strconv.FormatBool(false), }, }, + Status: arov1alpha1.ClusterStatus{ + Conditions: defaultConditions, + }, }, mocks: func(mdh *mock_dynamichelper.MockInterface) { mdh.EXPECT().EnsureDeleted(gomock.Any(), "MachineHealthCheck", "openshift-machine-api", "aro-machinehealthcheck").Times(1) mdh.EXPECT().EnsureDeleted(gomock.Any(), "PrometheusRule", "openshift-machine-api", "mhc-remediation-alert").Return(errors.New("Could not delete mhc alert")) }, - wantErr: "Could not delete mhc alert", + wantErr: "Could not delete mhc alert", + wantConditions: []operatorv1.OperatorCondition{ + defaultAvailable, + defaultProgressing, + { + Type: MHCControllerName + "Controller" + operatorv1.OperatorStatusTypeDegraded, + Status: operatorv1.ConditionTrue, + LastTransitionTime: transitionTime, + Message: "Could not delete mhc alert", + }, + }, wantRequeueAfter: time.Hour, }, { @@ -130,7 +176,8 @@ func TestMachineHealthCheckReconciler(t *testing.T) { mocks: func(mdh *mock_dynamichelper.MockInterface) { mdh.EXPECT().Ensure(gomock.Any(), gomock.Any()).Return(nil).Times(1) }, - wantErr: "", + wantConditions: defaultConditions, + wantErr: "", }, { name: "When ensuring resources fails, an error is returned", @@ -144,11 +191,24 @@ func TestMachineHealthCheckReconciler(t *testing.T) { MHCManaged: strconv.FormatBool(true), }, }, + Status: arov1alpha1.ClusterStatus{ + Conditions: defaultConditions, + }, }, mocks: func(mdh *mock_dynamichelper.MockInterface) { mdh.EXPECT().Ensure(gomock.Any(), gomock.Any()).Return(errors.New("failed to ensure")) }, wantErr: "failed to ensure", + wantConditions: []operatorv1.OperatorCondition{ + defaultAvailable, + defaultProgressing, + { + Type: MHCControllerName + "Controller" + operatorv1.OperatorStatusTypeDegraded, + Status: operatorv1.ConditionTrue, + LastTransitionTime: transitionTime, + Message: "failed to ensure", + }, + }, }, } { t.Run(tt.name, func(t *testing.T) { @@ -178,7 +238,11 @@ func TestMachineHealthCheckReconciler(t *testing.T) { result, err := r.Reconcile(ctx, request) if tt.wantRequeueAfter != result.RequeueAfter { - t.Errorf("Wanted to requeue after %v but was set to %v", tt.wantRequeueAfter, result.RequeueAfter) + t.Errorf("wanted to requeue after %v but was set to %v", tt.wantRequeueAfter, result.RequeueAfter) + } + + if tt.instance != nil { + utilconditions.AssertControllerConditions(t, ctx, r.AROController.Client, tt.wantConditions) } utilerror.AssertErrorMessage(t, err, tt.wantErr)