mirror of
https://github.com/kubernetes/sample-controller.git
synced 2025-04-20 01:46:27 +08:00
Merge pull request #121574 from skitt/generic-lister-gen
Generify lister-gen Kubernetes-commit: 6b260382a1b5e461c39289892b1d3a3b335ecc67
This commit is contained in:
commit
9a8a7c6f1c
4
go.mod
4
go.mod
@ -8,8 +8,8 @@ require (
|
||||
golang.org/x/time v0.3.0
|
||||
k8s.io/api v0.0.0-20240418173402-5975d5e5bda6
|
||||
k8s.io/apimachinery v0.0.0-20240418133208-0ee3e6150890
|
||||
k8s.io/client-go v0.0.0-20240422173645-5b2b83af7254
|
||||
k8s.io/code-generator v0.0.0-20240422141125-5ae098a374a1
|
||||
k8s.io/client-go v0.0.0-20240423013647-02e371e43736
|
||||
k8s.io/code-generator v0.0.0-20240423014140-a3972270948f
|
||||
k8s.io/klog/v2 v2.120.1
|
||||
)
|
||||
|
||||
|
8
go.sum
8
go.sum
@ -146,10 +146,10 @@ k8s.io/api v0.0.0-20240418173402-5975d5e5bda6 h1:iIqllpQqao2EVRqwEYv4PrT5rNpARgS
|
||||
k8s.io/api v0.0.0-20240418173402-5975d5e5bda6/go.mod h1:aiyYpZwHjPqNTHVIbcUReEDsDv1bLzwNhSENZpETJiA=
|
||||
k8s.io/apimachinery v0.0.0-20240418133208-0ee3e6150890 h1:QnCWgLriYnSGYNYeDsMidsvvh4zidzUylhjQeKRajk4=
|
||||
k8s.io/apimachinery v0.0.0-20240418133208-0ee3e6150890/go.mod h1:iexa2somDaxdnj7bha06bhb43Zpa6eWH8N8dbqVjTUc=
|
||||
k8s.io/client-go v0.0.0-20240422173645-5b2b83af7254 h1:StItvLTMkuQXsThbsTt/eGkhY4daBPgnk/16rQYH4EU=
|
||||
k8s.io/client-go v0.0.0-20240422173645-5b2b83af7254/go.mod h1:CJzwQ6WTXDHq3l3PgFTyna9j0c1kBo2tPbE6Pk7UHBo=
|
||||
k8s.io/code-generator v0.0.0-20240422141125-5ae098a374a1 h1:OkylnsNROJoiDL4LiuMphSlALQGgsLuEM7Ykd7tCH8I=
|
||||
k8s.io/code-generator v0.0.0-20240422141125-5ae098a374a1/go.mod h1:GUrN9h+CrE0UDmfJRpLRhtXkJG0pDQQMmADL64N+kfQ=
|
||||
k8s.io/client-go v0.0.0-20240423013647-02e371e43736 h1:1lIt49XT2c0v4BSGBAiImZSdQmvUsD0b4+snMX73+OI=
|
||||
k8s.io/client-go v0.0.0-20240423013647-02e371e43736/go.mod h1:CJzwQ6WTXDHq3l3PgFTyna9j0c1kBo2tPbE6Pk7UHBo=
|
||||
k8s.io/code-generator v0.0.0-20240423014140-a3972270948f h1:b7K38yFaCqIhX9ouq7Sf0q/FxiJ63FvQDRE4JeeA1r8=
|
||||
k8s.io/code-generator v0.0.0-20240423014140-a3972270948f/go.mod h1:GUrN9h+CrE0UDmfJRpLRhtXkJG0pDQQMmADL64N+kfQ=
|
||||
k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 h1:NGrVE502P0s0/1hudf8zjgwki1X/TByhmAoILTarmzo=
|
||||
k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70/go.mod h1:VH3AT8AaQOqiGjMF9p0/IM1Dj+82ZwjfxUP1IxaHE+8=
|
||||
k8s.io/klog/v2 v2.120.1 h1:QXU6cPEOIslTGvZaXvFWiP9VKyeet3sawzTOvdXb4Vw=
|
||||
|
@ -19,8 +19,8 @@ limitations under the License.
|
||||
package v1alpha1
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/client-go/listers"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
v1alpha1 "k8s.io/sample-controller/pkg/apis/samplecontroller/v1alpha1"
|
||||
)
|
||||
@ -38,25 +38,17 @@ type FooLister interface {
|
||||
|
||||
// fooLister implements the FooLister interface.
|
||||
type fooLister struct {
|
||||
indexer cache.Indexer
|
||||
listers.ResourceIndexer[*v1alpha1.Foo]
|
||||
}
|
||||
|
||||
// NewFooLister returns a new FooLister.
|
||||
func NewFooLister(indexer cache.Indexer) FooLister {
|
||||
return &fooLister{indexer: indexer}
|
||||
}
|
||||
|
||||
// List lists all Foos in the indexer.
|
||||
func (s *fooLister) List(selector labels.Selector) (ret []*v1alpha1.Foo, err error) {
|
||||
err = cache.ListAll(s.indexer, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*v1alpha1.Foo))
|
||||
})
|
||||
return ret, err
|
||||
return &fooLister{listers.New[*v1alpha1.Foo](indexer, v1alpha1.Resource("foo"))}
|
||||
}
|
||||
|
||||
// Foos returns an object that can list and get Foos.
|
||||
func (s *fooLister) Foos(namespace string) FooNamespaceLister {
|
||||
return fooNamespaceLister{indexer: s.indexer, namespace: namespace}
|
||||
return fooNamespaceLister{listers.NewNamespaced[*v1alpha1.Foo](s.ResourceIndexer, namespace)}
|
||||
}
|
||||
|
||||
// FooNamespaceLister helps list and get Foos.
|
||||
@ -74,26 +66,5 @@ type FooNamespaceLister interface {
|
||||
// fooNamespaceLister implements the FooNamespaceLister
|
||||
// interface.
|
||||
type fooNamespaceLister struct {
|
||||
indexer cache.Indexer
|
||||
namespace string
|
||||
}
|
||||
|
||||
// List lists all Foos in the indexer for a given namespace.
|
||||
func (s fooNamespaceLister) List(selector labels.Selector) (ret []*v1alpha1.Foo, err error) {
|
||||
err = cache.ListAllByNamespace(s.indexer, s.namespace, selector, func(m interface{}) {
|
||||
ret = append(ret, m.(*v1alpha1.Foo))
|
||||
})
|
||||
return ret, err
|
||||
}
|
||||
|
||||
// Get retrieves the Foo from the indexer for a given namespace and name.
|
||||
func (s fooNamespaceLister) Get(name string) (*v1alpha1.Foo, error) {
|
||||
obj, exists, err := s.indexer.GetByKey(s.namespace + "/" + name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.NewNotFound(v1alpha1.Resource("foo"), name)
|
||||
}
|
||||
return obj.(*v1alpha1.Foo), nil
|
||||
listers.ResourceIndexer[*v1alpha1.Foo]
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user