diff --git a/controllers/user/controllers/user_controller.go b/controllers/user/controllers/user_controller.go index 2f687b85fa1..f874c74044e 100644 --- a/controllers/user/controllers/user_controller.go +++ b/controllers/user/controllers/user_controller.go @@ -23,9 +23,10 @@ import ( "strconv" "time" + "golang.org/x/exp/rand" + utilcontroller "github.com/labring/operator-sdk/controller" "github.com/labring/operator-sdk/hash" - "github.com/labring/sealos/controllers/user/controllers/helper/config" "github.com/labring/sealos/controllers/user/controllers/helper/kubeconfig" @@ -67,7 +68,9 @@ type UserReconciler struct { config *rest.Config *runtime.Scheme client.Client - finalizer *utilcontroller.Finalizer + finalizer *utilcontroller.Finalizer + minRequeueDuration time.Duration + maxRequeueDuration time.Duration } type ctxKey string @@ -109,7 +112,8 @@ func (r *UserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. } // SetupWithManager sets up the controller with the Manager. -func (r *UserReconciler) SetupWithManager(mgr ctrl.Manager, opts utilcontroller.RateLimiterOptions) error { +func (r *UserReconciler) SetupWithManager(mgr ctrl.Manager, opts utilcontroller.RateLimiterOptions, + minRequeueDuration time.Duration, maxRequeueDuration time.Duration) error { const controllerName = "user_controller" if r.Client == nil { r.Client = mgr.GetClient() @@ -125,6 +129,8 @@ func (r *UserReconciler) SetupWithManager(mgr ctrl.Manager, opts utilcontroller. r.cache = mgr.GetCache() r.config = mgr.GetConfig() r.Logger.V(1).Info("init reconcile controller user") + r.minRequeueDuration = minRequeueDuration + r.maxRequeueDuration = maxRequeueDuration owner := &handler.EnqueueRequestForOwner{OwnerType: &userv1.User{}, IsController: true} return ctrl.NewControllerManagedBy(mgr). For(&userv1.User{}, builder.WithPredicates( @@ -174,7 +180,7 @@ func (r *UserReconciler) reconcile(ctx context.Context, obj client.Object) (ctrl r.Recorder.Eventf(user, v1.EventTypeWarning, "SyncStatus", "Sync status %s is error: %v", user.Name, err) return ctrl.Result{}, err } - return ctrl.Result{}, nil + return ctrl.Result{RequeueAfter: RandTimeDurationBetween(r.minRequeueDuration, r.maxRequeueDuration)}, nil } func (r *UserReconciler) initStatus(ctx context.Context, user *userv1.User) context.Context { @@ -595,3 +601,14 @@ func (r *UserReconciler) updateStatus(ctx context.Context, nn types.NamespacedNa return r.Client.Status().Update(ctx, original) }) } + +// RandTimeDurationBetween get a random time duration between min and max +func RandTimeDurationBetween(min, max time.Duration) time.Duration { + if min >= max { + return min + } + minInNano := min.Nanoseconds() + maxInNano := max.Nanoseconds() + randDurationInNano := rand.Int63n(maxInNano-minInNano) + minInNano + return time.Duration(randDurationInNano) * time.Nanosecond +} diff --git a/controllers/user/go.mod b/controllers/user/go.mod index 37c3f210435..bb232993217 100644 --- a/controllers/user/go.mod +++ b/controllers/user/go.mod @@ -7,7 +7,6 @@ require ( github.com/labring/operator-sdk v1.0.1 github.com/onsi/ginkgo v1.16.5 github.com/onsi/gomega v1.22.1 - golang.org/x/sync v0.1.0 k8s.io/api v0.25.6 k8s.io/apimachinery v0.25.6 k8s.io/client-go v0.25.6 diff --git a/controllers/user/go.sum b/controllers/user/go.sum index 053eea39835..8a4f6f8fb04 100644 --- a/controllers/user/go.sum +++ b/controllers/user/go.sum @@ -327,8 +327,8 @@ github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/labring/endpoints-operator/library v0.0.0-20221201063058-fa2d5a457d1d h1:WoYUoAGk61I2aVsrT/r15ASoznJ7zI9jHfbEBBsICzI= -github.com/labring/endpoints-operator/library v0.0.0-20221201063058-fa2d5a457d1d/go.mod h1:yYszaNC8F3plDtz/NfQpwP3Wil9YXsAId9j94t75iDk= +github.com/labring/operator-sdk v1.0.1 h1:JS+j9nF0lihkPJnMYJBZrH7Kfp/dKB2cnbBRMfkmE+g= +github.com/labring/operator-sdk v1.0.1/go.mod h1:velfQ6SyrLXBeAShetQyR7q1zJNd8vGO6jjzbKcofj8= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= @@ -625,8 +625,6 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/controllers/user/main.go b/controllers/user/main.go index f66a6e245c6..d2713796e4e 100644 --- a/controllers/user/main.go +++ b/controllers/user/main.go @@ -20,6 +20,7 @@ import ( "context" "flag" "os" + "time" utilcontroller "github.com/labring/operator-sdk/controller" @@ -57,12 +58,18 @@ func main() { enableLeaderElection bool probeAddr string rateLimiterOptions utilcontroller.RateLimiterOptions + syncPeriod time.Duration + minRequeueDuration time.Duration + maxRequeueDuration time.Duration ) flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") flag.BoolVar(&enableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") + flag.DurationVar(&syncPeriod, "sync-period", time.Hour*24*30, "SyncPeriod determines the minimum frequency at which watched resources are reconciled.") + flag.DurationVar(&minRequeueDuration, "min-requeue-duration", time.Hour*24, "The minimum duration between requeue options of a resource.") + flag.DurationVar(&maxRequeueDuration, "max-requeue-duration", time.Hour*24*2, "The maximum duration between requeue options of a resource.") rateLimiterOptions.BindFlags(flag.CommandLine) opts := zap.Options{ Development: true, @@ -79,6 +86,7 @@ func main() { HealthProbeBindAddress: probeAddr, LeaderElection: enableLeaderElection, LeaderElectionID: "785548a1.sealos.io", + SyncPeriod: &syncPeriod, // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily // when the Manager ends. This requires the binary to immediately end when the // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly @@ -95,7 +103,7 @@ func main() { setupLog.Error(err, "unable to start manager") os.Exit(1) } - if err = (&controllers.UserReconciler{}).SetupWithManager(mgr, rateLimiterOptions); err != nil { + if err = (&controllers.UserReconciler{}).SetupWithManager(mgr, rateLimiterOptions, minRequeueDuration, maxRequeueDuration); err != nil { setupLog.Error(err, "unable to create controller", "controller", "User") os.Exit(1) }