| 1 |
/* |
| 2 |
Copyright 2023 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 listers |
| 18 |
|
| 19 |
import ( |
| 20 |
"k8s.io/apimachinery/pkg/api/errors" |
| 21 |
"k8s.io/apimachinery/pkg/labels" |
| 22 |
"k8s.io/apimachinery/pkg/runtime" |
| 23 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 24 |
"k8s.io/client-go/tools/cache" |
| 25 |
) |
| 26 |
|
| 27 |
// ResourceIndexer wraps an indexer, resource, and optional namespace for a given type. |
| 28 |
// This is intended for use by listers (generated by lister-gen) only. |
| 29 |
type ResourceIndexer[T runtime.Object] struct { |
| 30 |
indexer cache.Indexer |
| 31 |
resource schema.GroupResource |
| 32 |
namespace string // empty for non-namespaced types |
| 33 |
} |
| 34 |
|
| 35 |
// New returns a new instance of a lister (resource indexer) wrapping the given indexer and resource for the specified type. |
| 36 |
// This is intended for use by listers (generated by lister-gen) only. |
| 37 |
func New[T runtime.Object](indexer cache.Indexer, resource schema.GroupResource) ResourceIndexer[T] { |
| 38 |
return ResourceIndexer[T]{indexer: indexer, resource: resource} |
| 39 |
} |
| 40 |
|
| 41 |
// NewNamespaced returns a new instance of a namespaced lister (resource indexer) wrapping the given parent and namespace for the specified type. |
| 42 |
// This is intended for use by listers (generated by lister-gen) only. |
| 43 |
func NewNamespaced[T runtime.Object](parent ResourceIndexer[T], namespace string) ResourceIndexer[T] { |
| 44 |
return ResourceIndexer[T]{indexer: parent.indexer, resource: parent.resource, namespace: namespace} |
| 45 |
} |
| 46 |
|
| 47 |
// List lists all resources in the indexer matching the given selector. |
| 48 |
func (l ResourceIndexer[T]) List(selector labels.Selector) (ret []T, err error) { |
| 49 |
// ListAllByNamespace reverts to ListAll on empty namespaces |
| 50 |
err = cache.ListAllByNamespace(l.indexer, l.namespace, selector, func(m interface{}) { |
| 51 |
ret = append(ret, m.(T)) |
| 52 |
}) |
| 53 |
return ret, err |
| 54 |
} |
| 55 |
|
| 56 |
// Get retrieves the resource from the index for a given name. |
| 57 |
func (l ResourceIndexer[T]) Get(name string) (T, error) { |
| 58 |
var key string |
| 59 |
if l.namespace == "" { |
| 60 |
key = name |
| 61 |
} else { |
| 62 |
key = l.namespace + "/" + name |
| 63 |
} |
| 64 |
obj, exists, err := l.indexer.GetByKey(key) |
| 65 |
if err != nil { |
| 66 |
return *new(T), err |
| 67 |
} |
| 68 |
if !exists { |
| 69 |
return *new(T), errors.NewNotFound(l.resource, name) |
| 70 |
} |
| 71 |
return obj.(T), nil |
| 72 |
} |