index.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // ListIndexFuncValues returns the list of generated values of an Index func
  25. ListIndexFuncValues(indexName string) []string
  26. // ByIndex lists object that match on the named indexing function with the exact key
  27. ByIndex(indexName, indexKey string) ([]interface{}, error)
  28. // GetIndexer return the indexers
  29. GetIndexers() Indexers
  30. // AddIndexers adds more indexers to this store. If you call this after you already have data
  31. // in the store, the results are undefined.
  32. AddIndexers(newIndexers Indexers) error
  33. }
  34. // IndexFunc knows how to provide an indexed value for an object.
  35. type IndexFunc func(obj interface{}) ([]string, error)
  36. // IndexFuncToKeyFuncAdapter adapts an indexFunc to a keyFunc. This is only useful if your index function returns
  37. // unique values for every object. This is conversion can create errors when more than one key is found. You
  38. // should prefer to make proper key and index functions.
  39. func IndexFuncToKeyFuncAdapter(indexFunc IndexFunc) KeyFunc {
  40. return func(obj interface{}) (string, error) {
  41. indexKeys, err := indexFunc(obj)
  42. if err != nil {
  43. return "", err
  44. }
  45. if len(indexKeys) > 1 {
  46. return "", fmt.Errorf("too many keys: %v", indexKeys)
  47. }
  48. if len(indexKeys) == 0 {
  49. return "", fmt.Errorf("unexpected empty indexKeys")
  50. }
  51. return indexKeys[0], nil
  52. }
  53. }
  54. const (
  55. NamespaceIndex string = "namespace"
  56. )
  57. // MetaNamespaceIndexFunc is a default index function that indexes based on an object's namespace
  58. func MetaNamespaceIndexFunc(obj interface{}) ([]string, error) {
  59. meta, err := meta.Accessor(obj)
  60. if err != nil {
  61. return []string{""}, fmt.Errorf("object has no meta: %v", err)
  62. }
  63. return []string{meta.GetNamespace()}, nil
  64. }
  65. // Index maps the indexed value to a set of keys in the store that match on that value
  66. type Index map[string]sets.String
  67. // Indexers maps a name to a IndexFunc
  68. type Indexers map[string]IndexFunc
  69. // Indices maps a name to an Index
  70. type Indices map[string]Index