...

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