2019-01-18 01:50:10 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
|
|
|
)
|
|
|
|
|
2021-05-15 10:08:31 +00:00
|
|
|
// Indexer extends Store with multiple indices and restricts each
|
|
|
|
// accumulator to simply hold the current object (and be empty after
|
|
|
|
// Delete).
|
|
|
|
//
|
|
|
|
// There are three kinds of strings here:
|
|
|
|
// 1. a storage key, as defined in the Store interface,
|
|
|
|
// 2. a name of an index, and
|
|
|
|
// 3. an "indexed value", which is produced by an IndexFunc and
|
|
|
|
// can be a field value or any other string computed from the object.
|
2019-01-18 01:50:10 +00:00
|
|
|
type Indexer interface {
|
|
|
|
Store
|
2021-05-15 10:08:31 +00:00
|
|
|
// Index returns the stored objects whose set of indexed values
|
|
|
|
// intersects the set of indexed values of the given object, for
|
|
|
|
// the named index
|
2019-01-18 01:50:10 +00:00
|
|
|
Index(indexName string, obj interface{}) ([]interface{}, error)
|
2021-05-15 10:08:31 +00:00
|
|
|
// IndexKeys returns the storage keys of the stored objects whose
|
|
|
|
// set of indexed values for the named index includes the given
|
|
|
|
// indexed value
|
|
|
|
IndexKeys(indexName, indexedValue string) ([]string, error)
|
|
|
|
// ListIndexFuncValues returns all the indexed values of the given index
|
2019-01-18 01:50:10 +00:00
|
|
|
ListIndexFuncValues(indexName string) []string
|
2021-05-15 10:08:31 +00:00
|
|
|
// ByIndex returns the stored objects whose set of indexed values
|
|
|
|
// for the named index includes the given indexed value
|
|
|
|
ByIndex(indexName, indexedValue string) ([]interface{}, error)
|
2019-01-18 01:50:10 +00:00
|
|
|
// GetIndexer return the indexers
|
|
|
|
GetIndexers() Indexers
|
|
|
|
|
|
|
|
// AddIndexers adds more indexers to this store. If you call this after you already have data
|
|
|
|
// in the store, the results are undefined.
|
|
|
|
AddIndexers(newIndexers Indexers) error
|
|
|
|
}
|
|
|
|
|
2021-05-15 10:08:31 +00:00
|
|
|
// IndexFunc knows how to compute the set of indexed values for an object.
|
2019-01-18 01:50:10 +00:00
|
|
|
type IndexFunc func(obj interface{}) ([]string, error)
|
|
|
|
|
|
|
|
// IndexFuncToKeyFuncAdapter adapts an indexFunc to a keyFunc. This is only useful if your index function returns
|
2021-05-15 10:08:31 +00:00
|
|
|
// unique values for every object. This conversion can create errors when more than one key is found. You
|
2019-01-18 01:50:10 +00:00
|
|
|
// should prefer to make proper key and index functions.
|
|
|
|
func IndexFuncToKeyFuncAdapter(indexFunc IndexFunc) KeyFunc {
|
|
|
|
return func(obj interface{}) (string, error) {
|
|
|
|
indexKeys, err := indexFunc(obj)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(indexKeys) > 1 {
|
|
|
|
return "", fmt.Errorf("too many keys: %v", indexKeys)
|
|
|
|
}
|
|
|
|
if len(indexKeys) == 0 {
|
|
|
|
return "", fmt.Errorf("unexpected empty indexKeys")
|
|
|
|
}
|
|
|
|
return indexKeys[0], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2022-04-23 09:01:19 +00:00
|
|
|
// NamespaceIndex is the lookup name for the most common index function, which is to index by the namespace field.
|
2019-01-18 01:50:10 +00:00
|
|
|
NamespaceIndex string = "namespace"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MetaNamespaceIndexFunc is a default index function that indexes based on an object's namespace
|
|
|
|
func MetaNamespaceIndexFunc(obj interface{}) ([]string, error) {
|
|
|
|
meta, err := meta.Accessor(obj)
|
|
|
|
if err != nil {
|
|
|
|
return []string{""}, fmt.Errorf("object has no meta: %v", err)
|
|
|
|
}
|
|
|
|
return []string{meta.GetNamespace()}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index maps the indexed value to a set of keys in the store that match on that value
|
|
|
|
type Index map[string]sets.String
|
|
|
|
|
2022-04-23 09:01:19 +00:00
|
|
|
// Indexers maps a name to an IndexFunc
|
2019-01-18 01:50:10 +00:00
|
|
|
type Indexers map[string]IndexFunc
|
|
|
|
|
|
|
|
// Indices maps a name to an Index
|
|
|
|
type Indices map[string]Index
|