/
resourcechannels.go
310 lines (253 loc) · 8.15 KB
/
resourcechannels.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package common
import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/extensions"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
)
// ResourceChannels struct holds channels to resource lists. Each list channel is paired with
// an error channel which *must* be read sequentially: first read the list channel and then
// the error channel.
//
// This struct can be used when there are multiple clients that want to process, e.g., a
// list of pods. With this helper, the list can be read only once from the backend and
// distributed asynchronously to clients that need it.
//
// When a channel is nil, it means that no resource list is available for getting.
//
// Each channel pair can be read up to N times. N is specified upon creation of the channels.
type ResourceChannels struct {
// List and error channels to Replication Controllers.
ReplicationControllerList ReplicationControllerListChannel
// List and error channels to Replica Sets.
ReplicaSetList ReplicaSetListChannel
// List and error channels to Deployments.
DeploymentList DeploymentListChannel
// List and error channels to Daemon Sets.
DaemonSetList DaemonSetListChannel
// List and error channels to Services.
ServiceList ServiceListChannel
// List and error channels to Pods.
PodList PodListChannel
// List and error channels to Events.
EventList EventListChannel
// List and error channels to Nodes.
NodeList NodeListChannel
}
// List and error channels to Services.
type ServiceListChannel struct {
List chan *api.ServiceList
Error chan error
}
var listEverything api.ListOptions = api.ListOptions{
LabelSelector: labels.Everything(),
FieldSelector: fields.Everything(),
}
// Returns a pair of channels to a Service list and errors that both must be read
// numReads times.
func GetServiceListChannel(client client.ServicesNamespacer, numReads int) ServiceListChannel {
channel := ServiceListChannel{
List: make(chan *api.ServiceList, numReads),
Error: make(chan error, numReads),
}
go func() {
services, err := client.Services(api.NamespaceAll).List(listEverything)
for i := 0; i < numReads; i++ {
channel.List <- services
channel.Error <- err
}
}()
return channel
}
// List and error channels to Nodes.
type NodeListChannel struct {
List chan *api.NodeList
Error chan error
}
// Returns a pair of channels to a Node list and errors that both must be read
// numReads times.
func GetNodeListChannel(client client.NodesInterface, numReads int) NodeListChannel {
channel := NodeListChannel{
List: make(chan *api.NodeList, numReads),
Error: make(chan error, numReads),
}
go func() {
nodes, err := client.Nodes().List(listEverything)
for i := 0; i < numReads; i++ {
channel.List <- nodes
channel.Error <- err
}
}()
return channel
}
// List and error channels to Nodes.
type EventListChannel struct {
List chan *api.EventList
Error chan error
}
// Returns a pair of channels to an Event list and errors that both must be read
// numReads times.
func GetEventListChannel(client client.EventNamespacer, numReads int) EventListChannel {
channel := EventListChannel{
List: make(chan *api.EventList, numReads),
Error: make(chan error, numReads),
}
go func() {
events, err := client.Events(api.NamespaceAll).List(listEverything)
for i := 0; i < numReads; i++ {
channel.List <- events
channel.Error <- err
}
}()
return channel
}
func GetNamespacedEventListChannel(client client.EventNamespacer, numReads int,
namespace string, options api.ListOptions) EventListChannel {
channel := EventListChannel{
List: make(chan *api.EventList, numReads),
Error: make(chan error, numReads),
}
go func() {
events, err := client.Events(namespace).List(options)
for i := 0; i < numReads; i++ {
channel.List <- events
channel.Error <- err
}
}()
return channel
}
// List and error channels to Nodes.
type PodListChannel struct {
List chan *api.PodList
Error chan error
}
// Returns a pair of channels to a Pod list and errors that both must be read
// numReads times.
func GetPodListChannel(client client.PodsNamespacer, numReads int) PodListChannel {
channel := PodListChannel{
List: make(chan *api.PodList, numReads),
Error: make(chan error, numReads),
}
go func() {
pods, err := client.Pods(api.NamespaceAll).List(listEverything)
for i := 0; i < numReads; i++ {
channel.List <- pods
channel.Error <- err
}
}()
return channel
}
func GetNamespacedPodListChannel(client client.PodsNamespacer, numReads int,
namespace string, options api.ListOptions) PodListChannel {
channel := PodListChannel{
List: make(chan *api.PodList, numReads),
Error: make(chan error, numReads),
}
go func() {
pods, err := client.Pods(namespace).List(options)
for i := 0; i < numReads; i++ {
channel.List <- pods
channel.Error <- err
}
}()
return channel
}
// List and error channels to Nodes.
type ReplicationControllerListChannel struct {
List chan *api.ReplicationControllerList
Error chan error
}
// Returns a pair of channels to a Replication Controller list and errors that both must be read
// numReads times.
func GetReplicationControllerListChannel(client client.ReplicationControllersNamespacer,
numReads int) ReplicationControllerListChannel {
channel := ReplicationControllerListChannel{
List: make(chan *api.ReplicationControllerList, numReads),
Error: make(chan error, numReads),
}
go func() {
rcs, err := client.ReplicationControllers(api.NamespaceAll).List(listEverything)
for i := 0; i < numReads; i++ {
channel.List <- rcs
channel.Error <- err
}
}()
return channel
}
// List and error channels to Deployments.
type DeploymentListChannel struct {
List chan *extensions.DeploymentList
Error chan error
}
// Returns a pair of channels to a Deployment list and errors that both must be read
// numReads times.
func GetDeploymentListChannel(client client.DeploymentsNamespacer, numReads int) DeploymentListChannel {
channel := DeploymentListChannel{
List: make(chan *extensions.DeploymentList, numReads),
Error: make(chan error, numReads),
}
go func() {
rcs, err := client.Deployments(api.NamespaceAll).List(listEverything)
for i := 0; i < numReads; i++ {
channel.List <- rcs
channel.Error <- err
}
}()
return channel
}
// List and error channels to Replica Sets.
type ReplicaSetListChannel struct {
List chan *extensions.ReplicaSetList
Error chan error
}
// Returns a pair of channels to a ReplicaSet list and errors that both must be read
// numReads times.
func GetReplicaSetListChannel(client client.ReplicaSetsNamespacer, numReads int) ReplicaSetListChannel {
channel := ReplicaSetListChannel{
List: make(chan *extensions.ReplicaSetList, numReads),
Error: make(chan error, numReads),
}
go func() {
rcs, err := client.ReplicaSets(api.NamespaceAll).List(listEverything)
for i := 0; i < numReads; i++ {
channel.List <- rcs
channel.Error <- err
}
}()
return channel
}
// List and error channels to Nodes.
type DaemonSetListChannel struct {
List chan *extensions.DaemonSetList
Error chan error
}
// Returns a pair of channels to a DaemonSet list and errors that both must be read
// numReads times.
func GetDaemonSetListChannel(client client.DaemonSetsNamespacer, numReads int) DaemonSetListChannel {
channel := DaemonSetListChannel{
List: make(chan *extensions.DaemonSetList, numReads),
Error: make(chan error, numReads),
}
go func() {
rcs, err := client.DaemonSets(api.NamespaceAll).List(listEverything)
for i := 0; i < numReads; i++ {
channel.List <- rcs
channel.Error <- err
}
}()
return channel
}