forked from rancher/wrangler
/
simple.go
115 lines (95 loc) · 2.56 KB
/
simple.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
113
114
115
package client
import (
"context"
"github.com/kubernot/wrangler/pkg/summary"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
)
type summaryClient struct {
client dynamic.Interface
}
var _ Interface = &summaryClient{}
func NewForDynamicClient(client dynamic.Interface) Interface {
return &summaryClient{client: client}
}
type summaryResourceClient struct {
client dynamic.Interface
namespace string
resource schema.GroupVersionResource
}
func (c *summaryClient) Resource(resource schema.GroupVersionResource) NamespaceableResourceInterface {
return &summaryResourceClient{client: c.client, resource: resource}
}
func (c *summaryResourceClient) Namespace(ns string) ResourceInterface {
ret := *c
ret.namespace = ns
return &ret
}
func (c *summaryResourceClient) List(ctx context.Context, opts metav1.ListOptions) (*summary.SummarizedObjectList, error) {
var (
u *unstructured.UnstructuredList
err error
)
if c.namespace == "" {
u, err = c.client.Resource(c.resource).List(ctx, opts)
} else {
u, err = c.client.Resource(c.resource).Namespace(c.namespace).List(ctx, opts)
}
if err != nil {
return nil, err
}
list := &summary.SummarizedObjectList{
TypeMeta: metav1.TypeMeta{
Kind: u.GetKind(),
APIVersion: u.GetAPIVersion(),
},
ListMeta: metav1.ListMeta{
ResourceVersion: u.GetResourceVersion(),
Continue: u.GetContinue(),
RemainingItemCount: u.GetRemainingItemCount(),
},
}
for _, obj := range u.Items {
list.Items = append(list.Items, *summary.Summarized(&obj))
}
return list, nil
}
func (c *summaryResourceClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
var (
resp watch.Interface
err error
)
eventChan := make(chan watch.Event)
if c.namespace == "" {
resp, err = c.client.Resource(c.resource).Watch(ctx, opts)
} else {
resp, err = c.client.Resource(c.resource).Namespace(c.namespace).Watch(ctx, opts)
}
if err != nil {
return nil, err
}
go func() {
defer close(eventChan)
for event := range resp.ResultChan() {
// don't encode status objects
if _, ok := event.Object.(*metav1.Status); !ok {
event.Object = summary.Summarized(event.Object)
}
eventChan <- event
}
}()
return &watcher{
Interface: resp,
eventChan: eventChan,
}, nil
}
type watcher struct {
watch.Interface
eventChan chan watch.Event
}
func (w watcher) ResultChan() <-chan watch.Event {
return w.eventChan
}