forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listers.go
246 lines (214 loc) · 9.88 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
/*
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"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/tools/cache"
apiv1 "k8s.io/kubernetes/pkg/api/v1"
policyv1 "k8s.io/kubernetes/pkg/apis/policy/v1beta1"
client "k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
v1lister "k8s.io/kubernetes/pkg/client/listers/core/v1"
v1policylister "k8s.io/kubernetes/pkg/client/listers/policy/v1beta1"
)
// ListerRegistry is a registry providing various listers to list pods or nodes matching conditions
type ListerRegistry interface {
AllNodeLister() *AllNodeLister
ReadyNodeLister() *ReadyNodeLister
ScheduledPodLister() *ScheduledPodLister
UnschedulablePodLister() *UnschedulablePodLister
PodDisruptionBudgetLister() *PodDisruptionBudgetLister
}
type listerRegistryImpl struct {
allNodeLister *AllNodeLister
readyNodeLister *ReadyNodeLister
scheduledPodLister *ScheduledPodLister
unschedulablePodLister *UnschedulablePodLister
podDisruptionBudgetLister *PodDisruptionBudgetLister
}
// NewListerRegistry returns a registry providing various listers to list pods or nodes matching conditions
func NewListerRegistry(allNode *AllNodeLister, readyNode *ReadyNodeLister, scheduledPod *ScheduledPodLister,
unschedulablePod *UnschedulablePodLister, podDisruptionBudgetLister *PodDisruptionBudgetLister) ListerRegistry {
return listerRegistryImpl{
allNodeLister: allNode,
readyNodeLister: readyNode,
scheduledPodLister: scheduledPod,
unschedulablePodLister: unschedulablePod,
podDisruptionBudgetLister: podDisruptionBudgetLister,
}
}
// 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)
return NewListerRegistry(allNodeLister, readyNodeLister, scheduledPodLister,
unschedulablePodLister, podDisruptionBudgetLister)
}
// AllNodeLister returns the AllNodeLister registered to this registry
func (r listerRegistryImpl) AllNodeLister() *AllNodeLister {
return r.allNodeLister
}
// ReadyNodeLister returns the ReadyNodeLister registered to this registry
func (r listerRegistryImpl) ReadyNodeLister() *ReadyNodeLister {
return r.readyNodeLister
}
// ScheduledPodLister returns the ScheduledPodLister registered to this registry
func (r listerRegistryImpl) ScheduledPodLister() *ScheduledPodLister {
return r.scheduledPodLister
}
// UnschedulablePodLister returns the UnschedulablePodLister registered to this registry
func (r listerRegistryImpl) UnschedulablePodLister() *UnschedulablePodLister {
return r.unschedulablePodLister
}
// PodDisruptionBudgetLister returns the podDisruptionBudgetLister registered to this registry
func (r listerRegistryImpl) PodDisruptionBudgetLister() *PodDisruptionBudgetLister {
return r.podDisruptionBudgetLister
}
// 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 := apiv1.GetPodCondition(&pod.Status, apiv1.PodScheduled)
if condition != nil && condition.Status == apiv1.ConditionFalse && condition.Reason == "Unschedulable" {
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{}) *UnschedulablePodLister {
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{}) *UnschedulablePodLister {
// watch unscheduled pods
selector := fields.ParseSelectorOrDie("spec.nodeName==" + "" + ",status.phase!=" +
string(apiv1.PodSucceeded) + ",status.phase!=" + string(apiv1.PodFailed))
podListWatch := cache.NewListWatchFromClient(kubeClient.Core().RESTClient(), "pods", namespace, selector)
store := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
podLister := v1lister.NewPodLister(store)
podReflector := cache.NewReflector(podListWatch, &apiv1.Pod{}, store, time.Hour)
podReflector.RunUntil(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{}) *ScheduledPodLister {
// watch unscheduled pods
selector := fields.ParseSelectorOrDie("spec.nodeName!=" + "" + ",status.phase!=" +
string(apiv1.PodSucceeded) + ",status.phase!=" + string(apiv1.PodFailed))
podListWatch := cache.NewListWatchFromClient(kubeClient.Core().RESTClient(), "pods", apiv1.NamespaceAll, selector)
store := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
podLister := v1lister.NewPodLister(store)
podReflector := cache.NewReflector(podListWatch, &apiv1.Pod{}, store, time.Hour)
podReflector.RunUntil(stopchannel)
return &ScheduledPodLister{
podLister: podLister,
}
}
// ReadyNodeLister lists ready nodes.
type ReadyNodeLister struct {
nodeLister v1lister.NodeLister
}
// List returns ready nodes.
func (readyNodeLister *ReadyNodeLister) List() ([]*apiv1.Node, error) {
nodes, err := readyNodeLister.nodeLister.List(labels.Everything())
if err != nil {
return []*apiv1.Node{}, err
}
readyNodes := make([]*apiv1.Node, 0, len(nodes))
for _, node := range nodes {
if IsNodeReadyAndSchedulable(node) {
readyNodes = append(readyNodes, node)
}
}
return readyNodes, nil
}
// NewReadyNodeLister builds a node lister.
func NewReadyNodeLister(kubeClient client.Interface, stopChannel <-chan struct{}) *ReadyNodeLister {
listWatcher := cache.NewListWatchFromClient(kubeClient.Core().RESTClient(), "nodes", apiv1.NamespaceAll, fields.Everything())
store := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
nodeLister := v1lister.NewNodeLister(store)
reflector := cache.NewReflector(listWatcher, &apiv1.Node{}, store, time.Hour)
reflector.RunUntil(stopChannel)
return &ReadyNodeLister{
nodeLister: nodeLister,
}
}
// AllNodeLister lists all nodes
type AllNodeLister struct {
nodeLister v1lister.NodeLister
}
// List returns all nodes
func (allNodeLister *AllNodeLister) List() ([]*apiv1.Node, error) {
nodes, err := allNodeLister.nodeLister.List(labels.Everything())
if err != nil {
return []*apiv1.Node{}, err
}
allNodes := make([]*apiv1.Node, 0, len(nodes))
for _, node := range nodes {
allNodes = append(allNodes, node)
}
return allNodes, nil
}
// NewAllNodeLister builds a node lister that returns all nodes (ready and unready)
func NewAllNodeLister(kubeClient client.Interface, stopchannel <-chan struct{}) *AllNodeLister {
listWatcher := cache.NewListWatchFromClient(kubeClient.Core().RESTClient(), "nodes", apiv1.NamespaceAll, fields.Everything())
store := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
nodeLister := v1lister.NewNodeLister(store)
reflector := cache.NewReflector(listWatcher, &apiv1.Node{}, store, time.Hour)
reflector.RunUntil(stopchannel)
return &AllNodeLister{
nodeLister: nodeLister,
}
}
// PodDisruptionBudgetLister lists all pod disruption budgets
type PodDisruptionBudgetLister struct {
pdbLister v1policylister.PodDisruptionBudgetLister
}
// List returns all nodes
func (lister *PodDisruptionBudgetLister) 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.Policy().RESTClient(), "poddisruptionbudgets", apiv1.NamespaceAll, fields.Everything())
store := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
pdbLister := v1policylister.NewPodDisruptionBudgetLister(store)
reflector := cache.NewReflector(listWatcher, &policyv1.PodDisruptionBudget{}, store, time.Hour)
reflector.RunUntil(stopchannel)
return &PodDisruptionBudgetLister{
pdbLister: pdbLister,
}
}