knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/reconciler/filter.go (about)

     1  /*
     2  Copyright 2020 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package reconciler
    18  
    19  import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    20  
    21  // AnnotationFilterFunc creates a FilterFunc only accepting objects with given annotation key and value
    22  func AnnotationFilterFunc(key, value string, allowUnset bool) func(interface{}) bool {
    23  	return func(obj interface{}) bool {
    24  		if mo, ok := obj.(metav1.Object); ok {
    25  			return mapHasOrDefault(mo.GetAnnotations(), key, value, allowUnset)
    26  		}
    27  		return false
    28  	}
    29  }
    30  
    31  // LabelExistsFilterFunc creates a FilterFunc only accepting objects which have a given label.
    32  func LabelExistsFilterFunc(label string) func(obj interface{}) bool {
    33  	return func(obj interface{}) bool {
    34  		if mo, ok := obj.(metav1.Object); ok {
    35  			labels := mo.GetLabels()
    36  			_, ok := labels[label]
    37  			return ok
    38  		}
    39  		return false
    40  	}
    41  }
    42  
    43  // LabelFilterFunc creates a FilterFunc only accepting objects where a label is set to a specific value.
    44  func LabelFilterFunc(label, value string, allowUnset bool) func(interface{}) bool {
    45  	return func(obj interface{}) bool {
    46  		if mo, ok := obj.(metav1.Object); ok {
    47  			return mapHasOrDefault(mo.GetLabels(), label, value, allowUnset)
    48  		}
    49  		return false
    50  	}
    51  }
    52  
    53  // NameFilterFunc creates a FilterFunc only accepting objects with the given name.
    54  func NameFilterFunc(name string) func(interface{}) bool {
    55  	return func(obj interface{}) bool {
    56  		if mo, ok := obj.(metav1.Object); ok {
    57  			return mo.GetName() == name
    58  		}
    59  		return false
    60  	}
    61  }
    62  
    63  // NamespaceFilterFunc creates a FilterFunc only accepting objects in the given namespace.
    64  func NamespaceFilterFunc(namespace string) func(interface{}) bool {
    65  	return func(obj interface{}) bool {
    66  		if mo, ok := obj.(metav1.Object); ok {
    67  			return mo.GetNamespace() == namespace
    68  		}
    69  		return false
    70  	}
    71  }
    72  
    73  // Not inverts the result of the predicate.
    74  func Not(f func(interface{}) bool) func(interface{}) bool {
    75  	return func(obj interface{}) bool {
    76  		return !f(obj)
    77  	}
    78  }
    79  
    80  // Or creates a FilterFunc which performs an OR of the passed in FilterFuncs
    81  func Or(funcs ...func(interface{}) bool) func(interface{}) bool {
    82  	return func(obj interface{}) bool {
    83  		for _, f := range funcs {
    84  			if f(obj) {
    85  				return true
    86  			}
    87  		}
    88  		return false
    89  	}
    90  }
    91  
    92  // ChainFilterFuncs creates a FilterFunc which performs an AND of the passed FilterFuncs.
    93  func ChainFilterFuncs(funcs ...func(interface{}) bool) func(interface{}) bool {
    94  	return func(obj interface{}) bool {
    95  		for _, f := range funcs {
    96  			if !f(obj) {
    97  				return false
    98  			}
    99  		}
   100  		return true
   101  	}
   102  }
   103  
   104  // mapHasOrDefault returns true if the map has the key and its value is equal to value.
   105  // If the key is not found, it returns defaultValue.
   106  func mapHasOrDefault(m map[string]string, key string, value string, defaultValue bool) bool {
   107  	val, ok := m[key]
   108  	if !ok {
   109  		return defaultValue
   110  	}
   111  	return val == value
   112  }