Skip to content

Commit 9124197

Browse files
committed
support resource usage custom labels
1 parent 8217d7b commit 9124197

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ AWS_PROFILE=myprofile AWS_REGION=us-west-2
6363
`eks-node-viewer` supports some custom label names that can be passed to the `--extra-labels` to display additional node information.
6464
6565
- `eks-node-viewer/node-age` - Age of the node
66+
- `eks-node-viewer/node-cpu-usage` - CPU usage (requests)
67+
- `eks-node-viewer/node-memory-usage` - Memory usage (requests)
68+
- `eks-node-viewer/node-pods-usage` - Pod usage (requests)
69+
- `eks-node-viewer/node-ephemeral-storage-usage` - Ephemeral Storage usage (requests)
6670
6771
### Default Options
6872
You can supply default options to `eks-node-viewer` by creating a file named `.eks-node-viewer` in your home directory and specifying

pkg/model/node.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package model
1717
import (
1818
"fmt"
1919
"math"
20+
"regexp"
2021
"sync"
2122
"time"
2223

@@ -233,11 +234,33 @@ func (n *Node) UpdatePrice(pricing *pricing.Provider) {
233234
}
234235
}
235236

237+
var resourceLabelRe = regexp.MustCompile("eks-node-viewer/node-(.*?)-usage")
238+
236239
// ComputeLabel computes dynamic labels
237240
func (n *Node) ComputeLabel(labelName string) string {
238241
switch labelName {
239242
case "eks-node-viewer/node-age":
240243
return duration.HumanDuration(time.Since(n.Created()))
241244
}
245+
// resource based custom labels
246+
if match := resourceLabelRe.FindStringSubmatch(labelName); len(match) > 0 {
247+
return pctUsage(n.Allocatable(), n.Used(), match[1])
248+
}
242249
return labelName
243250
}
251+
252+
func pctUsage(allocatable v1.ResourceList, used v1.ResourceList, resource string) string {
253+
allocRes, hasAlloc := allocatable[v1.ResourceName(resource)]
254+
if !hasAlloc {
255+
return "N/A"
256+
}
257+
usedRes, hasUsed := used[v1.ResourceName(resource)]
258+
if !hasUsed || usedRes.AsApproximateFloat64() == 0 {
259+
return "0%"
260+
}
261+
pctUsed := 0.0
262+
if allocRes.AsApproximateFloat64() != 0 {
263+
pctUsed = 100 * (usedRes.AsApproximateFloat64() / allocRes.AsApproximateFloat64())
264+
}
265+
return fmt.Sprintf("%.0f%%", pctUsed)
266+
}

0 commit comments

Comments
 (0)