github.com/banzaicloud/operator-tools@v0.28.10/pkg/reconciler/predicates.go (about)

     1  // Copyright © 2020 Banzai Cloud
     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  package reconciler
    16  
    17  import (
    18  	"sigs.k8s.io/controller-runtime/pkg/event"
    19  	"sigs.k8s.io/controller-runtime/pkg/predicate"
    20  
    21  	"github.com/banzaicloud/k8s-objectmatcher/patch"
    22  	"github.com/banzaicloud/operator-tools/pkg/types"
    23  )
    24  
    25  type SkipCreatePredicate struct {
    26  	predicate.Funcs
    27  }
    28  
    29  func (SkipCreatePredicate) Create(e event.CreateEvent) bool {
    30  	return false
    31  }
    32  
    33  type SkipUpdatePredicate struct {
    34  	predicate.Funcs
    35  }
    36  
    37  func (SkipUpdatePredicate) Update(e event.UpdateEvent) bool {
    38  	return false
    39  }
    40  
    41  type SkipDeletePredicate struct {
    42  	predicate.Funcs
    43  }
    44  
    45  func (SkipDeletePredicate) Delete(e event.DeleteEvent) bool {
    46  	return false
    47  }
    48  
    49  type PendingStatusPredicate struct {
    50  	predicate.Funcs
    51  }
    52  
    53  func (PendingStatusPredicate) Update(e event.UpdateEvent) bool {
    54  	if o, ok := e.ObjectNew.(interface {
    55  		IsAnyInState(state types.ReconcileStatus) bool
    56  	}); ok {
    57  		return o.IsAnyInState(types.ReconcileStatusPending)
    58  	}
    59  
    60  	if o, ok := e.ObjectNew.(interface {
    61  		IsPending() bool
    62  	}); ok {
    63  		return o.IsPending()
    64  	}
    65  
    66  	return false
    67  }
    68  
    69  type SpecChangePredicate struct {
    70  	predicate.Funcs
    71  
    72  	patchMaker       patch.Maker
    73  	calculateOptions []patch.CalculateOption
    74  }
    75  
    76  func (p SpecChangePredicate) Update(e event.UpdateEvent) bool {
    77  	if p.patchMaker == nil {
    78  		p.patchMaker = patch.DefaultPatchMaker
    79  	}
    80  	if p.calculateOptions == nil {
    81  		p.calculateOptions = []patch.CalculateOption{patch.IgnoreStatusFields(), IgnoreManagedFields()}
    82  	}
    83  
    84  	oldRV := e.ObjectOld.GetResourceVersion()
    85  	e.ObjectOld.SetResourceVersion(e.ObjectNew.GetResourceVersion())
    86  	defer e.ObjectOld.SetResourceVersion(oldRV)
    87  
    88  	patchResult, err := p.patchMaker.Calculate(e.ObjectOld, e.ObjectNew, p.calculateOptions...)
    89  	if err != nil {
    90  		return true
    91  	} else if patchResult.IsEmpty() {
    92  		return false
    93  	}
    94  
    95  	return true
    96  }