Add informers and internal go routines in manager (#2870)
* used manager's Add function in more places * exit when ctx.Done is received in archivePruner go routines * fix manager tests * fix data race * added more gpm function in manager and removed manager from a util function * closed unused channel and stopped ticker after context is done * added log statements * used context.Done inside function instead of stopper channel
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
k8sCache "k8s.io/client-go/tools/cache"
|
||||
)
|
||||
|
||||
var _ Interface = &GroupManager{}
|
||||
@@ -15,6 +17,8 @@ type Interface interface {
|
||||
// and will also remove the "function" from the list when it completes
|
||||
Add(ctx context.Context, function func(context.Context))
|
||||
|
||||
AddInformers(ctx context.Context, informers map[string]k8sCache.SharedIndexInformer)
|
||||
|
||||
// Wait blocks the execution of the process until all the go routines in the manager are completed.
|
||||
Wait()
|
||||
|
||||
@@ -39,6 +43,15 @@ func (g *GroupManager) Add(ctx context.Context, f func(context.Context)) {
|
||||
}()
|
||||
}
|
||||
|
||||
func (g *GroupManager) AddInformers(ctx context.Context, informers map[string]k8sCache.SharedIndexInformer) {
|
||||
for _, informer := range informers {
|
||||
informer := informer
|
||||
g.Add(ctx, func(ctxArg context.Context) {
|
||||
informer.Run(ctxArg.Done())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GroupManager) Wait() {
|
||||
g.wg.Wait()
|
||||
}
|
||||
|
||||
@@ -53,6 +53,8 @@ func TestAddAndWaitWithTimeout(t *testing.T) {
|
||||
err := mgr.WaitWithTimeout(1 * time.Second)
|
||||
require.NotNil(t, err, "manager WaitWithTimeout did not return an error when timeout exceeded")
|
||||
|
||||
mgr = New()
|
||||
|
||||
expectedValue := 11
|
||||
mgr.Add(context.Background(), func(ctx context.Context) {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
@@ -10,9 +10,12 @@ import (
|
||||
"k8s.io/client-go/kubernetes/fake"
|
||||
|
||||
"github.com/fission/fission/pkg/utils/loggerfactory"
|
||||
"github.com/fission/fission/pkg/utils/manager"
|
||||
)
|
||||
|
||||
func TestServiceAccountCheck(t *testing.T) {
|
||||
mgr := manager.New()
|
||||
defer mgr.Wait()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
kubernetesClient := fake.NewSimpleClientset()
|
||||
|
||||
Reference in New Issue
Block a user