...

Source file src/edge-infra.dev/pkg/edge/monitoring/k8s/controllers/prometheusctl/predicates.go

Documentation: edge-infra.dev/pkg/edge/monitoring/k8s/controllers/prometheusctl

     1  package prometheusctl
     2  
     3  import (
     4  	"reflect"
     5  
     6  	monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
     7  
     8  	"edge-infra.dev/pkg/k8s/meta"
     9  
    10  	"sigs.k8s.io/controller-runtime/pkg/event"
    11  	"sigs.k8s.io/controller-runtime/pkg/predicate"
    12  )
    13  
    14  var (
    15  	prometheusKind = reflect.TypeOf(monitoringv1.Prometheus{}).Name()
    16  )
    17  
    18  func predicates(prometheus meta.NamespacedObjectReference) predicate.Funcs {
    19  	return predicate.Funcs{
    20  		UpdateFunc: func(e event.UpdateEvent) bool {
    21  			// process updates to the spec of Prometheus objects that we care about
    22  			if e.ObjectNew.GetObjectKind().GroupVersionKind().Kind == prometheusKind &&
    23  				e.ObjectOld.GetGeneration() != e.ObjectNew.GetGeneration() &&
    24  				e.ObjectNew.GetName() == prometheus.Name &&
    25  				e.ObjectNew.GetNamespace() == prometheus.Namespace {
    26  				return true
    27  			}
    28  
    29  			// otherwise, we want to reconcile `*Monitor` objects if their annotations
    30  			// change and they contain the annotation that we care about
    31  			if e.ObjectNew.GetObjectKind().GroupVersionKind().Kind != prometheusKind &&
    32  				e.ObjectNew.GetResourceVersion() != e.ObjectOld.GetResourceVersion() &&
    33  				e.ObjectNew.GetAnnotations()[allowedMetrics] != "" {
    34  				return true
    35  			}
    36  
    37  			return false
    38  		},
    39  		CreateFunc: func(e event.CreateEvent) bool {
    40  			// only process the prometheus that we care about
    41  			if e.Object.GetObjectKind().GroupVersionKind().Kind == prometheusKind &&
    42  				e.Object.GetName() == prometheus.Name &&
    43  				e.Object.GetNamespace() == prometheus.Namespace {
    44  				return true
    45  			}
    46  
    47  			// otherwise, we want to reconcile `*Monitor` objects if they have the annotation
    48  			// we care ab out
    49  			if e.Object.GetAnnotations()[allowedMetrics] != "" {
    50  				return true
    51  			}
    52  
    53  			return false
    54  		},
    55  		DeleteFunc: func(_ event.DeleteEvent) bool {
    56  			return true
    57  		},
    58  	}
    59  }
    60  

View as plain text