diff --git a/cmd/controller-manager/app/controllers.go b/cmd/controller-manager/app/controllers.go index d24f02f81d96242ef99c79b82cec83d87f68e12e..67d009e7617bfbca050c94a87c1faa4518cb986b 100644 --- a/cmd/controller-manager/app/controllers.go +++ b/cmd/controller-manager/app/controllers.go @@ -48,6 +48,7 @@ import ( "kubesphere.io/kubesphere/pkg/simple/client/devops" "kubesphere.io/kubesphere/pkg/simple/client/k8s" ldapclient "kubesphere.io/kubesphere/pkg/simple/client/ldap" + "kubesphere.io/kubesphere/pkg/simple/client/multicluster" "kubesphere.io/kubesphere/pkg/simple/client/network" ippoolclient "kubesphere.io/kubesphere/pkg/simple/client/network/ippool" "kubesphere.io/kubesphere/pkg/simple/client/openpitrix" @@ -66,7 +67,7 @@ func addControllers( options *k8s.KubernetesOptions, authenticationOptions *authoptions.AuthenticationOptions, openpitrixClient openpitrix.Client, - multiClusterEnabled bool, + multiClusterOptions *multicluster.Options, networkOptions *network.Options, serviceMeshEnabled bool, kubectlImage string, @@ -76,6 +77,8 @@ func addControllers( istioInformer := informerFactory.IstioSharedInformerFactory() kubesphereInformer := informerFactory.KubeSphereSharedInformerFactory() + multiClusterEnabled := multiClusterOptions.Enable + var vsController, drController manager.Runnable if serviceMeshEnabled { vsController = virtualservice.NewVirtualServiceController(kubernetesInformer.Core().V1().Services(), @@ -229,7 +232,8 @@ func addControllers( client.Config(), kubesphereInformer.Cluster().V1alpha1().Clusters(), client.KubeSphere().ClusterV1alpha1().Clusters(), - openpitrixClient) + openpitrixClient, + multiClusterOptions.ClusterControllerResyncSecond) } var nsnpController manager.Runnable diff --git a/cmd/controller-manager/app/server.go b/cmd/controller-manager/app/server.go index d6c85abb49227f9b3bd41c5715282a8c7eafd01b..d0fc7399b77e8bb1c264e0d6fb0c392eb670d08a 100644 --- a/cmd/controller-manager/app/server.go +++ b/cmd/controller-manager/app/server.go @@ -251,7 +251,7 @@ func run(s *options.KubeSphereControllerManagerOptions, stopCh <-chan struct{}) s.KubernetesOptions, s.AuthenticationOptions, openpitrixClient, - s.MultiClusterOptions.Enable, + s.MultiClusterOptions, s.NetworkOptions, servicemeshEnabled, s.AuthenticationOptions.KubectlImage, stopCh); err != nil { diff --git a/pkg/controller/cluster/cluster_controller.go b/pkg/controller/cluster/cluster_controller.go index 4ce14ba7eb365f8a4e87bfa8d3b4055d3e705f02..c138e6cae1cd4a3dbe75175ba03031f0b3123d0c 100644 --- a/pkg/controller/cluster/cluster_controller.go +++ b/pkg/controller/cluster/cluster_controller.go @@ -158,6 +158,8 @@ type clusterController struct { mu sync.RWMutex clusterMap map[string]*clusterData + + resyncPeriod time.Duration } func NewClusterController( @@ -166,6 +168,7 @@ func NewClusterController( clusterInformer clusterinformer.ClusterInformer, clusterClient clusterclient.ClusterInterface, openpitrixClient openpitrix.Client, + resyncPeriod time.Duration, ) *clusterController { broadcaster := record.NewBroadcaster() @@ -185,23 +188,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 } @@ -227,15 +225,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 @@ -355,6 +349,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) diff --git a/pkg/simple/client/multicluster/options.go b/pkg/simple/client/multicluster/options.go index bde77123091f0494dba150d6faaa39e247d5dffc..7c31cc3fd3dbb8f755a31083c989981798e4e1d7 100644 --- a/pkg/simple/client/multicluster/options.go +++ b/pkg/simple/client/multicluster/options.go @@ -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.") }