index.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 extends Store with multiple indices and restricts each
  20. // accumulator to simply hold the current object (and be empty after
  21. // Delete).
  22. //
  23. // There are three kinds of strings here:
  24. // 1. a storage key, as defined in the Store interface,
  25. // 2. a name of an index, and
  26. // 3. an "indexed value", which is produced by an IndexFunc and
  27. // can be a field value or any other string computed from the object.
  28. type Indexer interface {
  29. Store
  30. // Index returns the stored objects whose set of indexed values
  31. // intersects the set of indexed values of the given object, for
  32. // the named index
  33. Index(indexName string, obj interface{}) ([]interface{}, error)
  34. // IndexKeys returns the storage keys of the stored objects whose
  35. // set of indexed values for the named index includes the given
  36. // indexed value
  37. IndexKeys(indexName, indexedValue string) ([]string, error)
  38. // ListIndexFuncValues returns all the indexed values of the given index
  39. ListIndexFuncValues(indexName string) []string
  40. // ByIndex returns the stored objects whose set of indexed values
  41. // for the named index includes the given indexed value
  42. ByIndex(indexName, indexedValue string) ([]interface{}, error)
  43. // GetIndexer return the indexers
  44. GetIndexers() Indexers
  45. // AddIndexers adds more indexers to this store. If you call this after you already have data
  46. // in the store, the results are undefined.
  47. AddIndexers(newIndexers Indexers) error
  48. }
  49. // IndexFunc knows how to compute the set of indexed values for an object.
  50. type IndexFunc func(obj interface{}) ([]string, error)
  51. // IndexFuncToKeyFuncAdapter adapts an indexFunc to a keyFunc. This is only useful if your index function returns
  52. // unique values for every object. This conversion can create errors when more than one key is found. You
  53. // should prefer to make proper key and index functions.
  54. func IndexFuncToKeyFuncAdapter(indexFunc IndexFunc) KeyFunc {
  55. return func(obj interface{}) (string, error) {
  56. indexKeys, err := indexFunc(obj)
  57. if err != nil {
  58. return "", err
  59. }
  60. if len(indexKeys) > 1 {
  61. return "", fmt.Errorf("too many keys: %v", indexKeys)
  62. }
  63. if len(indexKeys) == 0 {
  64. return "", fmt.Errorf("unexpected empty indexKeys")
  65. }
  66. return indexKeys[0], nil
  67. }
  68. }
  69. const (
  70. // NamespaceIndex is the lookup name for the most comment index function, which is to index by the namespace field.
  71. NamespaceIndex string = "namespace"
  72. )
  73. // MetaNamespaceIndexFunc is a default index function that indexes based on an object's namespace
  74. func MetaNamespaceIndexFunc(obj interface{}) ([]string, error) {
  75. meta, err := meta.Accessor(obj)
  76. if err != nil {
  77. return []string{""}, fmt.Errorf("object has no meta: %v", err)
  78. }
  79. return []string{meta.GetNamespace()}, nil
  80. }
  81. // Index maps the indexed value to a set of keys in the store that match on that value
  82. type Index map[string]sets.String
  83. // Indexers maps a name to a IndexFunc
  84. type Indexers map[string]IndexFunc
  85. // Indices maps a name to an Index
  86. type Indices map[string]Index