/
listers.go
370 lines (320 loc) · 15.1 KB
/
listers.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
Copyright 2016 The Kubernetes Authors.
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 kubernetes
import (
"time"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
apiv1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
client "k8s.io/client-go/kubernetes"
v1appslister "k8s.io/client-go/listers/apps/v1"
v1batchlister "k8s.io/client-go/listers/batch/v1"
v1lister "k8s.io/client-go/listers/core/v1"
v1policylister "k8s.io/client-go/listers/policy/v1"
"k8s.io/client-go/tools/cache"
podv1 "k8s.io/kubernetes/pkg/api/v1/pod"
)
// ListerRegistry is a registry providing various listers to list pods or nodes matching conditions
type ListerRegistry interface {
AllNodeLister() NodeLister
ReadyNodeLister() NodeLister
ScheduledPodLister() PodLister
UnschedulablePodLister() PodLister
PodDisruptionBudgetLister() PodDisruptionBudgetLister
DaemonSetLister() v1appslister.DaemonSetLister
ReplicationControllerLister() v1lister.ReplicationControllerLister
JobLister() v1batchlister.JobLister
ReplicaSetLister() v1appslister.ReplicaSetLister
StatefulSetLister() v1appslister.StatefulSetLister
}
type listerRegistryImpl struct {
allNodeLister NodeLister
readyNodeLister NodeLister
scheduledPodLister PodLister
unschedulablePodLister PodLister
podDisruptionBudgetLister PodDisruptionBudgetLister
daemonSetLister v1appslister.DaemonSetLister
replicationControllerLister v1lister.ReplicationControllerLister
jobLister v1batchlister.JobLister
replicaSetLister v1appslister.ReplicaSetLister
statefulSetLister v1appslister.StatefulSetLister
}
// NewListerRegistry returns a registry providing various listers to list pods or nodes matching conditions
func NewListerRegistry(allNode NodeLister, readyNode NodeLister, scheduledPod PodLister,
unschedulablePod PodLister, podDisruptionBudgetLister PodDisruptionBudgetLister,
daemonSetLister v1appslister.DaemonSetLister, replicationControllerLister v1lister.ReplicationControllerLister,
jobLister v1batchlister.JobLister, replicaSetLister v1appslister.ReplicaSetLister,
statefulSetLister v1appslister.StatefulSetLister) ListerRegistry {
return listerRegistryImpl{
allNodeLister: allNode,
readyNodeLister: readyNode,
scheduledPodLister: scheduledPod,
unschedulablePodLister: unschedulablePod,
podDisruptionBudgetLister: podDisruptionBudgetLister,
daemonSetLister: daemonSetLister,
replicationControllerLister: replicationControllerLister,
jobLister: jobLister,
replicaSetLister: replicaSetLister,
statefulSetLister: statefulSetLister,
}
}
// NewListerRegistryWithDefaultListers returns a registry filled with listers of the default implementations
func NewListerRegistryWithDefaultListers(kubeClient client.Interface, stopChannel <-chan struct{}) ListerRegistry {
unschedulablePodLister := NewUnschedulablePodLister(kubeClient, stopChannel)
scheduledPodLister := NewScheduledPodLister(kubeClient, stopChannel)
readyNodeLister := NewReadyNodeLister(kubeClient, stopChannel)
allNodeLister := NewAllNodeLister(kubeClient, stopChannel)
podDisruptionBudgetLister := NewPodDisruptionBudgetLister(kubeClient, stopChannel)
daemonSetLister := NewDaemonSetLister(kubeClient, stopChannel)
replicationControllerLister := NewReplicationControllerLister(kubeClient, stopChannel)
jobLister := NewJobLister(kubeClient, stopChannel)
replicaSetLister := NewReplicaSetLister(kubeClient, stopChannel)
statefulSetLister := NewStatefulSetLister(kubeClient, stopChannel)
return NewListerRegistry(allNodeLister, readyNodeLister, scheduledPodLister,
unschedulablePodLister, podDisruptionBudgetLister, daemonSetLister,
replicationControllerLister, jobLister, replicaSetLister, statefulSetLister)
}
// AllNodeLister returns the AllNodeLister registered to this registry
func (r listerRegistryImpl) AllNodeLister() NodeLister {
return r.allNodeLister
}
// ReadyNodeLister returns the ReadyNodeLister registered to this registry
func (r listerRegistryImpl) ReadyNodeLister() NodeLister {
return r.readyNodeLister
}
// ScheduledPodLister returns the ScheduledPodLister registered to this registry
func (r listerRegistryImpl) ScheduledPodLister() PodLister {
return r.scheduledPodLister
}
// UnschedulablePodLister returns the UnschedulablePodLister registered to this registry
func (r listerRegistryImpl) UnschedulablePodLister() PodLister {
return r.unschedulablePodLister
}
// PodDisruptionBudgetLister returns the podDisruptionBudgetLister registered to this registry
func (r listerRegistryImpl) PodDisruptionBudgetLister() PodDisruptionBudgetLister {
return r.podDisruptionBudgetLister
}
// DaemonSetLister returns the daemonSetLister registered to this registry
func (r listerRegistryImpl) DaemonSetLister() v1appslister.DaemonSetLister {
return r.daemonSetLister
}
// ReplicationControllerLister returns the replicationControllerLister registered to this registry
func (r listerRegistryImpl) ReplicationControllerLister() v1lister.ReplicationControllerLister {
return r.replicationControllerLister
}
// JobLister returns the jobLister registered to this registry
func (r listerRegistryImpl) JobLister() v1batchlister.JobLister {
return r.jobLister
}
// ReplicaSetLister returns the replicaSetLister registered to this registry
func (r listerRegistryImpl) ReplicaSetLister() v1appslister.ReplicaSetLister {
return r.replicaSetLister
}
// StatefulSetLister returns the statefulSetLister registered to this registry
func (r listerRegistryImpl) StatefulSetLister() v1appslister.StatefulSetLister {
return r.statefulSetLister
}
// PodLister lists pods.
type PodLister interface {
List() ([]*apiv1.Pod, error)
}
// UnschedulablePodLister lists unscheduled pods
type UnschedulablePodLister struct {
podLister v1lister.PodLister
}
// List returns all unscheduled pods.
func (unschedulablePodLister *UnschedulablePodLister) List() ([]*apiv1.Pod, error) {
var unschedulablePods []*apiv1.Pod
allPods, err := unschedulablePodLister.podLister.List(labels.Everything())
if err != nil {
return unschedulablePods, err
}
for _, pod := range allPods {
_, condition := podv1.GetPodCondition(&pod.Status, apiv1.PodScheduled)
if condition != nil && condition.Status == apiv1.ConditionFalse && condition.Reason == apiv1.PodReasonUnschedulable {
unschedulablePods = append(unschedulablePods, pod)
}
}
return unschedulablePods, nil
}
// NewUnschedulablePodLister returns a lister providing pods that failed to be scheduled.
func NewUnschedulablePodLister(kubeClient client.Interface, stopchannel <-chan struct{}) PodLister {
return NewUnschedulablePodInNamespaceLister(kubeClient, apiv1.NamespaceAll, stopchannel)
}
// NewUnschedulablePodInNamespaceLister returns a lister providing pods that failed to be scheduled in the given namespace.
func NewUnschedulablePodInNamespaceLister(kubeClient client.Interface, namespace string, stopchannel <-chan struct{}) PodLister {
// watch unscheduled pods
selector := fields.ParseSelectorOrDie("spec.nodeName==" + "" + ",status.phase!=" +
string(apiv1.PodSucceeded) + ",status.phase!=" + string(apiv1.PodFailed))
podListWatch := cache.NewListWatchFromClient(kubeClient.CoreV1().RESTClient(), "pods", namespace, selector)
store, reflector := cache.NewNamespaceKeyedIndexerAndReflector(podListWatch, &apiv1.Pod{}, time.Hour)
podLister := v1lister.NewPodLister(store)
go reflector.Run(stopchannel)
return &UnschedulablePodLister{
podLister: podLister,
}
}
// ScheduledPodLister lists scheduled pods.
type ScheduledPodLister struct {
podLister v1lister.PodLister
}
// List returns all scheduled pods.
func (lister *ScheduledPodLister) List() ([]*apiv1.Pod, error) {
return lister.podLister.List(labels.Everything())
}
// NewScheduledPodLister builds ScheduledPodLister
func NewScheduledPodLister(kubeClient client.Interface, stopchannel <-chan struct{}) PodLister {
// watch unscheduled pods
selector := fields.ParseSelectorOrDie("spec.nodeName!=" + "" + ",status.phase!=" +
string(apiv1.PodSucceeded) + ",status.phase!=" + string(apiv1.PodFailed))
podListWatch := cache.NewListWatchFromClient(kubeClient.CoreV1().RESTClient(), "pods", apiv1.NamespaceAll, selector)
store, reflector := cache.NewNamespaceKeyedIndexerAndReflector(podListWatch, &apiv1.Pod{}, time.Hour)
podLister := v1lister.NewPodLister(store)
go reflector.Run(stopchannel)
return &ScheduledPodLister{
podLister: podLister,
}
}
// NodeLister lists nodes.
type NodeLister interface {
List() ([]*apiv1.Node, error)
Get(name string) (*apiv1.Node, error)
}
// nodeLister implementation.
type nodeListerImpl struct {
nodeLister v1lister.NodeLister
filter func(*apiv1.Node) bool
}
// NewReadyNodeLister builds a node lister that returns only ready nodes.
func NewReadyNodeLister(kubeClient client.Interface, stopChannel <-chan struct{}) NodeLister {
return NewNodeLister(kubeClient, IsNodeReadyAndSchedulable, stopChannel)
}
// NewAllNodeLister builds a node lister that returns all nodes (ready and unready).
func NewAllNodeLister(kubeClient client.Interface, stopChannel <-chan struct{}) NodeLister {
return NewNodeLister(kubeClient, nil, stopChannel)
}
// NewNodeLister builds a node lister.
func NewNodeLister(kubeClient client.Interface, filter func(*apiv1.Node) bool, stopChannel <-chan struct{}) NodeLister {
listWatcher := cache.NewListWatchFromClient(kubeClient.CoreV1().RESTClient(), "nodes", apiv1.NamespaceAll, fields.Everything())
store, reflector := cache.NewNamespaceKeyedIndexerAndReflector(listWatcher, &apiv1.Node{}, time.Hour)
nodeLister := v1lister.NewNodeLister(store)
go reflector.Run(stopChannel)
return &nodeListerImpl{
nodeLister: nodeLister,
filter: filter,
}
}
// List returns list of nodes.
func (l *nodeListerImpl) List() ([]*apiv1.Node, error) {
var nodes []*apiv1.Node
var err error
nodes, err = l.nodeLister.List(labels.Everything())
if err != nil {
return []*apiv1.Node{}, err
}
if l.filter != nil {
nodes = filterNodes(nodes, l.filter)
}
return nodes, nil
}
// Get returns the node with the given name.
func (l *nodeListerImpl) Get(name string) (*apiv1.Node, error) {
node, err := l.nodeLister.Get(name)
if err != nil {
return nil, err
}
return node, nil
}
func filterNodes(nodes []*apiv1.Node, predicate func(*apiv1.Node) bool) []*apiv1.Node {
var filtered []*apiv1.Node
for i := range nodes {
if predicate(nodes[i]) {
filtered = append(filtered, nodes[i])
}
}
return filtered
}
// PodDisruptionBudgetLister lists pod disruption budgets.
type PodDisruptionBudgetLister interface {
List() ([]*policyv1.PodDisruptionBudget, error)
}
// PodDisruptionBudgetListerImpl lists pod disruption budgets
type PodDisruptionBudgetListerImpl struct {
pdbLister v1policylister.PodDisruptionBudgetLister
}
// List returns all pdbs
func (lister *PodDisruptionBudgetListerImpl) List() ([]*policyv1.PodDisruptionBudget, error) {
return lister.pdbLister.List(labels.Everything())
}
// NewPodDisruptionBudgetLister builds a pod disruption budget lister.
func NewPodDisruptionBudgetLister(kubeClient client.Interface, stopchannel <-chan struct{}) PodDisruptionBudgetLister {
listWatcher := cache.NewListWatchFromClient(kubeClient.PolicyV1().RESTClient(), "poddisruptionbudgets", apiv1.NamespaceAll, fields.Everything())
store, reflector := cache.NewNamespaceKeyedIndexerAndReflector(listWatcher, &policyv1.PodDisruptionBudget{}, time.Hour)
pdbLister := v1policylister.NewPodDisruptionBudgetLister(store)
go reflector.Run(stopchannel)
return &PodDisruptionBudgetListerImpl{
pdbLister: pdbLister,
}
}
// NewDaemonSetLister builds a daemonset lister.
func NewDaemonSetLister(kubeClient client.Interface, stopchannel <-chan struct{}) v1appslister.DaemonSetLister {
listWatcher := cache.NewListWatchFromClient(kubeClient.AppsV1().RESTClient(), "daemonsets", apiv1.NamespaceAll, fields.Everything())
store, reflector := cache.NewNamespaceKeyedIndexerAndReflector(listWatcher, &appsv1.DaemonSet{}, time.Hour)
lister := v1appslister.NewDaemonSetLister(store)
go reflector.Run(stopchannel)
return lister
}
// NewReplicationControllerLister builds a replicationcontroller lister.
func NewReplicationControllerLister(kubeClient client.Interface, stopchannel <-chan struct{}) v1lister.ReplicationControllerLister {
listWatcher := cache.NewListWatchFromClient(kubeClient.CoreV1().RESTClient(), "replicationcontrollers", apiv1.NamespaceAll, fields.Everything())
store, reflector := cache.NewNamespaceKeyedIndexerAndReflector(listWatcher, &apiv1.ReplicationController{}, time.Hour)
lister := v1lister.NewReplicationControllerLister(store)
go reflector.Run(stopchannel)
return lister
}
// NewJobLister builds a job lister.
func NewJobLister(kubeClient client.Interface, stopchannel <-chan struct{}) v1batchlister.JobLister {
listWatcher := cache.NewListWatchFromClient(kubeClient.BatchV1().RESTClient(), "jobs", apiv1.NamespaceAll, fields.Everything())
store, reflector := cache.NewNamespaceKeyedIndexerAndReflector(listWatcher, &batchv1.Job{}, time.Hour)
lister := v1batchlister.NewJobLister(store)
go reflector.Run(stopchannel)
return lister
}
// NewReplicaSetLister builds a replicaset lister.
func NewReplicaSetLister(kubeClient client.Interface, stopchannel <-chan struct{}) v1appslister.ReplicaSetLister {
listWatcher := cache.NewListWatchFromClient(kubeClient.AppsV1().RESTClient(), "replicasets", apiv1.NamespaceAll, fields.Everything())
store, reflector := cache.NewNamespaceKeyedIndexerAndReflector(listWatcher, &appsv1.ReplicaSet{}, time.Hour)
lister := v1appslister.NewReplicaSetLister(store)
go reflector.Run(stopchannel)
return lister
}
// NewStatefulSetLister builds a statefulset lister.
func NewStatefulSetLister(kubeClient client.Interface, stopchannel <-chan struct{}) v1appslister.StatefulSetLister {
listWatcher := cache.NewListWatchFromClient(kubeClient.AppsV1().RESTClient(), "statefulsets", apiv1.NamespaceAll, fields.Everything())
store, reflector := cache.NewNamespaceKeyedIndexerAndReflector(listWatcher, &appsv1.StatefulSet{}, time.Hour)
lister := v1appslister.NewStatefulSetLister(store)
go reflector.Run(stopchannel)
return lister
}
// NewConfigMapListerForNamespace builds a configmap lister for the passed namespace (including all).
func NewConfigMapListerForNamespace(kubeClient client.Interface, stopchannel <-chan struct{},
namespace string) v1lister.ConfigMapLister {
listWatcher := cache.NewListWatchFromClient(kubeClient.CoreV1().RESTClient(), "configmaps", namespace, fields.Everything())
store, reflector := cache.NewNamespaceKeyedIndexerAndReflector(listWatcher, &apiv1.ConfigMap{}, time.Hour)
lister := v1lister.NewConfigMapLister(store)
go reflector.Run(stopchannel)
return lister
}