new status format, new podSelector field, more accurate pod stats

* renamed pod label prefix from "homepage" to "gethomepage.dev"
  which is more inline with typical kubernetes practices
This commit is contained in:
James Wynn 2022-12-08 16:03:29 -06:00
parent 174cb651b4
commit 09eb172079
8 changed files with 65 additions and 40 deletions

View file

@ -8,7 +8,7 @@ const logger = createLogger("kubernetesStatsService");
export default async function handler(req, res) {
const APP_LABEL = "app.kubernetes.io/name";
const { service } = req.query;
const { service, podSelector } = req.query;
const [namespace, appName] = service;
if (!namespace && !appName) {
@ -17,7 +17,7 @@ export default async function handler(req, res) {
});
return;
}
const labelSelector = `${APP_LABEL}=${appName}`;
const labelSelector = podSelector !== undefined ? podSelector : `${APP_LABEL}=${appName}`;
try {
const kc = getKubeConfig();
@ -63,7 +63,7 @@ export default async function handler(req, res) {
});
});
const stats = await pods.map(async (pod) => {
const podStatsList = await Promise.all(pods.map(async (pod) => {
let depMem = 0;
let depCpu = 0;
const podMetrics = await metricsApi.getPodMetrics(namespace, pod.metadata.name)
@ -85,13 +85,15 @@ export default async function handler(req, res) {
mem: depMem,
cpu: depCpu
};
}).reduce(async (finalStats, podStatPromise) => {
const podStats = await podStatPromise;
return {
mem: finalStats.mem + podStats.mem,
cpu: finalStats.cpu + podStats.cpu
};
});
}));
const stats = {
mem: 0,
cpu: 0
}
podStatsList.forEach((podStat) => {
stats.mem += podStat.mem;
stats.cpu += podStat.cpu;
});
stats.cpuLimit = cpuLimit;
stats.memLimit = memLimit;
stats.cpuUsage = cpuLimit ? stats.cpu / cpuLimit : 0;

View file

@ -7,7 +7,7 @@ const logger = createLogger("kubernetesStatusService");
export default async function handler(req, res) {
const APP_LABEL = "app.kubernetes.io/name";
const { service } = req.query;
const { service, podSelector } = req.query;
const [namespace, appName] = service;
if (!namespace && !appName) {
@ -16,8 +16,8 @@ export default async function handler(req, res) {
});
return;
}
const labelSelector = `${APP_LABEL}=${appName}`;
const labelSelector = podSelector !== undefined ? podSelector : `${APP_LABEL}=${appName}`;
logger.info("labelSelector %s/%s = %s", namespace, appName, labelSelector);
try {
const kc = getKubeConfig();
if (!kc) {
@ -47,10 +47,14 @@ export default async function handler(req, res) {
});
return;
}
// at least one pod must be in the "Running" phase, otherwise its "down"
const runningPod = pods.find(pod => pod.status.phase === "Running");
const status = runningPod ? "running" : "down";
const someReady = pods.find(pod => pod.status.phase === "Running");
const allReady = pods.every((pod) => pod.status.phase === "Running");
let status = "down";
if (allReady) {
status = "running";
} else if (someReady) {
status = "partial";
}
res.status(200).json({
status
});