github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/index/label.go (about) 1 package indexer 2 3 import ( 4 "fmt" 5 6 "k8s.io/apimachinery/pkg/api/meta" 7 "k8s.io/apimachinery/pkg/labels" 8 "k8s.io/client-go/tools/cache" 9 ) 10 11 const ( 12 // MetaLabelIndexFuncKey is the recommended key to use for registering the index func with an indexer. 13 MetaLabelIndexFuncKey string = "metalabelindexfunc" 14 ) 15 16 // MetaLabelIndexFunc returns indicies from the labels of the given object. 17 func MetaLabelIndexFunc(obj interface{}) ([]string, error) { 18 indicies := []string{} 19 m, err := meta.Accessor(obj) 20 if err != nil { 21 return indicies, fmt.Errorf("object has no meta: %v", err) 22 } 23 24 for k, v := range m.GetLabels() { 25 indicies = append(indicies, fmt.Sprintf("%s=%s", k, v)) 26 } 27 28 return indicies, nil 29 } 30 31 // LabelIndexKeys returns the union of indexed cache keys in the given indexers matching the same labels as the given selector 32 func LabelIndexKeys(indexers map[string]cache.Indexer, labelSets ...labels.Set) ([]string, error) { 33 keySet := map[string]struct{}{} 34 keys := []string{} 35 for _, indexer := range indexers { 36 for _, labelSet := range labelSets { 37 for key, value := range labelSet { 38 apiLabelKey := fmt.Sprintf("%s=%s", key, value) 39 cacheKeys, err := indexer.IndexKeys(MetaLabelIndexFuncKey, apiLabelKey) 40 if err != nil { 41 return nil, err 42 } 43 44 for _, cacheKey := range cacheKeys { 45 // Detect duplication 46 if _, ok := keySet[cacheKey]; ok { 47 continue 48 } 49 50 // Add to set 51 keySet[cacheKey] = struct{}{} 52 keys = append(keys, cacheKey) 53 } 54 55 } 56 } 57 } 58 59 return keys, nil 60 }