forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpods.go
195 lines (171 loc) · 5.17 KB
/
pods.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
/*
Copyright 2020 The Knative 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 resources
import (
"sort"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
corev1listers "k8s.io/client-go/listers/core/v1"
"knative.dev/serving/pkg/apis/serving"
)
// PodAccessor provides access to various dimensions of pods listing
// and querying for a given bound revision.
type PodAccessor struct {
podsLister corev1listers.PodNamespaceLister
selector labels.Selector
}
// NewPodAccessor creates a PodAccessor implementation that counts
// pods for a namespace/revision.
func NewPodAccessor(lister corev1listers.PodLister, namespace, revisionName string) PodAccessor {
return PodAccessor{
podsLister: lister.Pods(namespace),
selector: labels.SelectorFromSet(labels.Set{
serving.RevisionLabelKey: revisionName,
}),
}
}
// PodCountsByState returns number of pods for the revision grouped by their state, that is
// of interest to knative (e.g. ignoring failed or terminated pods).
func (pa PodAccessor) PodCountsByState() (ready, notReady, pending, terminating int, err error) {
pods, err := pa.podsLister.List(pa.selector)
if err != nil {
return 0, 0, 0, 0, err
}
for _, p := range pods {
switch p.Status.Phase {
case corev1.PodPending:
pending++
notReady++
case corev1.PodRunning:
if p.DeletionTimestamp != nil {
terminating++
notReady++
continue
}
if podReady(p) {
ready++
} else {
notReady++
}
}
}
return
}
// ReadyCount implements EndpointsCounter.
func (pa PodAccessor) ReadyCount() (int, error) {
r, _, _, _, err := pa.PodCountsByState()
return r, err
}
// NotReadyCount implements EndpointsCounter.
func (pa PodAccessor) NotReadyCount() (int, error) {
_, nr, _, _, err := pa.PodCountsByState()
return nr, err
}
// PodFilter provides a way to filter pods for a revision.
// Returning true, means that pod should be kept.
type PodFilter func(p *corev1.Pod) bool
// PodTransformer provides a way to do something with the pod
// that has been selected by all the filters.
// For example pod transformer may extract a field and store it in
// internal state.
type PodTransformer func(p *corev1.Pod)
// ProcessPods filters all the pods using provided pod filters and then if the pod
// is selected, applies the transformer to it.
func (pa PodAccessor) ProcessPods(pt PodTransformer, pfs ...PodFilter) error {
pods, err := pa.podsLister.List(pa.selector)
if err != nil {
return err
}
for _, p := range pods {
if applyFilters(p, pfs...) {
pt(p)
}
}
return nil
}
func applyFilters(p *corev1.Pod, pfs ...PodFilter) bool {
for _, pf := range pfs {
if !pf(p) {
return false
}
}
return true
}
func podRunning(p *corev1.Pod) bool {
return p.Status.Phase == corev1.PodRunning && p.DeletionTimestamp == nil
}
// podReady checks whether pod's Ready status is True.
func podReady(p *corev1.Pod) bool {
for _, cond := range p.Status.Conditions {
if cond.Type == corev1.PodReady {
return cond.Status == corev1.ConditionTrue
}
}
// No ready status, probably not even running.
return false
}
type podIPByAgeSorter struct {
pods []*corev1.Pod
}
func (s *podIPByAgeSorter) process(p *corev1.Pod) {
s.pods = append(s.pods, p)
}
func (s *podIPByAgeSorter) get() []string {
if len(s.pods) > 1 {
// This results in a few reflection calls, which we can easily avoid.
sort.SliceStable(s.pods, func(i, j int) bool {
return s.pods[i].Status.StartTime.Before(s.pods[j].Status.StartTime)
})
}
ret := make([]string, 0, len(s.pods))
for _, p := range s.pods {
ret = append(ret, p.Status.PodIP)
}
return ret
}
// PodIPsByAge returns the list of running pods (terminating
// and non-running are excluded) IP addresses, sorted descending by pod age.
func (pa PodAccessor) PodIPsByAge() ([]string, error) {
ps := podIPByAgeSorter{}
if err := pa.ProcessPods(ps.process, podRunning, podReady); err != nil {
return nil, err
}
return ps.get(), nil
}
type podIPWithCutoffProcessor struct {
cutOff time.Duration
now time.Time
older []string
younger []string
}
func (pp *podIPWithCutoffProcessor) process(p *corev1.Pod) {
// If pod is at least as old as cutoff.
if pp.now.Sub(p.Status.StartTime.Time) >= pp.cutOff {
pp.older = append(pp.older, p.Status.PodIP)
} else {
pp.younger = append(pp.younger, p.Status.PodIP)
}
}
// PodIPsSplitByAge returns all the ready Pod IPs in two lists: older than cutoff and younger
// than cutoff.
func (pa PodAccessor) PodIPsSplitByAge(cutOff time.Duration, now time.Time) (older, younger []string, err error) {
pp := podIPWithCutoffProcessor{
now: now,
cutOff: cutOff,
}
if err := pa.ProcessPods(pp.process, podRunning, podReady); err != nil {
return nil, nil, err
}
return pp.older, pp.younger, nil
}