-
Notifications
You must be signed in to change notification settings - Fork 103
/
pods.go
382 lines (320 loc) · 11.1 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
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
371
372
373
374
375
376
377
378
379
380
381
382
/*
Copyright 2022 The Katalyst 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 native
import (
"fmt"
"strings"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/apis/core/v1/helper/qos"
kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
"github.com/kubewharf/katalyst-core/pkg/consts"
)
var GetPodHostIP = func(pod *v1.Pod) (string, error) {
if pod == nil {
return "", fmt.Errorf("empty pod")
}
hostIP := pod.Status.HostIP
if len(hostIP) == 0 {
return "", fmt.Errorf("empty hostIP")
}
return hostIP, nil
}
// PodAnnotationFilter is used to filter pods annotated with a pair of specific key and value
func PodAnnotationFilter(pod *v1.Pod, key, value string) bool {
if pod == nil || pod.Annotations == nil {
return false
}
return pod.Annotations[key] == value
}
// FilterPods filter pods that filter func return true.
func FilterPods(pods []*v1.Pod, filterFunc func(*v1.Pod) (bool, error)) []*v1.Pod {
var filtered []*v1.Pod
for _, pod := range pods {
if pod == nil {
continue
}
if ok, err := filterFunc(pod); err != nil {
klog.Errorf("filter pod %v err: %v", pod.Name, err)
} else if ok {
filtered = append(filtered, pod)
}
}
return filtered
}
// SumUpPodRequestResources sum up resources in all containers request
// init container is included (count on the max request of all init containers)
func SumUpPodRequestResources(pod *v1.Pod) v1.ResourceList {
res := make(v1.ResourceList)
sumRequests := func(containers []v1.Container) {
for _, container := range containers {
res = AddResources(res, container.Resources.Requests)
}
if pod.Spec.Overhead != nil {
res = AddResources(res, pod.Spec.Overhead)
}
}
sumRequests(pod.Spec.Containers)
for _, container := range pod.Spec.InitContainers {
for resourceName := range container.Resources.Requests {
quantity := container.Resources.Requests[resourceName].DeepCopy()
if origin, ok := res[resourceName]; !ok || (&origin).Value() < quantity.Value() {
res[resourceName] = quantity
}
}
}
return res
}
// SumUpPodLimitResources sum up resources in all containers request
// init container is included (count on the max limit of all init containers)
func SumUpPodLimitResources(pod *v1.Pod) v1.ResourceList {
res := make(v1.ResourceList)
sumLimits := func(containers []v1.Container) {
for _, container := range containers {
res = AddResources(res, container.Resources.Limits)
}
if pod.Spec.Overhead != nil {
res = AddResources(res, pod.Spec.Overhead)
}
}
sumLimits(pod.Spec.Containers)
for _, container := range pod.Spec.InitContainers {
for resourceName := range container.Resources.Limits {
quantity := container.Resources.Limits[resourceName].DeepCopy()
if origin, ok := res[resourceName]; !ok || (&origin).Value() < quantity.Value() {
res[resourceName] = quantity
}
}
}
return res
}
func PodAndContainersAreTerminal(pod *v1.Pod) (containersTerminal, podWorkerTerminal bool) {
status := pod.Status
// A pod transitions into failed or succeeded from either container lifecycle (RestartNever container
// fails) or due to external events like deletion or eviction. A terminal pod *should* have no running
// containers, but to know that the pod has completed its lifecycle you must wait for containers to also
// be terminal.
containersTerminal = containerNotRunning(status.ContainerStatuses)
// The kubelet must accept config changes from the pod spec until it has reached a point where changes would
// have no effect on any running container.
podWorkerTerminal = status.Phase == v1.PodFailed || status.Phase == v1.PodSucceeded || (pod.DeletionTimestamp != nil && containersTerminal)
return
}
// PodIsTerminated returns whether the pod is at terminal state.
func PodIsTerminated(pod *v1.Pod) bool {
if pod == nil {
return true
}
_, podWorkerTerminal := PodAndContainersAreTerminal(pod)
return podWorkerTerminal
}
// PodIsReady returns whether the pod is at ready state.
func PodIsReady(pod *v1.Pod) bool {
if len(pod.Spec.Containers) != len(pod.Status.ContainerStatuses) {
return false
}
for _, containerStatus := range pod.Status.ContainerStatuses {
if !containerStatus.Ready {
return false
}
}
return true
}
// PodIsActive returns whether the pod is not terminated.
func PodIsActive(pod *v1.Pod) bool {
return !PodIsTerminated(pod)
}
// FilterOutSkipEvictionPods return pods should be candidates to evict
// including native critical pods and user-defined filtered pods
func FilterOutSkipEvictionPods(pods []*v1.Pod, filterOutAnnotations, filterOutLabels sets.String) []*v1.Pod {
var filteredPods []*v1.Pod
filter:
for _, p := range pods {
if p == nil || kubelettypes.IsCriticalPod(p) {
continue
}
for key := range p.Annotations {
if filterOutAnnotations.Has(key) {
continue filter
}
}
for key := range p.Labels {
if filterOutLabels.Has(key) {
continue filter
}
}
filteredPods = append(filteredPods, p)
}
return filteredPods
}
// GeneratePodContainerName return a unique key for a container in a pod
func GeneratePodContainerName(podName, containerName string) consts.PodContainerName {
return consts.PodContainerName(podName + "," + containerName)
}
// ParsePodContainerName parse key and return pod name and container name
func ParsePodContainerName(key consts.PodContainerName) (string, string, error) {
containerKeys := strings.Split(string(key), ",")
if len(containerKeys) != 2 {
err := fmt.Errorf("split result's length mismatch")
return "", "", err
}
return containerKeys[0], containerKeys[1], nil
}
// GenerateContainerName return a unique key for a container
func GenerateContainerName(containerName string) consts.ContainerName {
return consts.ContainerName(containerName)
}
// ParseContainerName parse key and return container name
func ParseContainerName(key consts.ContainerName) string {
return string(key)
}
// CheckQosClassChanged checks whether the pod's QosClass will change if annotationResources are applied to this pod
func CheckQosClassChanged(resources map[string]v1.ResourceRequirements, pod *v1.Pod) (bool, error) {
if pod == nil {
return false, fmt.Errorf("pod is nil")
}
podCopy := &v1.Pod{}
podCopy.Spec.Containers = DeepCopyPodContainers(pod)
ApplyPodResources(resources, podCopy)
return qos.GetPodQOS(podCopy) != qos.GetPodQOS(pod), nil
}
// ApplyPodResources is used to apply map[string]v1.ResourceRequirements to the given pod,
// and ignore the container-names / resource-names that not appear in the given map param
func ApplyPodResources(resources map[string]v1.ResourceRequirements, pod *v1.Pod) {
for i := 0; i < len(pod.Spec.Containers); i++ {
if containerResource, ok := resources[pod.Spec.Containers[i].Name]; ok {
if pod.Spec.Containers[i].Resources.Requests == nil {
pod.Spec.Containers[i].Resources.Requests = v1.ResourceList{}
}
if containerResource.Requests != nil {
for resourceName, quantity := range containerResource.Requests {
pod.Spec.Containers[i].Resources.Requests[resourceName] = quantity
}
}
if pod.Spec.Containers[i].Resources.Limits == nil {
pod.Spec.Containers[i].Resources.Limits = v1.ResourceList{}
}
if containerResource.Limits != nil {
for resourceName, quantity := range containerResource.Limits {
pod.Spec.Containers[i].Resources.Limits[resourceName] = quantity
}
}
}
}
}
func GetPodNamespaceNameKeyMap(podList []*v1.Pod) map[string]*v1.Pod {
podMap := make(map[string]*v1.Pod, len(podList))
for _, pod := range podList {
if pod == nil {
continue
}
key := GenerateUniqObjectNameKey(pod)
if oldPod, ok := podMap[key]; ok && oldPod.CreationTimestamp.After(pod.CreationTimestamp.Time) {
continue
}
podMap[key] = pod
}
return podMap
}
// IsAssignedPod selects pods that are assigned (scheduled and running).
func IsAssignedPod(pod *v1.Pod) bool {
return len(pod.Spec.NodeName) != 0
}
// ParseHostPortsForPod gets host ports from pod spec
func ParseHostPortsForPod(pod *v1.Pod, portName string) []int32 {
var res []int32
for i := range pod.Spec.Containers {
res = append(res, ParseHostPortsForContainer(&pod.Spec.Containers[i], portName)...)
}
return res
}
// GetNamespacedNameListFromSlice returns a slice of namespaced name
func GetNamespacedNameListFromSlice(podSlice []*v1.Pod) []string {
namespacedNameList := make([]string, 0, len(podSlice))
for _, pod := range podSlice {
namespacedNameList = append(namespacedNameList, pod.Namespace+"/"+pod.Name)
}
return namespacedNameList
}
// CheckDaemonPod returns true if pod is for DaemonSet
func CheckDaemonPod(pod *v1.Pod) bool {
for _, owner := range pod.OwnerReferences {
if owner.Kind == "DaemonSet" {
return true
}
}
return false
}
// GetContainerID gets container id from pod status by container name
func GetContainerID(pod *v1.Pod, containerName string) (string, error) {
if pod == nil {
return "", fmt.Errorf("empty pod")
}
for _, containerStatus := range pod.Status.ContainerStatuses {
if containerStatus.Name == containerName {
if containerStatus.ContainerID == "" {
return "", fmt.Errorf("empty container id in container statues of pod")
}
return TrimContainerIDPrefix(containerStatus.ContainerID), nil
}
}
return "", fmt.Errorf("container %s container id not found", containerName)
}
// GetContainerEnvs gets container envs from pod spec by container name and envs name
func GetContainerEnvs(pod *v1.Pod, containerName string, envs ...string) map[string]string {
if pod == nil {
return nil
}
envSet := sets.NewString(envs...)
envMap := make(map[string]string)
for _, container := range pod.Spec.Containers {
if container.Name != containerName {
continue
}
for _, env := range container.Env {
if envSet.Has(env.Name) {
envMap[env.Name] = env.Value
}
}
}
return envMap
}
// GetPodCondition extracts the given condition for the given pod
func GetPodCondition(pod *v1.Pod, conditionType v1.PodConditionType) (v1.PodCondition, bool) {
for _, condition := range pod.Status.Conditions {
if condition.Type == conditionType {
return condition, true
}
}
return v1.PodCondition{}, false
}
// DeepCopyPodContainers returns a deep-copied objects for v1.Container slice
func DeepCopyPodContainers(pod *v1.Pod) (containers []v1.Container) {
in, out := &pod.Spec.Containers, &containers
*out = make([]v1.Container, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
return
}
// FilterPodAnnotations returns the needed annotations for the given pod.
func FilterPodAnnotations(filterKeys []string, pod *v1.Pod) map[string]string {
netAttrMap := make(map[string]string)
for _, attrKey := range filterKeys {
if attrVal, ok := pod.GetAnnotations()[attrKey]; ok {
netAttrMap[attrKey] = attrVal
}
}
return netAttrMap
}