...

Source file src/k8s.io/client-go/tools/cache/index.go

Documentation: k8s.io/client-go/tools/cache

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cache
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/apimachinery/pkg/api/meta"
    23  	"k8s.io/apimachinery/pkg/util/sets"
    24  )
    25  
    26  // Indexer extends Store with multiple indices and restricts each
    27  // accumulator to simply hold the current object (and be empty after
    28  // Delete).
    29  //
    30  // There are three kinds of strings here:
    31  //  1. a storage key, as defined in the Store interface,
    32  //  2. a name of an index, and
    33  //  3. an "indexed value", which is produced by an IndexFunc and
    34  //     can be a field value or any other string computed from the object.
    35  type Indexer interface {
    36  	Store
    37  	// Index returns the stored objects whose set of indexed values
    38  	// intersects the set of indexed values of the given object, for
    39  	// the named index
    40  	Index(indexName string, obj interface{}) ([]interface{}, error)
    41  	// IndexKeys returns the storage keys of the stored objects whose
    42  	// set of indexed values for the named index includes the given
    43  	// indexed value
    44  	IndexKeys(indexName, indexedValue string) ([]string, error)
    45  	// ListIndexFuncValues returns all the indexed values of the given index
    46  	ListIndexFuncValues(indexName string) []string
    47  	// ByIndex returns the stored objects whose set of indexed values
    48  	// for the named index includes the given indexed value
    49  	ByIndex(indexName, indexedValue string) ([]interface{}, error)
    50  	// GetIndexers return the indexers
    51  	GetIndexers() Indexers
    52  
    53  	// AddIndexers adds more indexers to this store. This supports adding indexes after the store already has items.
    54  	AddIndexers(newIndexers Indexers) error
    55  }
    56  
    57  // IndexFunc knows how to compute the set of indexed values for an object.
    58  type IndexFunc func(obj interface{}) ([]string, error)
    59  
    60  // IndexFuncToKeyFuncAdapter adapts an indexFunc to a keyFunc.  This is only useful if your index function returns
    61  // unique values for every object.  This conversion can create errors when more than one key is found.  You
    62  // should prefer to make proper key and index functions.
    63  func IndexFuncToKeyFuncAdapter(indexFunc IndexFunc) KeyFunc {
    64  	return func(obj interface{}) (string, error) {
    65  		indexKeys, err := indexFunc(obj)
    66  		if err != nil {
    67  			return "", err
    68  		}
    69  		if len(indexKeys) > 1 {
    70  			return "", fmt.Errorf("too many keys: %v", indexKeys)
    71  		}
    72  		if len(indexKeys) == 0 {
    73  			return "", fmt.Errorf("unexpected empty indexKeys")
    74  		}
    75  		return indexKeys[0], nil
    76  	}
    77  }
    78  
    79  const (
    80  	// NamespaceIndex is the lookup name for the most common index function, which is to index by the namespace field.
    81  	NamespaceIndex string = "namespace"
    82  )
    83  
    84  // MetaNamespaceIndexFunc is a default index function that indexes based on an object's namespace
    85  func MetaNamespaceIndexFunc(obj interface{}) ([]string, error) {
    86  	meta, err := meta.Accessor(obj)
    87  	if err != nil {
    88  		return []string{""}, fmt.Errorf("object has no meta: %v", err)
    89  	}
    90  	return []string{meta.GetNamespace()}, nil
    91  }
    92  
    93  // Index maps the indexed value to a set of keys in the store that match on that value
    94  type Index map[string]sets.String
    95  
    96  // Indexers maps a name to an IndexFunc
    97  type Indexers map[string]IndexFunc
    98  
    99  // Indices maps a name to an Index
   100  type Indices map[string]Index
   101  

View as plain text