index.go 3.0 KB

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