...

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

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/podiochaos

     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 podiochaos
    17  
    18  import (
    19  	"reflect"
    20  
    21  	"github.com/go-logr/logr"
    22  	ctrl "sigs.k8s.io/controller-runtime"
    23  	"sigs.k8s.io/controller-runtime/pkg/client"
    24  	"sigs.k8s.io/controller-runtime/pkg/event"
    25  	"sigs.k8s.io/controller-runtime/pkg/predicate"
    26  
    27  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    28  	"github.com/chaos-mesh/chaos-mesh/controllers/config"
    29  	"github.com/chaos-mesh/chaos-mesh/controllers/utils/builder"
    30  	"github.com/chaos-mesh/chaos-mesh/controllers/utils/chaosdaemon"
    31  )
    32  
    33  func Bootstrap(mgr ctrl.Manager, client client.Client, logger logr.Logger, b *chaosdaemon.ChaosDaemonClientBuilder) error {
    34  	if !config.ShouldSpawnController("podiochaos") {
    35  		return nil
    36  	}
    37  
    38  	return builder.Default(mgr).
    39  		For(&v1alpha1.PodIOChaos{}).
    40  		Named("podiochaos").
    41  		WithEventFilter(predicate.Funcs{
    42  			UpdateFunc: func(e event.UpdateEvent) bool {
    43  				oldObj := e.ObjectOld.(*v1alpha1.PodIOChaos)
    44  				newObj := e.ObjectNew.(*v1alpha1.PodIOChaos)
    45  
    46  				return !reflect.DeepEqual(oldObj.Spec, newObj.Spec)
    47  			},
    48  		}).
    49  		Complete(&Reconciler{
    50  			Client:                   client,
    51  			Log:                      logger.WithName("podiochaos"),
    52  			Recorder:                 mgr.GetEventRecorderFor("podiochaos"),
    53  			ChaosDaemonClientBuilder: b,
    54  		})
    55  }
    56