-
Notifications
You must be signed in to change notification settings - Fork 1
/
kube-get-gpu-nodes
executable file
·44 lines (35 loc) · 1.54 KB
/
kube-get-gpu-nodes
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
#!/bin/bash
# Copyright 2019 Dmitri Rubinstein
#
# 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.
# Based on https://github.com/kubernetes/kubernetes/issues/76995#issuecomment-501239997
set -eo pipefail
export LC_ALL=C
unset CDPATH
THIS_DIR=$( cd "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P )
# shellcheck source=kube-shlib.sh
source "$THIS_DIR/kube-shlib.sh"
define-kubectl-funcs
kubectl describe nodes --all-namespaces | \
tr -d '\000' | \
sed -n -e '/^Name/,/Roles/p' -e '/^Capacity/,/Allocatable/p' -e '/^Allocatable/,/System Info/p' -e '/^Allocated resources/,/Events/p' | \
grep -e Name -e nvidia.com | \
perl -pe 's/\n//' | \
perl -pe 's/Name:/\n/g' | \
sed 's/nvidia.com\/gpu:\?//g' | \
sed '1s/^/Node Capacity(GPUs) Allocatable(GPUs) Allocated(GPUs)/' | \
sed 's/$/ 0 0 0 0/' | \
awk '{print $1, $2, $3, $4}' | \
column -t
# kubectl get node -o go-template --template
# '{{range .items}} {{.metadata.name}} {{$agpus:=(index .status.allocatable "nvidia.com/gpu")}} {{$cgpus:=(index .status.capacity "nvidia.com/gpu")}} {{$agpus}} {{$cgpus}}{{"\n"}}{{end}}'