From 5500e93b5f7d11abe0acada7249a411c0c1fbb20 Mon Sep 17 00:00:00 2001 From: "Roland.Ma" Date: Mon, 9 Nov 2020 01:50:11 +0000 Subject: [PATCH] update comments Signed-off-by: Roland.Ma --- pkg/controller/group/group_controller.go | 58 ++----------------- .../groupbinding/groupbinding_controller.go | 56 ++---------------- 2 files changed, 11 insertions(+), 103 deletions(-) diff --git a/pkg/controller/group/group_controller.go b/pkg/controller/group/group_controller.go index 02886be4f..49fa942dd 100644 --- a/pkg/controller/group/group_controller.go +++ b/pkg/controller/group/group_controller.go @@ -42,9 +42,7 @@ import ( ) const ( - // SuccessSynced is used as part of the Event 'reason' when a Foo is synced - successSynced = "Synced" - // is synced successfully + successSynced = "Synced" messageResourceSynced = "Group synced successfully" controllerName = "groupbinding-controller" finalizer = "finalizers.kubesphere.io/groups" @@ -57,22 +55,12 @@ type Controller struct { groupInformer iamv1alpha2informers.GroupInformer groupLister iamv1alpha1listers.GroupLister groupSynced cache.InformerSynced - // workqueue is a rate limited work queue. This is used to queue work to be - // processed instead of performing it as soon as a change happens. This - // means we can ensure we only process a fixed amount of resources at a - // time, and makes it easy to ensure we are never processing the same item - // simultaneously in two different workers. - workqueue workqueue.RateLimitingInterface - // recorder is an event recorder for recording Event resources to the - // Kubernetes API. - recorder record.EventRecorder + workqueue workqueue.RateLimitingInterface + recorder record.EventRecorder } // NewController creates Group Controller instance func NewController(k8sClient kubernetes.Interface, ksClient kubesphere.Interface, groupInformer iamv1alpha2informers.GroupInformer) *Controller { - // Create event broadcaster - // Add sample-controller types to the default Kubernetes Scheme so Events can be - // logged for sample-controller types. klog.V(4).Info("Creating event broadcaster") eventBroadcaster := record.NewBroadcaster() @@ -103,20 +91,14 @@ func (c *Controller) Run(threadiness int, stopCh <-chan struct{}) error { defer utilruntime.HandleCrash() defer c.workqueue.ShutDown() - // Start the informer factories to begin populating the informer caches klog.Info("Starting Group controller") - - // Wait for the caches to be synced before starting workers klog.Info("Waiting for informer caches to sync") - synced := []cache.InformerSynced{c.groupSynced} - if ok := cache.WaitForCacheSync(stopCh, synced...); !ok { return fmt.Errorf("failed to wait for caches to sync") } klog.Info("Starting workers") - // Launch two workers to process Foo resources for i := 0; i < threadiness; i++ { go wait.Until(c.runWorker, time.Second, stopCh) } @@ -148,40 +130,20 @@ func (c *Controller) processNextWorkItem() bool { if shutdown { return false } - - // We wrap this block in a func so we can defer c.workqueue.Done. err := func(obj interface{}) error { - // We call Done here so the workqueue knows we have finished - // processing this item. We also must remember to call Forget if we - // do not want this work item being re-queued. For example, we do - // not call Forget if a transient error occurs, instead the item is - // put back on the workqueue and attempted again after a back-off - // period. defer c.workqueue.Done(obj) var key string var ok bool - // We expect strings to come off the workqueue. These are of the - // form namespace/name. We do this as the delayed nature of the - // workqueue means the items in the informer cache may actually be - // more up to date that when the item was initially put onto the - // workqueue. + if key, ok = obj.(string); !ok { - // As the item in the workqueue is actually invalid, we call - // Forget here else we'd go into a loop of attempting to - // process a work item that is invalid. c.workqueue.Forget(obj) utilruntime.HandleError(fmt.Errorf("expected string in workqueue but got %#v", obj)) return nil } - // Run the reconcile, passing it the namespace/name string of the - // Foo resource to be synced. if err := c.reconcile(key); err != nil { - // Put the item back on the workqueue to handle any transient errors. c.workqueue.AddRateLimited(key) return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error()) } - // Finally, if no error occurs we Forget this item so it does not - // get queued again until another change happens. c.workqueue.Forget(obj) klog.Infof("Successfully synced %s:%s", "key", key) return nil @@ -195,15 +157,11 @@ func (c *Controller) processNextWorkItem() bool { return true } -// syncHandler compares the actual state with the desired, and attempts to -// converge the two. It then updates the Status block of the Foo resource -// with the current status of the resource. +// reconcile handles Group informer events, clear up related reource when group is being deleted. func (c *Controller) reconcile(key string) error { group, err := c.groupLister.Get(key) if err != nil { - // The user may no longer exist, in which case we stop - // processing. if errors.IsNotFound(err) { utilruntime.HandleError(fmt.Errorf("group '%s' in work queue no longer exists", key)) return nil @@ -212,8 +170,6 @@ func (c *Controller) reconcile(key string) error { return err } if group.ObjectMeta.DeletionTimestamp.IsZero() { - // The object is not being deleted, so if it does not have our finalizer, - // then lets add the finalizer and update the object. if !sliceutil.HasString(group.Finalizers, finalizer) { group.ObjectMeta.Finalizers = append(group.ObjectMeta.Finalizers, finalizer) @@ -236,7 +192,6 @@ func (c *Controller) reconcile(key string) error { return err } - // remove our finalizer from the list and update it. group.Finalizers = sliceutil.RemoveString(group.ObjectMeta.Finalizers, func(item string) bool { return item == finalizer }) @@ -245,7 +200,6 @@ func (c *Controller) reconcile(key string) error { return err } } - // Our finalizer has finished, so the reconciler can do nothing. return nil } @@ -254,7 +208,7 @@ func (c *Controller) reconcile(key string) error { } func (c *Controller) Start(stopCh <-chan struct{}) error { - return c.Run(4, stopCh) + return c.Run(1, stopCh) } func (c *Controller) deleteGroupBindings(group *iam1alpha2.Group) error { diff --git a/pkg/controller/groupbinding/groupbinding_controller.go b/pkg/controller/groupbinding/groupbinding_controller.go index 568ba122f..0cc050db9 100644 --- a/pkg/controller/groupbinding/groupbinding_controller.go +++ b/pkg/controller/groupbinding/groupbinding_controller.go @@ -42,9 +42,7 @@ import ( ) const ( - // SuccessSynced is used as part of the Event 'reason' when a Foo is synced - successSynced = "Synced" - // is synced successfully + successSynced = "Synced" messageResourceSynced = "GroupBinding synced successfully" controllerName = "groupbinding-controller" finalizer = "finalizers.kubesphere.io/groupsbindings" @@ -57,23 +55,12 @@ type Controller struct { groupBindingInformer iamv1alpha2informers.GroupBindingInformer groupBindingLister iamv1alpha2listers.GroupBindingLister groupBindingSynced cache.InformerSynced - // workqueue is a rate limited work queue. This is used to queue work to be - // processed instead of performing it as soon as a change happens. This - // means we can ensure we only process a fixed amount of resources at a - // time, and makes it easy to ensure we are never processing the same item - // simultaneously in two different workers. - workqueue workqueue.RateLimitingInterface - // recorder is an event recorder for recording Event resources to the - // Kubernetes API. - recorder record.EventRecorder + workqueue workqueue.RateLimitingInterface + recorder record.EventRecorder } // NewController creates GroupBinding Controller instance func NewController(k8sClient kubernetes.Interface, ksClient kubesphere.Interface, groupBindingInformer iamv1alpha2informers.GroupBindingInformer) *Controller { - // Create event broadcaster - // Add sample-controller types to the default Kubernetes Scheme so Events can be - // logged for sample-controller types. - klog.V(4).Info("Creating event broadcaster") eventBroadcaster := record.NewBroadcaster() eventBroadcaster.StartLogging(klog.Infof) @@ -103,10 +90,7 @@ func (c *Controller) Run(threadiness int, stopCh <-chan struct{}) error { defer utilruntime.HandleCrash() defer c.workqueue.ShutDown() - // Start the informer factories to begin populating the informer caches klog.Info("Starting GroupBinding controller") - - // Wait for the caches to be synced before starting workers klog.Info("Waiting for informer caches to sync") synced := []cache.InformerSynced{c.groupBindingSynced} @@ -116,7 +100,6 @@ func (c *Controller) Run(threadiness int, stopCh <-chan struct{}) error { } klog.Info("Starting workers") - // Launch two workers to process Foo resources for i := 0; i < threadiness; i++ { go wait.Until(c.runWorker, time.Second, stopCh) } @@ -149,39 +132,19 @@ func (c *Controller) processNextWorkItem() bool { return false } - // We wrap this block in a func so we can defer c.workqueue.Done. err := func(obj interface{}) error { - // We call Done here so the workqueue knows we have finished - // processing this item. We also must remember to call Forget if we - // do not want this work item being re-queued. For example, we do - // not call Forget if a transient error occurs, instead the item is - // put back on the workqueue and attempted again after a back-off - // period. defer c.workqueue.Done(obj) var key string var ok bool - // We expect strings to come off the workqueue. These are of the - // form namespace/name. We do this as the delayed nature of the - // workqueue means the items in the informer cache may actually be - // more up to date that when the item was initially put onto the - // workqueue. if key, ok = obj.(string); !ok { - // As the item in the workqueue is actually invalid, we call - // Forget here else we'd go into a loop of attempting to - // process a work item that is invalid. c.workqueue.Forget(obj) utilruntime.HandleError(fmt.Errorf("expected string in workqueue but got %#v", obj)) return nil } - // Run the reconcile, passing it the namespace/name string of the - // Foo resource to be synced. if err := c.reconcile(key); err != nil { - // Put the item back on the workqueue to handle any transient errors. c.workqueue.AddRateLimited(key) return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error()) } - // Finally, if no error occurs we Forget this item so it does not - // get queued again until another change happens. c.workqueue.Forget(obj) klog.Infof("Successfully synced %s:%s", "key", key) return nil @@ -195,15 +158,11 @@ func (c *Controller) processNextWorkItem() bool { return true } -// syncHandler compares the actual state with the desired, and attempts to -// converge the two. It then updates the Status block of the Foo resource -// with the current status of the resource. +// reconcile handles GroupBinding informer events, it updates user's Groups property with the current GroupBinding. func (c *Controller) reconcile(key string) error { groupBinding, err := c.groupBindingLister.Get(key) if err != nil { - // The user may no longer exist, in which case we stop - // processing. if errors.IsNotFound(err) { utilruntime.HandleError(fmt.Errorf("groupbinding '%s' in work queue no longer exists", key)) return nil @@ -212,9 +171,6 @@ func (c *Controller) reconcile(key string) error { return err } if groupBinding.ObjectMeta.DeletionTimestamp.IsZero() { - - // The object is not being deleted, so if it does not have our finalizer, - // then lets add the finalizer and update the object. if !sliceutil.HasString(groupBinding.Finalizers, finalizer) { groupBinding.ObjectMeta.Finalizers = append(groupBinding.ObjectMeta.Finalizers, finalizer) if groupBinding, err = c.ksClient.IamV1alpha2().GroupBindings().Update(groupBinding); err != nil { @@ -231,7 +187,6 @@ func (c *Controller) reconcile(key string) error { return err } - // remove our finalizer from the list and update it. groupBinding.Finalizers = sliceutil.RemoveString(groupBinding.ObjectMeta.Finalizers, func(item string) bool { return item == finalizer }) @@ -240,7 +195,6 @@ func (c *Controller) reconcile(key string) error { return err } } - // Our finalizer has finished, so the reconciler can do nothing. return nil } @@ -254,7 +208,7 @@ func (c *Controller) reconcile(key string) error { } func (c *Controller) Start(stopCh <-chan struct{}) error { - return c.Run(4, stopCh) + return c.Run(2, stopCh) } func (c *Controller) unbindUser(groupBinding *iamv1alpha2.GroupBinding) error {