...

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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package manager
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"os"
    20  
    21  	"go.uber.org/fx"
    22  	"k8s.io/client-go/rest"
    23  	ctrl "sigs.k8s.io/controller-runtime"
    24  
    25  	ccfg "github.com/chaos-mesh/chaos-mesh/controllers/config"
    26  )
    27  
    28  func NewTestManager(lc fx.Lifecycle, options *ctrl.Options, cfg *rest.Config) (ctrl.Manager, error) {
    29  	if ccfg.ControllerCfg.QPS > 0 {
    30  		cfg.QPS = ccfg.ControllerCfg.QPS
    31  	}
    32  	if ccfg.ControllerCfg.Burst > 0 {
    33  		cfg.Burst = ccfg.ControllerCfg.Burst
    34  	}
    35  	ch := make(chan struct{})
    36  
    37  	mgr, err := ctrl.NewManager(cfg, *options)
    38  
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	lc.Append(fx.Hook{
    43  		OnStart: func(context.Context) error {
    44  			fmt.Println("Starting manager")
    45  			go func() {
    46  				if err := mgr.Start(ch); err != nil {
    47  					fmt.Println(err)
    48  					os.Exit(1)
    49  				}
    50  			}()
    51  			return nil
    52  		},
    53  		OnStop: func(ctx context.Context) error {
    54  			fmt.Println("Stopping manager")
    55  			close(ch)
    56  			return nil
    57  		},
    58  	})
    59  	return mgr, nil
    60  }
    61