Last active
August 3, 2018 22:02
-
-
Save jasonk000/416f757ebbc62fa6ff503662eb3e9941 to your computer and use it in GitHub Desktop.
top-for-hpa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const fs = require('fs') | |
const columnify = require('columnify') | |
const exec = require('sync-exec') | |
let stdinBuffer = exec('kubectl get hpa --all-namespaces --output json').stdout | |
let results = JSON.parse(stdinBuffer.toString()) | |
// stdinBuffer = exec('kubectl get pods --all-namespaces | grep -v Running').stdout | |
let items = results.items.map(i => { | |
return { | |
namespace: i.metadata.namespace, | |
name: i.metadata.name, | |
minReplicas: i.spec.minReplicas, | |
maxReplicas: i.spec.maxReplicas, | |
targetCpu: i.spec.targetCPUUtilizationPercentage, | |
currentCpu: i.status.currentCPUUtilizationPercentage, | |
currentReplicas: i.status.currentReplicas, | |
desiredReplicas: (i.status.desiredReplicas === i.status.currentReplicas) ? '' : | |
(i.status.desiredReplicas > i.status.currentReplicas ? `+${i.status.desiredReplicas - i.status.currentReplicas}` : `-${i.status.currentReplicas - i.status.desiredReplicas}`), | |
lastScaleTime: i.status.lastScaleTime, | |
lastScaleTimeDelta: Math.round((Date.now() - Date.parse(i.status.lastScaleTime))/(60*1000)) + "m", | |
closeness: Math.round((i.status.currentReplicas / i.spec.maxReplicas) * i.status.currentCPUUtilizationPercentage) | |
} | |
}) | |
let config = { | |
columns: [ 'namespace', 'name', 'closeness', 'minReplicas', 'currentReplicas', 'desiredReplicas', 'maxReplicas', 'currentCpu', 'targetCpu', 'lastScaleTimeDelta' ], | |
config: { | |
// TODO why is this ignored | |
namespace: { align: 'left', width: 20 }, | |
name: { align: 'left' }, | |
closeness: { align: 'right' }, | |
minReplicas: { align: 'right' }, | |
currentReplicas: { align: 'right' }, | |
desiredReplicas: { align: 'right' }, | |
maxReplicas: { align: 'right' }, | |
currentCpu: { align: 'right' }, | |
targetCpu: { align: 'right' }, | |
lastScaleTimeDelta: { align: 'right' } | |
} | |
} | |
let sorted = items.sort((a, b) => a.closeness - b.closeness).reverse() | |
console.log('---- DEPLOYMENTS NEAR MAX HPA ----') | |
console.log(columnify(items.filter(i => i.currentReplicas >= i.maxReplicas * 0.95), config)) | |
console.log('') | |
console.log('---- DEPLOYMENTS NEAR MAX CPU ----') | |
console.log(columnify(items.filter(i => i.currentCpu >= 80), config)) | |
console.log('') | |
console.log('---- OVERALL CAPACITY ----') | |
console.log(columnify(sorted, config)) | |
console.log('') | |
// ------------------------------------ | |
// console.log('---- PODS WITH STATE CHANGE ----') | |
// console.log(stdinBuffer) | |
// console.log('') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment