-
Notifications
You must be signed in to change notification settings - Fork 0
/
top_node.go
306 lines (262 loc) · 7.77 KB
/
top_node.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
// Copyright 2018 The Kubeflow 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 commands
import (
"fmt"
"os"
"strconv"
"strings"
"text/tabwriter"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
)
var (
showDetails bool
)
type NodeInfo struct {
node v1.Node
pods []v1.Pod
}
func NewTopNodeCommand() *cobra.Command {
var command = &cobra.Command{
Use: "node",
Short: "Display Resource (GPU) usage of nodes.",
Run: func(cmd *cobra.Command, args []string) {
setupKubeconfig()
client, err := initKubeClient()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
allPods, err = acquireAllActivePods(client)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
nd := newNodeDescriber(client, allPods)
nodeInfos, err := nd.getAllNodeInfos()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
displayTopNode(nodeInfos)
},
}
command.Flags().BoolVarP(&showDetails, "details", "d", false, "Display details")
return command
}
type NodeDescriber struct {
client *kubernetes.Clientset
allPods []v1.Pod
}
func newNodeDescriber(client *kubernetes.Clientset, pods []v1.Pod) *NodeDescriber {
return &NodeDescriber{
client: client,
allPods: pods,
}
}
func (d *NodeDescriber) getAllNodeInfos() ([]NodeInfo, error) {
nodeInfoList := []NodeInfo{}
nodeList, err := d.client.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
return nodeInfoList, err
}
for _, node := range nodeList.Items {
pods := d.getPodsFromNode(node)
nodeInfoList = append(nodeInfoList, NodeInfo{
node: node,
pods: pods,
})
}
return nodeInfoList, nil
}
func (d *NodeDescriber) getPodsFromNode(node v1.Node) []v1.Pod {
pods := []v1.Pod{}
for _, pod := range d.allPods {
if pod.Spec.NodeName == node.Name {
pods = append(pods, pod)
}
}
return pods
}
func displayTopNode(nodes []NodeInfo) {
if showDetails {
displayTopNodeDetails(nodes)
} else {
displayTopNodeSummary(nodes)
}
}
func displayTopNodeSummary(nodeInfos []NodeInfo) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
var (
totalGPUsInCluster int64
allocatedGPUsInCluster int64
)
fmt.Fprintf(w, "NAME\tIPADDRESS\tROLE\tGPU(Total)\tGPU(Allocated)\n")
for _, nodeInfo := range nodeInfos {
// Skip NotReady node
//if ! isNodeReady(nodeInfo.node) {
// continue
//}
totalGPU, allocatedGPU := calculateNodeGPU(nodeInfo)
totalGPUsInCluster += totalGPU
allocatedGPUsInCluster += allocatedGPU
address := getNodeInternalAddress(nodeInfo.node)
role := strings.Join(findNodeRoles(&nodeInfo.node), ",")
if len(role) == 0 {
role = "<none>"
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", nodeInfo.node.Name,
address,
role,
strconv.FormatInt(totalGPU, 10),
strconv.FormatInt(allocatedGPU, 10))
}
fmt.Fprintf(w, "-----------------------------------------------------------------------------------------\n")
fmt.Fprintf(w, "Allocated/Total GPUs In Cluster:\n")
log.Debugf("gpu: %s, allocated GPUs %s", strconv.FormatInt(totalGPUsInCluster, 10),
strconv.FormatInt(allocatedGPUsInCluster, 10))
var gpuUsage float64 = 0
if totalGPUsInCluster > 0 {
gpuUsage = float64(allocatedGPUsInCluster) / float64(totalGPUsInCluster) * 100
}
fmt.Fprintf(w, "%s/%s (%d%%)\t\n",
strconv.FormatInt(allocatedGPUsInCluster, 10),
strconv.FormatInt(totalGPUsInCluster, 10),
int64(gpuUsage))
// fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", ...)
_ = w.Flush()
}
func displayTopNodeDetails(nodeInfos []NodeInfo) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
var (
totalGPUsInCluster int64
allocatedGPUsInCluster int64
)
fmt.Fprintf(w, "\n")
for _, nodeInfo := range nodeInfos {
// Skip NotReady node
//if ! isNodeReady(nodeInfo.node) {
// continue
//}
totalGPU, allocatedGPU := calculateNodeGPU(nodeInfo)
totalGPUsInCluster += totalGPU
allocatedGPUsInCluster += allocatedGPU
address := getNodeInternalAddress(nodeInfo.node)
role := strings.Join(findNodeRoles(&nodeInfo.node), ",")
if len(role) == 0 {
role = "<none>"
}
fmt.Fprintf(w, "\n")
fmt.Fprintf(w, "NAME:\t%s\n", nodeInfo.node.Name)
fmt.Fprintf(w, "IPADDRESS:\t%s\n", address)
fmt.Fprintf(w, "ROLE:\t%s\n", role)
pods := gpuPods(nodeInfo.pods)
if len(pods) > 0 {
fmt.Fprintf(w, "\n")
fmt.Fprintf(w, "NAMESPACE\tNAME\tGPU REQUESTS\tGPU LIMITS\n")
for _, pod := range pods {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", pod.Namespace,
pod.Name,
strconv.FormatInt(gpuInPod(pod), 10),
strconv.FormatInt(gpuInPod(pod), 10))
}
fmt.Fprintf(w, "\n")
}
var gpuUsageInNode float64 = 0
if totalGPU > 0 {
gpuUsageInNode = float64(allocatedGPU) / float64(totalGPU) * 100
} else {
fmt.Fprintf(w, "\n")
}
fmt.Fprintf(w, "Total GPUs In Node %s:\t%s \t\n", nodeInfo.node.Name, strconv.FormatInt(totalGPU, 10))
fmt.Fprintf(w, "Allocated GPUs In Node %s:\t%s (%d%%)\t\n", nodeInfo.node.Name, strconv.FormatInt(allocatedGPU, 10), int64(gpuUsageInNode))
log.Debugf("gpu: %s, allocated GPUs %s", strconv.FormatInt(totalGPU, 10),
strconv.FormatInt(allocatedGPU, 10))
fmt.Fprintf(w, "-----------------------------------------------------------------------------------------\n")
}
fmt.Fprintf(w, "\n")
fmt.Fprintf(w, "\n")
fmt.Fprintf(w, "Allocated/Total GPUs In Cluster:\t")
log.Debugf("gpu: %s, allocated GPUs %s", strconv.FormatInt(totalGPUsInCluster, 10),
strconv.FormatInt(allocatedGPUsInCluster, 10))
var gpuUsage float64 = 0
if totalGPUsInCluster > 0 {
gpuUsage = float64(allocatedGPUsInCluster) / float64(totalGPUsInCluster) * 100
}
fmt.Fprintf(w, "%s/%s (%d%%)\t\n",
strconv.FormatInt(allocatedGPUsInCluster, 10),
strconv.FormatInt(totalGPUsInCluster, 10),
int64(gpuUsage))
// fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", ...)
_ = w.Flush()
}
// calculate the GPU count of each node
func calculateNodeGPU(nodeInfo NodeInfo) (totalGPU int64, allocatedGPU int64) {
node := nodeInfo.node
totalGPU = gpuInNode(node)
// allocatedGPU = gpuInPod()
for _, pod := range nodeInfo.pods {
allocatedGPU += gpuInPod(pod)
}
return totalGPU, allocatedGPU
}
func isMasterNode(node v1.Node) bool {
if _, ok := node.Labels[masterLabelRole]; ok {
return true
}
return false
}
// findNodeRoles returns the roles of a given node.
// The roles are determined by looking for:
// * a node-role.kubernetes.io/<role>="" label
// * a kubernetes.io/role="<role>" label
func findNodeRoles(node *v1.Node) []string {
roles := sets.NewString()
for k, v := range node.Labels {
switch {
case strings.HasPrefix(k, labelNodeRolePrefix):
if role := strings.TrimPrefix(k, labelNodeRolePrefix); len(role) > 0 {
roles.Insert(role)
}
case k == nodeLabelRole && v != "":
roles.Insert(v)
}
}
return roles.List()
}
func isNodeReady(node v1.Node) bool {
for _, condition := range node.Status.Conditions {
if condition.Type == v1.NodeReady && condition.Status == v1.ConditionTrue {
return true
}
}
return false
}
func getNodeInternalAddress(node v1.Node) string {
address := "unknown"
if len(node.Status.Addresses) > 0 {
//address = nodeInfo.node.Status.Addresses[0].Address
for _, addr := range node.Status.Addresses {
if addr.Type == v1.NodeInternalIP {
address = addr.Address
}
}
}
return address
}