support configurable cluster resync perioud

Signed-off-by: yuswift <yuswiftli@yunify.com>
This commit is contained in:
yuswift
2020-12-21 12:28:24 +08:00
parent d814d5db7b
commit 9e0a4b69e8
4 changed files with 34 additions and 22 deletions

View File

@@ -157,6 +157,8 @@ type clusterController struct {
mu sync.RWMutex
clusterMap map[string]*clusterData
resyncPeriod time.Duration
}
func NewClusterController(
@@ -165,6 +167,7 @@ func NewClusterController(
clusterInformer clusterinformer.ClusterInformer,
clusterClient clusterclient.ClusterInterface,
openpitrixClient openpitrix.Client,
resyncPeriod time.Duration,
) *clusterController {
broadcaster := record.NewBroadcaster()
@@ -184,23 +187,18 @@ func NewClusterController(
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "cluster"),
workerLoopPeriod: time.Second,
clusterMap: make(map[string]*clusterData),
resyncPeriod: resyncPeriod,
}
c.clusterLister = clusterInformer.Lister()
c.clusterHasSynced = clusterInformer.Informer().HasSynced
clusterInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
clusterInformer.Informer().AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
AddFunc: c.addCluster,
UpdateFunc: func(oldObj, newObj interface{}) {
newCluster := newObj.(*clusterv1alpha1.Cluster)
oldCluster := oldObj.(*clusterv1alpha1.Cluster)
if newCluster.ResourceVersion == oldCluster.ResourceVersion {
return
}
c.addCluster(newObj)
},
DeleteFunc: c.addCluster,
})
}, resyncPeriod)
return c
}
@@ -226,15 +224,11 @@ func (c *clusterController) Run(workers int, stopCh <-chan struct{}) error {
// refresh cluster configz every 2 minutes
go wait.Until(func() {
if err := c.syncStatus(); err != nil {
klog.Errorf("Error periodically sync cluster status, %v", err)
}
if err := c.reconcileHostCluster(); err != nil {
klog.Errorf("Error create host cluster, error %v", err)
}
}, 2*time.Minute, stopCh)
}, c.resyncPeriod, stopCh)
<-stopCh
return nil
@@ -354,6 +348,7 @@ func (c *clusterController) reconcileHostCluster() error {
}
func (c *clusterController) syncCluster(key string) error {
klog.V(5).Infof("starting to sync cluster %s", key)
startTime := time.Now()
_, name, err := cache.SplitMetaNamespaceKey(key)

View File

@@ -16,7 +16,13 @@ limitations under the License.
package multicluster
import "github.com/spf13/pflag"
import (
"time"
"github.com/spf13/pflag"
)
const DefaultResyncPeriod = time.Duration(120) * time.Second
type Options struct {
// Enable
@@ -36,16 +42,20 @@ type Options struct {
// AgentImage is the image used when generating deployment for all cluster agents.
AgentImage string `json:"agentImage,omitempty"`
// ClusterControllerResyncSecond is the resync period used by cluster controller.
ClusterControllerResyncSecond time.Duration `json:"clusterControllerResyncSecond,omitempty" yaml:"clusterControllerResyncSecond"`
}
// NewOptions() returns a default nil options
func NewOptions() *Options {
return &Options{
Enable: false,
EnableFederation: false,
ProxyPublishAddress: "",
ProxyPublishService: "",
AgentImage: "kubesphere/tower:v1.0",
Enable: false,
EnableFederation: false,
ProxyPublishAddress: "",
ProxyPublishService: "",
AgentImage: "kubesphere/tower:v1.0",
ClusterControllerResyncSecond: DefaultResyncPeriod,
}
}
@@ -67,4 +77,7 @@ func (o *Options) AddFlags(fs *pflag.FlagSet, s *Options) {
fs.StringVar(&o.AgentImage, "agent-image", s.AgentImage, ""+
"This field is used when generating deployment yaml for agent.")
fs.DurationVar(&o.ClusterControllerResyncSecond, "cluster-controller-resync-second", s.ClusterControllerResyncSecond,
"Cluster controller resync second to sync cluster resource.")
}