generated from cybozu-go/neco-template
/
indexing.go
44 lines (39 loc) · 1.31 KB
/
indexing.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
package indexing
import (
"context"
"github.com/cybozu-go/accurate/pkg/constants"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
// SetupIndexForResource sets up an indexer for a watched resource.
func SetupIndexForResource(ctx context.Context, mgr manager.Manager, res client.Object) error {
return mgr.GetFieldIndexer().IndexField(ctx, res, constants.PropagateKey, func(rawObj client.Object) []string {
val := rawObj.GetAnnotations()[constants.AnnPropagate]
if val == "" {
return nil
}
return []string{val, constants.PropagateAny}
})
}
// SetupIndexForNamespace sets up indexers for namespaces.
func SetupIndexForNamespace(ctx context.Context, mgr manager.Manager) error {
ns := &corev1.Namespace{}
err := mgr.GetFieldIndexer().IndexField(context.Background(), ns, constants.NamespaceParentKey, func(rawObj client.Object) []string {
parent := rawObj.GetLabels()[constants.LabelParent]
if parent == "" {
return nil
}
return []string{parent}
})
if err != nil {
return err
}
return mgr.GetFieldIndexer().IndexField(context.Background(), ns, constants.NamespaceTemplateKey, func(rawObj client.Object) []string {
tmpl := rawObj.GetLabels()[constants.LabelTemplate]
if tmpl == "" {
return nil
}
return []string{tmpl}
})
}