...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/multicluster/remotechaosmonitor/fx.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/multicluster/remotechaosmonitor

     1  // Copyright 2022 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 remotechaosmonitor
    17  
    18  import (
    19  	"github.com/go-logr/logr"
    20  	"go.uber.org/fx"
    21  	ctrl "sigs.k8s.io/controller-runtime"
    22  	"sigs.k8s.io/controller-runtime/pkg/client"
    23  
    24  	"github.com/chaos-mesh/chaos-mesh/controllers/config"
    25  	"github.com/chaos-mesh/chaos-mesh/controllers/types"
    26  	"github.com/chaos-mesh/chaos-mesh/controllers/utils/builder"
    27  )
    28  
    29  type Params struct {
    30  	fx.In
    31  
    32  	Mgr          ctrl.Manager
    33  	ManageClient client.Client `name:"manage-client"`
    34  	LocalClient  client.Client
    35  	ClusterName  string `name:"cluster-name"`
    36  	Logger       logr.Logger
    37  	Objs         []types.Object `group:"objs"`
    38  }
    39  
    40  func Bootstrap(params Params) error {
    41  	logger := params.Logger
    42  	mgr := params.Mgr
    43  	objs := params.Objs
    44  	setupLog := logger.WithName("setup-remotechaosmonitor")
    45  
    46  	for _, obj := range objs {
    47  		name := obj.Name + "-remotechaos-monitor"
    48  
    49  		if !config.ShouldSpawnController(name) {
    50  			return nil
    51  		}
    52  
    53  		setupLog.Info("setting up controller", "resource-name", obj.Name)
    54  
    55  		// TODO: filter out chaos controlled by remote chaos
    56  		builder := builder.Default(mgr).
    57  			For(obj.Object).
    58  			Named(obj.Name + "-remotechaos-monitor")
    59  
    60  		err := builder.Complete(New(obj.Object, params.ManageClient, params.ClusterName, params.LocalClient, params.Logger))
    61  
    62  		if err != nil {
    63  			return err
    64  		}
    65  
    66  	}
    67  	return nil
    68  }
    69  
    70  var Module = fx.Options(
    71  	fx.Invoke(Bootstrap),
    72  )
    73