github.com/dmvolod/operator-sdk@v0.8.2/pkg/predicate/predicate.go (about)

     1  // Copyright 2018 The Operator-SDK 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  package predicate
    16  
    17  import (
    18  	"sigs.k8s.io/controller-runtime/pkg/event"
    19  	"sigs.k8s.io/controller-runtime/pkg/predicate"
    20  	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
    21  )
    22  
    23  var log = logf.Log.WithName("predicate").WithName("eventFilters")
    24  
    25  // GenerationChangedPredicate implements a default update predicate function on generation change
    26  // (adapted from sigs.k8s.io/controller-runtime/pkg/predicate/predicate.ResourceVersionChangedPredicate)
    27  type GenerationChangedPredicate struct {
    28  	predicate.Funcs
    29  }
    30  
    31  // Update implements default UpdateEvent filter for validating generation change
    32  func (GenerationChangedPredicate) Update(e event.UpdateEvent) bool {
    33  	if e.MetaOld == nil {
    34  		log.Error(nil, "Update event has no old metadata", "event", e)
    35  		return false
    36  	}
    37  	if e.ObjectOld == nil {
    38  		log.Error(nil, "Update event has no old runtime object to update", "event", e)
    39  		return false
    40  	}
    41  	if e.ObjectNew == nil {
    42  		log.Error(nil, "Update event has no new runtime object for update", "event", e)
    43  		return false
    44  	}
    45  	if e.MetaNew == nil {
    46  		log.Error(nil, "Update event has no new metadata", "event", e)
    47  		return false
    48  	}
    49  	if e.MetaNew.GetGeneration() == e.MetaOld.GetGeneration() {
    50  		return false
    51  	}
    52  	return true
    53  }