-
Notifications
You must be signed in to change notification settings - Fork 4
/
annotation.go
52 lines (43 loc) · 1.62 KB
/
annotation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019 Datadog, Inc.
package predicate
import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)
var _ predicate.Predicate = AnnotationPredicate{}
// AnnotationPredicate Predicate interface implementation that is looking
// for a specific Annotation key in the object metadata
// if Value is also set, validate that the Value is also equal
type AnnotationPredicate struct {
Key string
Value string
}
// Create returns true if the Create event should be processed
func (a AnnotationPredicate) Create(e event.CreateEvent) bool {
return a.isAnnotationKeyPresent(e.Meta)
}
// Delete returns true if the Delete event should be processed
func (a AnnotationPredicate) Delete(e event.DeleteEvent) bool {
return a.isAnnotationKeyPresent(e.Meta)
}
// Update returns true if the Update event should be processed
func (a AnnotationPredicate) Update(e event.UpdateEvent) bool {
return a.isAnnotationKeyPresent(e.MetaNew) || a.isAnnotationKeyPresent(e.MetaOld)
}
// Generic returns true if the Generic event should be processed
func (a AnnotationPredicate) Generic(e event.GenericEvent) bool {
return a.isAnnotationKeyPresent(e.Meta)
}
func (a AnnotationPredicate) isAnnotationKeyPresent(obj v1.Object) bool {
if val, ok := obj.GetAnnotations()[a.Key]; ok {
if a.Value != "" && val != a.Value {
return false
}
return true
}
return false
}