blob: 15acb168ef2af74ef8e40909176c53a2b392feb6 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package cache
18
19import (
20 "fmt"
21
22 "k8s.io/apimachinery/pkg/api/meta"
23 "k8s.io/apimachinery/pkg/util/sets"
24)
25
26// Indexer is a storage interface that lets you list objects using multiple indexing functions
27type Indexer interface {
28 Store
29 // Retrieve list of objects that match on the named indexing function
30 Index(indexName string, obj interface{}) ([]interface{}, error)
31 // IndexKeys returns the set of keys that match on the named indexing function.
32 IndexKeys(indexName, indexKey string) ([]string, error)
33 // ListIndexFuncValues returns the list of generated values of an Index func
34 ListIndexFuncValues(indexName string) []string
35 // ByIndex lists object that match on the named indexing function with the exact key
36 ByIndex(indexName, indexKey string) ([]interface{}, error)
37 // GetIndexer return the indexers
38 GetIndexers() Indexers
39
40 // AddIndexers adds more indexers to this store. If you call this after you already have data
41 // in the store, the results are undefined.
42 AddIndexers(newIndexers Indexers) error
43}
44
45// IndexFunc knows how to provide an indexed value for an object.
46type IndexFunc func(obj interface{}) ([]string, error)
47
48// IndexFuncToKeyFuncAdapter adapts an indexFunc to a keyFunc. This is only useful if your index function returns
49// unique values for every object. This is conversion can create errors when more than one key is found. You
50// should prefer to make proper key and index functions.
51func IndexFuncToKeyFuncAdapter(indexFunc IndexFunc) KeyFunc {
52 return func(obj interface{}) (string, error) {
53 indexKeys, err := indexFunc(obj)
54 if err != nil {
55 return "", err
56 }
57 if len(indexKeys) > 1 {
58 return "", fmt.Errorf("too many keys: %v", indexKeys)
59 }
60 if len(indexKeys) == 0 {
61 return "", fmt.Errorf("unexpected empty indexKeys")
62 }
63 return indexKeys[0], nil
64 }
65}
66
67const (
68 NamespaceIndex string = "namespace"
69)
70
71// MetaNamespaceIndexFunc is a default index function that indexes based on an object's namespace
72func MetaNamespaceIndexFunc(obj interface{}) ([]string, error) {
73 meta, err := meta.Accessor(obj)
74 if err != nil {
75 return []string{""}, fmt.Errorf("object has no meta: %v", err)
76 }
77 return []string{meta.GetNamespace()}, nil
78}
79
80// Index maps the indexed value to a set of keys in the store that match on that value
81type Index map[string]sets.String
82
83// Indexers maps a name to a IndexFunc
84type Indexers map[string]IndexFunc
85
86// Indices maps a name to an Index
87type Indices map[string]Index