k8s.io/client-go@v0.31.1/dynamic/dynamiclister/lister.go (about) 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 dynamiclister 18 19 import ( 20 "k8s.io/apimachinery/pkg/api/errors" 21 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 22 "k8s.io/apimachinery/pkg/labels" 23 "k8s.io/apimachinery/pkg/runtime/schema" 24 "k8s.io/client-go/tools/cache" 25 ) 26 27 var _ Lister = &dynamicLister{} 28 var _ NamespaceLister = &dynamicNamespaceLister{} 29 30 // dynamicLister implements the Lister interface. 31 type dynamicLister struct { 32 indexer cache.Indexer 33 gvr schema.GroupVersionResource 34 } 35 36 // New returns a new Lister. 37 func New(indexer cache.Indexer, gvr schema.GroupVersionResource) Lister { 38 return &dynamicLister{indexer: indexer, gvr: gvr} 39 } 40 41 // List lists all resources in the indexer. 42 func (l *dynamicLister) List(selector labels.Selector) (ret []*unstructured.Unstructured, err error) { 43 err = cache.ListAll(l.indexer, selector, func(m interface{}) { 44 ret = append(ret, m.(*unstructured.Unstructured)) 45 }) 46 return ret, err 47 } 48 49 // Get retrieves a resource from the indexer with the given name 50 func (l *dynamicLister) Get(name string) (*unstructured.Unstructured, error) { 51 obj, exists, err := l.indexer.GetByKey(name) 52 if err != nil { 53 return nil, err 54 } 55 if !exists { 56 return nil, errors.NewNotFound(l.gvr.GroupResource(), name) 57 } 58 return obj.(*unstructured.Unstructured), nil 59 } 60 61 // Namespace returns an object that can list and get resources from a given namespace. 62 func (l *dynamicLister) Namespace(namespace string) NamespaceLister { 63 return &dynamicNamespaceLister{indexer: l.indexer, namespace: namespace, gvr: l.gvr} 64 } 65 66 // dynamicNamespaceLister implements the NamespaceLister interface. 67 type dynamicNamespaceLister struct { 68 indexer cache.Indexer 69 namespace string 70 gvr schema.GroupVersionResource 71 } 72 73 // List lists all resources in the indexer for a given namespace. 74 func (l *dynamicNamespaceLister) List(selector labels.Selector) (ret []*unstructured.Unstructured, err error) { 75 err = cache.ListAllByNamespace(l.indexer, l.namespace, selector, func(m interface{}) { 76 ret = append(ret, m.(*unstructured.Unstructured)) 77 }) 78 return ret, err 79 } 80 81 // Get retrieves a resource from the indexer for a given namespace and name. 82 func (l *dynamicNamespaceLister) Get(name string) (*unstructured.Unstructured, error) { 83 obj, exists, err := l.indexer.GetByKey(l.namespace + "/" + name) 84 if err != nil { 85 return nil, err 86 } 87 if !exists { 88 return nil, errors.NewNotFound(l.gvr.GroupResource(), name) 89 } 90 return obj.(*unstructured.Unstructured), nil 91 }