Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(4544): change condition for auto-approva #4585

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export default privateCloudProductUsageMetrics(({ getPathParams }) => {
containerName: 'Container name',
usage: { cpu: 'CPU usage', memory: 'Memory usage' },
requests: { cpu: 'CPU request', memory: 'Memory request' },
limits: { cpu: 'CPU limits', memory: 'Memory limits' },
},
...transformPodData(data.podMetrics),
];
Expand Down
8 changes: 4 additions & 4 deletions app/helpers/auto-approval-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ function checkAutoApprovalEligibility({ allocation, deployment, resourceType }:
if (deployment.usage === -1) return false;

// Calculate usage-to-limit and utilization ratios
const usageRatio = deployment.usage / allocation.limit;
const usageRatio = deployment.usage / allocation.request;
if (resourceType === ResourceType.storage) {
// Approve if storage usage exceeds 80% of limit
return usageRatio > 0.8;
}
const utilizationRate = deployment.usage / deployment.request;
// Approve CPU/Memory if usage exceeds 85% of limit and utilization is at least 35%
return usageRatio > 0.85 && utilizationRate >= 0.35;
const utilizationRate = deployment.usage / allocation.request;
// Approve CPU/Memory if utilization is at least 35%
return utilizationRate >= 0.35;
}

function extractResourceRequests(resourceRequests: ResourceRequestsEnv) {
Expand Down
16 changes: 1 addition & 15 deletions app/helpers/resource-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ export type Container = {
cpu: number;
memory: number;
};
limits: {
cpu: number;
memory: number;
};
};

// Conversion factors for resource units
Expand Down Expand Up @@ -148,14 +144,12 @@ export function formatCpu(millicores: number) {
export const getTotalMetrics = (data: Pod[] | PVC[], resource: ResourceType) => {
let totalUsage = 0;
let totalRequest = 0;
let totalLimit = 0;
// Iterate through each pod and each container
if (data.every((item) => 'containers' in item) && (resource === 'cpu' || resource === 'memory')) {
(data as Pod[]).forEach((pod) => {
pod.containers.forEach((container) => {
totalUsage += container.usage[resource] || 0;
totalRequest += container.requests[resource] || 0;
totalLimit += container.limits[resource] || 0;
});
});
} else {
Expand All @@ -165,7 +159,7 @@ export const getTotalMetrics = (data: Pod[] | PVC[], resource: ResourceType) =>
if (pvc.requests) totalRequest += pvc.requests;
});
}
return { totalUsage, totalRequest, totalLimit };
return { totalUsage, totalRequest };
};

export type TransformedPodData = {
Expand All @@ -179,10 +173,6 @@ export type TransformedPodData = {
cpu: number | string;
memory: number | string;
};
limits: {
cpu: number | string;
memory: number | string;
};
};

export type TransformedPVCData = {
Expand Down Expand Up @@ -224,10 +214,6 @@ export function transformPodData(data: Pod[]) {
cpu: container.requests.cpu,
memory: container.requests.memory,
},
limits: {
cpu: container.limits.cpu,
memory: container.limits.memory,
},
});
});
});
Expand Down
6 changes: 2 additions & 4 deletions app/services/k8s/resource-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,15 @@ export async function getResourceDetails({
const isStorage = resourceName === ResourceType.storage;
const namespaceData = await getPodMetrics(licencePlate, env, cluster);
const metricsData = isStorage ? namespaceData.pvcMetrics : namespaceData.podMetrics;

if (metricsData.length === 0) return result;

const { totalRequest, totalLimit, totalUsage } = getTotalMetrics(metricsData, resourceName);
const { totalRequest, totalUsage } = getTotalMetrics(metricsData, resourceName);
result.deployment.request = totalRequest;
result.deployment.limit = totalLimit;
result.deployment.usage = totalUsage;

const unitMultiplier = resourceName === 'cpu' ? cpuCoreToMillicoreMultiplier : memoryUnitMultipliers.Gi;
const resourceValue = currentResourceRequests[env][resourceName];
const deploymentRequest = isStorage ? 0 : resourceValue * unitMultiplier;
const deploymentRequest = resourceValue * unitMultiplier;

result.allocation.request = deploymentRequest;
return result;
Expand Down
5 changes: 1 addition & 4 deletions app/services/k8s/usage-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ async function collectPVCMetrics(namespace: string, cluster: Cluster) {
const { apiClient } = getK8sClients(cluster);
const res = await apiClient.listNamespacedPersistentVolumeClaim(namespace);
const pvcs = res.body.items;

const pvcPromises = pvcs.map(async (pvc) => {
const pvcName = pvc.metadata?.name;
if (!pvcName) return null;
Expand Down Expand Up @@ -155,10 +156,6 @@ export async function getPodMetrics(
cpu: normalizeCpu(lastTwoWeeksUsage.cpu),
memory: normalizeMemory(lastTwoWeeksUsage.memory),
},
limits: {
cpu: normalizeCpu(resourceDef.resources?.limits?.cpu || 0),
memory: normalizeMemory(resourceDef.resources?.limits?.memory || 0),
},
requests: {
cpu: normalizeCpu(resourceDef.resources?.requests?.cpu || 0),
memory: normalizeMemory(resourceDef.resources?.requests?.memory || 0),
Expand Down
Loading