Skip to content

Commit

Permalink
Propagate errors of ARO MachineHealthCheckController to ARO Operator
Browse files Browse the repository at this point in the history
  • Loading branch information
safwank97 committed Oct 10, 2023
1 parent 22a6d60 commit 2059f0f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 42 deletions.
6 changes: 3 additions & 3 deletions cmd/aro/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ func operator(ctx context.Context, log *logrus.Entry) error {
client)).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create controller %s: %v", autosizednodes.ControllerName, err)
}
if err = (machinehealthcheck.NewReconciler(
log.WithField("controller", machinehealthcheck.ControllerName),
if err = (machinehealthcheck.NewMachineHealthCheckReconciler(
log.WithField("controller", machinehealthcheck.MHCControllerName),
client, dh)).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create controller %s: %v", machinehealthcheck.ControllerName, err)
return fmt.Errorf("unable to create controller %s: %v", machinehealthcheck.MHCControllerName, err)
}
if err = (ingress.NewReconciler(
log.WithField("controller", ingress.ControllerName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/sirupsen/logrus"
kruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
Expand All @@ -22,6 +21,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
"github.com/Azure/ARO-RP/pkg/operator/controllers/base"
"github.com/Azure/ARO-RP/pkg/util/dynamichelper"
)

Expand All @@ -32,50 +32,63 @@ var machinehealthcheckYaml []byte
var mhcremediationalertYaml []byte

const (
ControllerName string = "MachineHealthCheck"
managed string = "aro.machinehealthcheck.managed"
enabled string = "aro.machinehealthcheck.enabled"
MHCControllerName string = "MachineHealthCheck"
MHCManaged string = "aro.machinehealthcheck.managed"
MHCEnabled string = "aro.machinehealthcheck.enabled"
)

type Reconciler struct {
log *logrus.Entry
type MachineHealthCheckReconciler struct {
base.AROController

dh dynamichelper.Interface

client client.Client
}

func NewReconciler(log *logrus.Entry, client client.Client, dh dynamichelper.Interface) *Reconciler {
return &Reconciler{
log: log,
dh: dh,
client: client,
func NewMachineHealthCheckReconciler(log *logrus.Entry, client client.Client, dh dynamichelper.Interface) *MachineHealthCheckReconciler {
return &MachineHealthCheckReconciler{
AROController: base.AROController{
Log: log,
Client: client,
Name: MHCControllerName,
},
dh: dh,
}
}

func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
instance := &arov1alpha1.Cluster{}
err := r.client.Get(ctx, types.NamespacedName{Name: arov1alpha1.SingletonClusterName}, instance)
// Reconcile watches MachineHealthCheck objects, and if any changes,
// reconciles the associated ARO MachineHealthCheck object

func (r *MachineHealthCheckReconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
instance, err := r.GetCluster(ctx)

if err != nil {
return reconcile.Result{}, err
}

if !instance.Spec.OperatorFlags.GetSimpleBoolean(enabled) {
r.log.Debug("controller is disabled")
if !instance.Spec.OperatorFlags.GetSimpleBoolean(MHCEnabled) {
r.Log.Debug("controller is disabled")
return reconcile.Result{}, nil
}

r.log.Debug("running")
if !instance.Spec.OperatorFlags.GetSimpleBoolean(managed) {
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.")

err := r.dh.EnsureDeleted(ctx, "MachineHealthCheck", "openshift-machine-api", "aro-machinehealthcheck")
if err != nil {
r.Log.Error(err)
r.SetDegraded(ctx, err)

return reconcile.Result{RequeueAfter: time.Hour}, err
}

err = r.dh.EnsureDeleted(ctx, "PrometheusRule", "openshift-machine-api", "mhc-remediation-alert")
if err != nil {
r.Log.Error(err)
r.SetDegraded(ctx, err)

return reconcile.Result{RequeueAfter: time.Hour}, err
}

return reconcile.Result{}, nil
}

Expand All @@ -84,6 +97,9 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
for _, asset := range [][]byte{machinehealthcheckYaml, mhcremediationalertYaml} {
resource, _, err := scheme.Codecs.UniversalDeserializer().Decode(asset, nil, nil)
if err != nil {
r.Log.Error(err)
r.SetDegraded(ctx, err)

return reconcile.Result{}, err
}

Expand All @@ -105,21 +121,25 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
// create/update the MHC CR
err = r.dh.Ensure(ctx, resources...)
if err != nil {
r.Log.Error(err)
r.SetDegraded(ctx, err)

return reconcile.Result{}, err
}

r.ClearConditions(ctx)
return reconcile.Result{}, nil
}

// SetupWithManager will manage only our MHC resource with our specific controller name
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *MachineHealthCheckReconciler) SetupWithManager(mgr ctrl.Manager) error {
aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool {
return strings.EqualFold(arov1alpha1.SingletonClusterName, o.GetName())
})

return ctrl.NewControllerManagedBy(mgr).
For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)).
Named(ControllerName).
Named(MHCControllerName).
Owns(&machinev1beta1.MachineHealthCheck{}).
Owns(&monitoringv1.PrometheusRule{}).
Complete(r)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

// Test reconcile function
func TestReconciler(t *testing.T) {
func TestMachineHealthCheckReconciler(t *testing.T) {
type test struct {
name string
instance *arov1alpha1.Cluster
Expand All @@ -46,7 +46,7 @@ func TestReconciler(t *testing.T) {
},
Spec: arov1alpha1.ClusterSpec{
OperatorFlags: arov1alpha1.OperatorFlags{
enabled: strconv.FormatBool(false),
MHCEnabled: strconv.FormatBool(false),
},
},
},
Expand All @@ -64,8 +64,8 @@ func TestReconciler(t *testing.T) {
},
Spec: arov1alpha1.ClusterSpec{
OperatorFlags: arov1alpha1.OperatorFlags{
enabled: strconv.FormatBool(true),
managed: strconv.FormatBool(false),
MHCEnabled: strconv.FormatBool(true),
MHCManaged: strconv.FormatBool(false),
},
},
},
Expand All @@ -83,8 +83,8 @@ func TestReconciler(t *testing.T) {
},
Spec: arov1alpha1.ClusterSpec{
OperatorFlags: arov1alpha1.OperatorFlags{
enabled: strconv.FormatBool(true),
managed: strconv.FormatBool(false),
MHCEnabled: strconv.FormatBool(true),
MHCManaged: strconv.FormatBool(false),
},
},
},
Expand All @@ -102,8 +102,8 @@ func TestReconciler(t *testing.T) {
},
Spec: arov1alpha1.ClusterSpec{
OperatorFlags: arov1alpha1.OperatorFlags{
enabled: strconv.FormatBool(true),
managed: strconv.FormatBool(false),
MHCEnabled: strconv.FormatBool(true),
MHCManaged: strconv.FormatBool(false),
},
},
},
Expand All @@ -122,8 +122,8 @@ func TestReconciler(t *testing.T) {
},
Spec: arov1alpha1.ClusterSpec{
OperatorFlags: arov1alpha1.OperatorFlags{
enabled: strconv.FormatBool(true),
managed: strconv.FormatBool(true),
MHCEnabled: strconv.FormatBool(true),
MHCManaged: strconv.FormatBool(true),
},
},
},
Expand All @@ -140,8 +140,8 @@ func TestReconciler(t *testing.T) {
},
Spec: arov1alpha1.ClusterSpec{
OperatorFlags: arov1alpha1.OperatorFlags{
enabled: strconv.FormatBool(true),
managed: strconv.FormatBool(true),
MHCEnabled: strconv.FormatBool(true),
MHCManaged: strconv.FormatBool(true),
},
},
},
Expand All @@ -165,11 +165,13 @@ func TestReconciler(t *testing.T) {
}

ctx := context.Background()
r := &Reconciler{
log: logrus.NewEntry(logrus.StandardLogger()),
dh: mdh,
client: clientBuilder.Build(),
}

r := NewMachineHealthCheckReconciler(
logrus.NewEntry(logrus.StandardLogger()),
clientBuilder.Build(),
mdh,
)

request := ctrl.Request{}
request.Name = "cluster"

Expand Down

0 comments on commit 2059f0f

Please sign in to comment.