/
filter.go
74 lines (62 loc) · 1.55 KB
/
filter.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package controllerlib
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
)
type Filter interface {
Add(obj metav1.Object) bool
Update(oldObj, newObj metav1.Object) bool
Delete(obj metav1.Object) bool
Parent(obj metav1.Object) Key
}
var _ Filter = FilterFuncs{}
type ParentFunc func(obj metav1.Object) Key
type FilterFuncs struct {
ParentFunc ParentFunc
AddFunc func(obj metav1.Object) bool
UpdateFunc func(oldObj, newObj metav1.Object) bool
DeleteFunc func(obj metav1.Object) bool
}
func (f FilterFuncs) Parent(obj metav1.Object) Key {
if f.ParentFunc == nil {
return Key{
Namespace: obj.GetNamespace(),
Name: obj.GetName(),
}
}
return f.ParentFunc(obj)
}
func (f FilterFuncs) Add(obj metav1.Object) bool {
if f.AddFunc == nil {
return false
}
return f.AddFunc(obj)
}
func (f FilterFuncs) Update(oldObj, newObj metav1.Object) bool {
if f.UpdateFunc == nil {
return false
}
return f.UpdateFunc(oldObj, newObj)
}
func (f FilterFuncs) Delete(obj metav1.Object) bool {
if f.DeleteFunc == nil {
return false
}
return f.DeleteFunc(obj)
}
func FilterByNames(parentFunc ParentFunc, names ...string) Filter {
set := sets.NewString(names...)
has := func(obj metav1.Object) bool {
return set.Has(obj.GetName())
}
return FilterFuncs{
ParentFunc: parentFunc,
AddFunc: has,
UpdateFunc: func(oldObj, newObj metav1.Object) bool {
return has(newObj)
},
DeleteFunc: has,
}
}