-
Notifications
You must be signed in to change notification settings - Fork 7
/
typedlister.go
112 lines (97 loc) · 3.07 KB
/
typedlister.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package typed
import (
"fmt"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
)
// Lister provides a generically typed interface for cache.GenericLister
// It assumes the objects are unstructured.Unstructured, as you would get
// from a dynamic informer.
type Lister[K runtime.Object] struct {
lister cache.GenericLister
}
// NewLister returns a Lister for a cache.GenericLister
func NewLister[K runtime.Object](lister cache.GenericLister) *Lister[K] {
return &Lister[K]{lister: lister}
}
func (t Lister[K]) List(selector labels.Selector) (ret []K, err error) {
objs, err := t.lister.List(selector)
if err != nil {
return nil, err
}
return UnstructuredListToTypeList[K](objs)
}
func (t Lister[K]) Get(name string) (K, error) {
obj, err := t.lister.Get(name)
if err != nil {
var nilObj K
return nilObj, err
}
return UnstructuredObjToTypedObj[K](obj)
}
func (t Lister[K]) ByNamespace(namespace string) NamespaceLister[K] {
return NamespaceLister[K]{
lister: t.lister.ByNamespace(namespace),
}
}
// NamespaceLister provides a generically typed interface for
// cache.GenericNamespaceLister.
// It assumes the objects are unstructured.Unstructured, as you would get
// from a dynamic informer.
type NamespaceLister[K runtime.Object] struct {
lister cache.GenericNamespaceLister
}
func (t NamespaceLister[K]) List(selector labels.Selector) (ret []K, err error) {
objs, err := t.lister.List(selector)
if err != nil {
return nil, err
}
return UnstructuredListToTypeList[K](objs)
}
func (t NamespaceLister[K]) Get(name string) (K, error) {
obj, err := t.lister.Get(name)
if err != nil {
var nilObj K
return nilObj, err
}
return UnstructuredObjToTypedObj[K](obj)
}
// UnstructuredListToTypeList is a helper that converts a list of unstructured
// to a particular type.
func UnstructuredListToTypeList[K runtime.Object](objs []runtime.Object) ([]K, error) {
typedObjs := make([]K, 0, len(objs))
for _, obj := range objs {
typedObj, err := UnstructuredObjToTypedObj[K](obj)
if err != nil {
return nil, fmt.Errorf("list conversion error: %w", err)
}
typedObjs = append(typedObjs, typedObj)
}
return typedObjs, nil
}
// UnstructuredObjToTypedObj is a helper that converts an unstructured object
// to a particular type.
func UnstructuredObjToTypedObj[K runtime.Object](obj runtime.Object) (K, error) {
var typedObj *K
u, ok := obj.(*unstructured.Unstructured)
if !ok {
var nilObj K
return nilObj, fmt.Errorf("invalid object %T", obj)
}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, &typedObj); err != nil {
var nilObj K
return nilObj, fmt.Errorf("invalid object: %w", err)
}
return *typedObj, nil
}
// ObjToUnstructuredObj is a helper that converts a typed object
// to unstructured.
func ObjToUnstructuredObj(typedObj runtime.Object) (*unstructured.Unstructured, error) {
object, err := runtime.DefaultUnstructuredConverter.ToUnstructured(typedObj)
if err != nil {
return nil, err
}
return &unstructured.Unstructured{Object: object}, nil
}