...

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

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

     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 remotechaos
    17  
    18  import (
    19  	"fmt"
    20  
    21  	"github.com/go-logr/logr"
    22  	"go.uber.org/fx"
    23  	ctrl "sigs.k8s.io/controller-runtime"
    24  	"sigs.k8s.io/controller-runtime/pkg/client"
    25  	"sigs.k8s.io/controller-runtime/pkg/event"
    26  	"sigs.k8s.io/controller-runtime/pkg/predicate"
    27  
    28  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    29  	"github.com/chaos-mesh/chaos-mesh/controllers/config"
    30  	"github.com/chaos-mesh/chaos-mesh/controllers/multicluster/clusterregistry"
    31  	"github.com/chaos-mesh/chaos-mesh/controllers/types"
    32  	"github.com/chaos-mesh/chaos-mesh/controllers/utils/builder"
    33  )
    34  
    35  type Params struct {
    36  	fx.In
    37  
    38  	Mgr      ctrl.Manager
    39  	Client   client.Client
    40  	Logger   logr.Logger
    41  	Objs     []types.Object `group:"objs"`
    42  	Registry *clusterregistry.RemoteClusterRegistry
    43  }
    44  
    45  func Bootstrap(params Params) error {
    46  	logger := params.Logger
    47  	mgr := params.Mgr
    48  	objs := params.Objs
    49  	client := params.Client
    50  	registry := params.Registry
    51  	setupLog := logger.WithName("setup-remotechaos")
    52  
    53  	for _, obj := range objs {
    54  		name := obj.Name + "-remote-apply"
    55  		if !config.ShouldSpawnController(name) {
    56  			return nil
    57  		}
    58  
    59  		setupLog.Info("setting up controller", "resource-name", obj.Name)
    60  
    61  		builder := builder.Default(mgr).
    62  			For(obj.Object).
    63  			Named(obj.Name + "-remotechaos").
    64  			WithEventFilter(remotePredicates)
    65  		err := builder.Complete(&Reconciler{
    66  			Client: client,
    67  			Log:    logger.WithName("remotechaos"),
    68  
    69  			Object: obj.Object,
    70  
    71  			registry: registry,
    72  		})
    73  
    74  		if err != nil {
    75  			return err
    76  		}
    77  
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  // this controller will only create or delete the remote chaos
    84  var remotePredicates = predicate.Funcs{
    85  	CreateFunc: func(e event.CreateEvent) bool {
    86  		obj, ok := e.Object.DeepCopyObject().(v1alpha1.RemoteObject)
    87  		if !ok {
    88  			fmt.Println("not a remote object")
    89  			return false
    90  		}
    91  
    92  		if obj.GetRemoteCluster() == "" {
    93  			return false
    94  		}
    95  
    96  		return true
    97  	},
    98  	UpdateFunc: func(e event.UpdateEvent) bool {
    99  		// TODO: consider carefully whether we'll need to handle
   100  		// delete event
   101  		obj, ok := e.ObjectNew.DeepCopyObject().(v1alpha1.RemoteObject)
   102  		if !ok {
   103  			fmt.Println("not a remote object")
   104  			return false
   105  		}
   106  
   107  		if obj.GetRemoteCluster() == "" {
   108  			fmt.Println("remote cluster is empty")
   109  			return false
   110  		}
   111  
   112  		return true
   113  	},
   114  	DeleteFunc: func(e event.DeleteEvent) bool {
   115  		// TODO: consider carefully whether we'll need to handle
   116  		// delete event
   117  		obj, ok := e.Object.DeepCopyObject().(v1alpha1.RemoteObject)
   118  		if !ok {
   119  			fmt.Println("not a remote object")
   120  			return false
   121  		}
   122  
   123  		if obj.GetRemoteCluster() == "" {
   124  			fmt.Println("remote cluster is empty")
   125  			return false
   126  		}
   127  
   128  		return true
   129  	},
   130  	GenericFunc: func(e event.GenericEvent) bool {
   131  		return false
   132  	},
   133  }
   134