-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkloads.go
More file actions
134 lines (119 loc) · 4.13 KB
/
workloads.go
File metadata and controls
134 lines (119 loc) · 4.13 KB
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
package main
import (
"context"
"fmt"
"sort"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func printWorkloadsTable(namespace string) error {
var err error
workloads, err = getAllWorkloads()
if err != nil {
return err
}
printSelectedWorkloadsTable(workloads, namespace)
return nil
}
func getDeploymentsWorkloadPriority(deployment *appsv1.Deployment) int32 {
// Get the priority of actual pods
pods, err := kubeClient.CoreV1().Pods(deployment.Namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("app=%s", deployment.Name),
})
if err != nil {
fmt.Printf("Failed to get pods for deployment %s/%s, err: %v", deployment.Namespace, deployment.Name, err)
return 0
}
if len(pods.Items) == 0 {
return 0
}
// Get the priority of the first pod, we do not support multiple priority in one deployment now
return getPodPriority(&pods.Items[0])
}
func getStatefulSetWorkloadPriority(statefulSet *appsv1.StatefulSet) int32 {
// Get the priority of actual pods
pods, err := kubeClient.CoreV1().Pods(statefulSet.Namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("app=%s", statefulSet.Name),
})
if err != nil {
fmt.Printf("Failed to get pods for statefulSet %s/%s, err: %v", statefulSet.Namespace, statefulSet.Name, err)
return 0
}
if len(pods.Items) == 0 {
return 0
}
// Get the priority of the first pod, we do not support multiple priority in one deployment now
return getPodPriority(&pods.Items[0])
}
func getPodPriority(pod *corev1.Pod) int32 {
if pod.Spec.PriorityClassName == "" {
return 0
}
priorityClass, err := kubeClient.SchedulingV1().PriorityClasses().Get(context.TODO(), pod.Spec.PriorityClassName, metav1.GetOptions{})
if err != nil {
fmt.Printf("Failed to get priority class %s, err: %v", pod.Spec.PriorityClassName, err)
return 0
}
if priorityClass == nil {
fmt.Printf("Priority class %s not found", pod.Spec.PriorityClassName)
return 0
}
return priorityClass.Value
}
func getAllWorkloads() ([]Workload, error) {
var newWorkloads []Workload
deployments, err := kubeClient.AppsV1().Deployments(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, d := range deployments.Items {
// Get the priority of the deployment
priority := getDeploymentsWorkloadPriority(&d)
newWorkloads = append(newWorkloads, Workload{
Name: d.Name,
Namespace: d.Namespace,
Kind: WorkloadDeployment,
Replicas: *d.Spec.Replicas,
Available: d.Status.AvailableReplicas,
Ready: CheckDeploymentIsReady(&d),
MigratePatched: CheckWorkloadIsMigrated(d.Spec.Template.Spec.NodeSelector, d.Spec.Template.Spec.Tolerations),
ARMPatched: HasArm64Preference(d.Spec.Template.Spec.Affinity) || CheckWorkloadHasARM64Toleration(d.Spec.Template.Spec.Tolerations),
deployment: &d,
Priority: priority,
})
}
statefulSets, err := kubeClient.AppsV1().StatefulSets(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, s := range statefulSets.Items {
// Get the priority of the statefulSet
priority := getStatefulSetWorkloadPriority(&s)
newWorkloads = append(newWorkloads, Workload{
Name: s.Name,
Namespace: s.Namespace,
Kind: WorkloadStatefulSet,
Replicas: *s.Spec.Replicas,
Available: s.Status.ReadyReplicas,
Ready: CheckStatefulSetIsReady(&s),
MigratePatched: CheckWorkloadIsMigrated(s.Spec.Template.Spec.NodeSelector, s.Spec.Template.Spec.Tolerations),
ARMPatched: HasArm64Preference(s.Spec.Template.Spec.Affinity) || CheckWorkloadHasARM64Toleration(s.Spec.Template.Spec.Tolerations),
statefulSet: &s,
Priority: priority,
})
}
sort.Slice(newWorkloads, func(i, j int) bool {
wi, wj := newWorkloads[i], newWorkloads[j]
// Sort by priority first
if wi.Namespace != wj.Namespace {
return wi.Namespace < wj.Namespace
}
// Then by workload kind
if wi.Kind != wj.Kind {
return wi.Kind < wj.Kind
}
return wi.Name < wj.Name
})
return newWorkloads, nil
}