Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ACM-8441] Added cronjob for intervaled reconciling #548

Merged
merged 4 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions controllers/backplaneconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
"time"

"github.com/go-co-op/gocron"
backplanev1 "github.com/stolostron/backplane-operator/api/v1"
"github.com/stolostron/backplane-operator/pkg/foundation"
"github.com/stolostron/backplane-operator/pkg/images"
Expand Down Expand Up @@ -83,6 +84,12 @@ const (
controlPlane = "backplane-operator"
)

var (
scheduler *gocron.Scheduler
cronResyncTag = "multiclusterengine-operator-resync"
reconciliationInterval = 10 // minutes
)

//+kubebuilder:rbac:groups=multicluster.openshift.io,resources=multiclusterengines,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=multicluster.openshift.io,resources=multiclusterengines/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=multicluster.openshift.io,resources=multiclusterengines/finalizers,verbs=update
Expand Down Expand Up @@ -146,6 +153,14 @@ const (
// move the current state of the cluster closer to the desired state.
func (r *MultiClusterEngineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (retRes ctrl.Result, retErr error) {
log := log.Log.WithName("reconcile")
log.Info("Reconciling MultiClusterEngine")

// Initalize sceduler instance for operator resync cronjob.
if scheduler == nil {
log.Info("Setting up scheduler for operator resync")
r.InitScheduler()
}

// Fetch the BackplaneConfig instance
backplaneConfig, err := r.getBackplaneConfig(ctx, req)
if err != nil && !apierrors.IsNotFound(err) {
Expand Down Expand Up @@ -287,6 +302,11 @@ func (r *MultiClusterEngineReconciler) Reconcile(ctx context.Context, req ctrl.R
// Do not reconcile objects if this instance of mce is labeled "paused"
if utils.IsPaused(backplaneConfig) {
log.Info("MultiClusterEngine reconciliation is paused. Nothing more to do.")
if ok := scheduler.IsRunning(); ok {
log.Info("Pausing MultiClusterEngine operator controller resync job.")
go r.StopScheduleOperatorControllerResync()
}

cond := status.NewCondition(
backplanev1.MultiClusterEngineProgressing,
metav1.ConditionUnknown,
Expand All @@ -295,6 +315,8 @@ func (r *MultiClusterEngineReconciler) Reconcile(ctx context.Context, req ctrl.R
)
r.StatusManager.AddCondition(cond)
return ctrl.Result{}, nil
} else if ok := scheduler.IsRunning(); !ok {
defer r.ScheduleOperatorControllerResync(ctx, req)
}

result, err = r.DeployAlwaysSubcomponents(ctx, backplaneConfig)
Expand Down Expand Up @@ -1411,3 +1433,32 @@ func (r *MultiClusterEngineReconciler) getClusterIngressDomain(ctx context.Conte
}
return clusterIngress.Spec.Domain, nil
}

func (r *MultiClusterEngineReconciler) InitScheduler() {
scheduler = gocron.NewScheduler(time.UTC)
}

func (r *MultiClusterEngineReconciler) ScheduleOperatorControllerResync(ctx context.Context, req ctrl.Request) {
log := log.Log.WithName("reconcile")

if ok := scheduler.IsRunning(); !ok {
_, err := scheduler.Tag(cronResyncTag).Every(reconciliationInterval).Minutes().Do(r.Reconcile, ctx, req)

if err != nil {
log.Error(err, "failed to schedule scheduler job for operator controller resync")
} else {
log.Info(fmt.Sprintf("Starting scheduler job for operator controller. Reconciling every %v minutes",
reconciliationInterval))
scheduler.StartAsync()
}
}
}

// StopScheduleOperatorControllerResync ...
func (r *MultiClusterEngineReconciler) StopScheduleOperatorControllerResync() {
scheduler.Stop()

if ok := scheduler.IsRunning(); !ok {
r.InitScheduler()
}
}
Loading
Loading