From 64a299406c64f54caa9180723b06de3bfe866a9e Mon Sep 17 00:00:00 2001 From: wyike <77846369+wangyikewxgm@users.noreply.github.com> Date: Mon, 31 May 2021 10:46:07 +0800 Subject: [PATCH] set LongWait as a configurable global var (#334) Signed-off-by: wangyike --- cmd/oam-kubernetes-runtime/main.go | 3 +++ pkg/controller/controller.go | 5 +++++ .../applicationconfiguration.go | 14 +++++++++++--- .../applicationconfiguration_test.go | 3 ++- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/cmd/oam-kubernetes-runtime/main.go b/cmd/oam-kubernetes-runtime/main.go index eeae138a..5590f58e 100644 --- a/cmd/oam-kubernetes-runtime/main.go +++ b/cmd/oam-kubernetes-runtime/main.go @@ -5,6 +5,7 @@ import ( "io" "os" "strconv" + "time" "github.com/crossplane/crossplane-runtime/pkg/logging" "go.uber.org/zap/zapcore" @@ -56,6 +57,8 @@ func main() { "RevisionLimit is the maximum number of revisions that will be maintained. The default value is 50.") flag.BoolVar(&controllerArgs.ApplyOnceOnly, "apply-once-only", false, "For the purpose of some production environment that workload or trait should not be affected if no spec change") + flag.DurationVar(&controllerArgs.LongWait, "long-wait", 1*time.Minute, "long-wait is controller next reconcile interval time like 30s, 2m etc. The default value is 1m, "+ + "you can set it to 0 for no reconcile routine after success ") flag.Parse() // setup logging diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index aa78064e..1f956287 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -13,6 +13,8 @@ limitations under the License. package controller +import "time" + // Args args used by controller type Args struct { // RevisionLimit is the maximum number of revisions that will be maintained. @@ -22,4 +24,7 @@ type Args struct { // ApplyOnceOnly indicates whether workloads and traits should be // affected if no spec change is made in the ApplicationConfiguration. ApplyOnceOnly bool + + // LongWait is controller next reconcile interval time + LongWait time.Duration } diff --git a/pkg/controller/v1alpha2/applicationconfiguration/applicationconfiguration.go b/pkg/controller/v1alpha2/applicationconfiguration/applicationconfiguration.go index 7256b852..c6b2f1f8 100644 --- a/pkg/controller/v1alpha2/applicationconfiguration/applicationconfiguration.go +++ b/pkg/controller/v1alpha2/applicationconfiguration/applicationconfiguration.go @@ -50,7 +50,6 @@ const ( reconcileTimeout = 1 * time.Minute dependCheckWait = 10 * time.Second shortWait = 30 * time.Second - longWait = 1 * time.Minute ) // Reconcile error strings. @@ -99,7 +98,8 @@ func Setup(mgr ctrl.Manager, args controller.Args, l logging.Logger) error { Complete(NewReconciler(mgr, dm, WithLogger(l.WithValues("controller", name)), WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - WithApplyOnceOnly(args.ApplyOnceOnly))) + WithApplyOnceOnly(args.ApplyOnceOnly), + WithLogWaitTime(args.LongWait))) } // An OAMApplicationReconciler reconciles OAM ApplicationConfigurations by rendering and @@ -115,6 +115,7 @@ type OAMApplicationReconciler struct { preHooks map[string]ControllerHooks postHooks map[string]ControllerHooks applyOnceOnly bool + longWait time.Duration } // A ReconcilerOption configures a Reconciler. @@ -179,6 +180,13 @@ func WithApplyOnceOnly(applyOnceOnly bool) ReconcilerOption { } } +// WithLogWaitTime set next reconcile time interval +func WithLogWaitTime(longWait time.Duration) ReconcilerOption { + return func(r *OAMApplicationReconciler) { + r.longWait = longWait + } +} + // NewReconciler returns an OAMApplicationReconciler that reconciles ApplicationConfigurations // by rendering and instantiating their Components and Traits. func NewReconciler(m ctrl.Manager, dm discoverymapper.DiscoveryMapper, o ...ReconcilerOption) *OAMApplicationReconciler { @@ -288,7 +296,7 @@ func (r *OAMApplicationReconciler) Reconcile(req reconcile.Request) (result reco ac.SetConditions(v1alpha1.ReconcileError(errors.Wrap(err, errRenderComponents))) return reconcile.Result{RequeueAfter: shortWait}, errors.Wrap(r.client.Status().Update(ctx, ac), errUpdateAppConfigStatus) } - waitTime := longWait + waitTime := r.longWait if len(depStatus.Unsatisfied) != 0 { waitTime = dependCheckWait ac.Status.Dependency = *depStatus diff --git a/pkg/controller/v1alpha2/applicationconfiguration/applicationconfiguration_test.go b/pkg/controller/v1alpha2/applicationconfiguration/applicationconfiguration_test.go index ea52c87b..5fb38ff6 100644 --- a/pkg/controller/v1alpha2/applicationconfiguration/applicationconfiguration_test.go +++ b/pkg/controller/v1alpha2/applicationconfiguration/applicationconfiguration_test.go @@ -538,10 +538,11 @@ func TestReconciler(t *testing.T) { WithPosthook("postHook", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration, logger logging.Logger) (reconcile.Result, error) { return reconcile.Result{RequeueAfter: shortWait}, nil })), + WithLogWaitTime(1 * time.Minute), }, }, want: want{ - result: reconcile.Result{RequeueAfter: longWait}, + result: reconcile.Result{RequeueAfter: 1 * time.Minute}, }, }, "RegisterFinalizer": {