...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/utils/test/manager/test_manager.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/utils/test/manager

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    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