|
| 1 | +/* |
| 2 | +Copyright 2018 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 | + "github.com/kubernetes-sigs/controller-runtime/pkg/cache/internal" |
| 23 | + "github.com/kubernetes-sigs/controller-runtime/pkg/client" |
| 24 | + "github.com/kubernetes-sigs/controller-runtime/pkg/client/apiutil" |
| 25 | + apimeta "k8s.io/apimachinery/pkg/api/meta" |
| 26 | + "k8s.io/apimachinery/pkg/runtime" |
| 27 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 28 | + "k8s.io/client-go/tools/cache" |
| 29 | +) |
| 30 | + |
| 31 | +// GetInformerForKind returns the informer for the GroupVersionKind |
| 32 | +func (ip *informerCache) GetInformerForKind(gvk schema.GroupVersionKind) (cache.SharedIndexInformer, error) { |
| 33 | + // Map the gvk to an object |
| 34 | + obj, err := ip.Scheme.New(gvk) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + i, err := ip.InformersMap.Get(gvk, obj) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + return i.Informer, err |
| 43 | +} |
| 44 | + |
| 45 | +// GetInformer returns the informer for the obj |
| 46 | +func (ip *informerCache) GetInformer(obj runtime.Object) (cache.SharedIndexInformer, error) { |
| 47 | + gvk, err := apiutil.GVKForObject(obj, ip.Scheme) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + i, err := ip.InformersMap.Get(gvk, obj) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + return i.Informer, err |
| 56 | +} |
| 57 | + |
| 58 | +// IndexField adds an indexer to the underlying cache, using extraction function to get |
| 59 | +// value(s) from the given field. This index can then be used by passing a field selector |
| 60 | +// to List. For one-to-one compatibility with "normal" field selectors, only return one value. |
| 61 | +// The values may be anything. They will automatically be prefixed with the namespace of the |
| 62 | +// given object, if present. The objects passed are guaranteed to be objects of the correct type. |
| 63 | +func (ip *informerCache) IndexField(obj runtime.Object, field string, extractValue client.IndexerFunc) error { |
| 64 | + informer, err := ip.GetInformer(obj) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + return indexByField(informer.GetIndexer(), field, extractValue) |
| 69 | +} |
| 70 | + |
| 71 | +func indexByField(indexer cache.Indexer, field string, extractor client.IndexerFunc) error { |
| 72 | + indexFunc := func(objRaw interface{}) ([]string, error) { |
| 73 | + // TODO(directxman12): check if this is the correct type? |
| 74 | + obj, isObj := objRaw.(runtime.Object) |
| 75 | + if !isObj { |
| 76 | + return nil, fmt.Errorf("object of type %T is not an Object", objRaw) |
| 77 | + } |
| 78 | + meta, err := apimeta.Accessor(obj) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + ns := meta.GetNamespace() |
| 83 | + |
| 84 | + rawVals := extractor(obj) |
| 85 | + var vals []string |
| 86 | + if ns == "" { |
| 87 | + // if we're not doubling the keys for the namespaced case, just re-use what was returned to us |
| 88 | + vals = rawVals |
| 89 | + } else { |
| 90 | + // if we need to add non-namespaced versions too, double the length |
| 91 | + vals = make([]string, len(rawVals)*2) |
| 92 | + } |
| 93 | + for i, rawVal := range rawVals { |
| 94 | + // save a namespaced variant, so that we can ask |
| 95 | + // "what are all the object matching a given index *in a given namespace*" |
| 96 | + vals[i] = internal.KeyToNamespacedKey(ns, rawVal) |
| 97 | + if ns != "" { |
| 98 | + // if we have a namespace, also inject a special index key for listing |
| 99 | + // regardless of the object namespace |
| 100 | + vals[i+len(rawVals)] = internal.KeyToNamespacedKey("", rawVal) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return vals, nil |
| 105 | + } |
| 106 | + |
| 107 | + return indexer.AddIndexers(cache.Indexers{internal.FieldIndexName(field): indexFunc}) |
| 108 | +} |
0 commit comments