...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package manager
17
18 import (
19 "context"
20 "fmt"
21 "os"
22
23 "go.uber.org/fx"
24 "k8s.io/client-go/rest"
25 ctrl "sigs.k8s.io/controller-runtime"
26
27 ccfg "github.com/chaos-mesh/chaos-mesh/controllers/config"
28 )
29
30 func NewTestManager(lc fx.Lifecycle, options *ctrl.Options, cfg *rest.Config) (ctrl.Manager, error) {
31 if ccfg.ControllerCfg.QPS > 0 {
32 cfg.QPS = ccfg.ControllerCfg.QPS
33 }
34 if ccfg.ControllerCfg.Burst > 0 {
35 cfg.Burst = ccfg.ControllerCfg.Burst
36 }
37
38 mgr, err := ctrl.NewManager(cfg, *options)
39 if err != nil {
40 return nil, err
41 }
42
43 ctx, cancelFunc := context.WithCancel(context.TODO())
44 lc.Append(fx.Hook{
45 OnStart: func(context.Context) error {
46 fmt.Println("Starting manager")
47 go func() {
48 if err := mgr.Start(ctx); err != nil {
49 fmt.Println(err)
50 os.Exit(1)
51 }
52 }()
53 return nil
54 },
55 OnStop: func(ctx context.Context) error {
56 fmt.Println("Stopping manager")
57 cancelFunc()
58 return nil
59 },
60 })
61 return mgr, nil
62 }
63