github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/client/cache/index.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     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/kubernetes/pkg/api/meta"
    23  	"k8s.io/kubernetes/pkg/util/sets"
    24  )
    25  
    26  // Indexer is a storage interface that lets you list objects using multiple indexing functions
    27  type Indexer interface {
    28  	Store
    29  	// Retrieve list of objects that match on the named indexing function
    30  	Index(indexName string, obj interface{}) ([]interface{}, error)
    31  	// ListIndexFuncValues returns the list of generated values of an Index func
    32  	ListIndexFuncValues(indexName string) []string
    33  	// ByIndex lists object that match on the named indexing function with the exact key
    34  	ByIndex(indexName, indexKey string) ([]interface{}, error)
    35  }
    36  
    37  // IndexFunc knows how to provide an indexed value for an object.
    38  type IndexFunc func(obj interface{}) ([]string, error)
    39  
    40  // IndexFuncToKeyFuncAdapter adapts an indexFunc to a keyFunc.  This is only useful if your index function returns
    41  // unique values for every object.  This is conversion can create errors when more than one key is found.  You
    42  // should prefer to make proper key and index functions.
    43  func IndexFuncToKeyFuncAdapter(indexFunc IndexFunc) KeyFunc {
    44  	return func(obj interface{}) (string, error) {
    45  		indexKeys, err := indexFunc(obj)
    46  		if err != nil {
    47  			return "", err
    48  		}
    49  		if len(indexKeys) > 1 {
    50  			return "", fmt.Errorf("too many keys: %v", indexKeys)
    51  		}
    52  		return indexKeys[0], nil
    53  	}
    54  }
    55  
    56  // MetaNamespaceIndexFunc is a default index function that indexes based on an object's namespace
    57  func MetaNamespaceIndexFunc(obj interface{}) ([]string, error) {
    58  	meta, err := meta.Accessor(obj)
    59  	if err != nil {
    60  		return []string{""}, fmt.Errorf("object has no meta: %v", err)
    61  	}
    62  	return []string{meta.Namespace()}, nil
    63  }
    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  
    68  // Indexers maps a name to a IndexFunc
    69  type Indexers map[string]IndexFunc
    70  
    71  // Indices maps a name to an Index
    72  type Indices map[string]Index