|
| 1 | +import { z } from 'zod'; |
| 2 | +import { GlobalRole } from '@/constants'; |
| 3 | +import createApiHandler from '@/core/api-handler'; |
| 4 | +import { OkResponse } from '@/core/responses'; |
| 5 | +import { Cluster } from '@/prisma/client'; |
| 6 | +import { queryCapacity, queryAllocatable, queryCpuRequests, queryCpuUsage } from '@/services/k8s/metrics/core'; |
| 7 | + |
| 8 | +const queryParamSchema = z.object({ |
| 9 | + cluster: z.nativeEnum(Cluster), |
| 10 | +}); |
| 11 | + |
| 12 | +export const GET = createApiHandler({ |
| 13 | + roles: [GlobalRole.Admin], |
| 14 | + useServiceAccount: true, |
| 15 | + validations: { queryParams: queryParamSchema }, |
| 16 | +})(async ({ queryParams }) => { |
| 17 | + const { cluster } = queryParams; |
| 18 | + |
| 19 | + // See https://docs.redhat.com/en/documentation/openshift_container_platform/4.19/html/nodes/nodes-dashboard-using |
| 20 | + const [capacityRes, allocatableRes, requestsRes, usageRes] = await Promise.all([ |
| 21 | + queryCapacity(cluster), |
| 22 | + queryAllocatable(cluster), |
| 23 | + queryCpuRequests(cluster), |
| 24 | + queryCpuUsage(cluster), |
| 25 | + ]); |
| 26 | + |
| 27 | + const capacity = Number(capacityRes[0]?.value[1] || 0); |
| 28 | + const allocatable = Number(allocatableRes[0]?.value[1] || 0); |
| 29 | + const requests = Number(requestsRes[0]?.value[1] || 0); |
| 30 | + const usage = Number(usageRes[0]?.value[1] || 0); |
| 31 | + |
| 32 | + // | Term | Meaning | |
| 33 | + // | --------------- | ------------------------------------------------------------------ | |
| 34 | + // | **Capacity** | The full amount of a resource on the node (e.g., total CPU/memory) | |
| 35 | + // | **Allocatable** | The portion of that resource Kubernetes allows for pod scheduling | |
| 36 | + // | **Requests** | The amount of resource that pods ask for | |
| 37 | + // | **Usage** | The actual usage by running containers | |
| 38 | + return OkResponse({ |
| 39 | + capacity, |
| 40 | + allocatable, |
| 41 | + requests, |
| 42 | + usage, |
| 43 | + requestUtilization: (requests / allocatable) * 100, |
| 44 | + usageEfficiency: (usage / requests) * 100, |
| 45 | + }); |
| 46 | +}); |
0 commit comments