From 3f629a5cc2204404bfcf147bfe314053ad3cec36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=A9o=20M=C3=A9vollon?= <38255502+matmut7@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:30:11 +0200 Subject: [PATCH] fix: oblik patch requests (#495) --- .../oblik-cap-resources.dev.yaml | 8 ++++++- .../samples/oblik-cap-resources/values.yaml | 5 +++++ .../contrib/patches/81-oblik-cap-limits.js | 22 ++++++++++++++----- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/packages/kontinuous/tests/__snapshots__/oblik-cap-resources.dev.yaml b/packages/kontinuous/tests/__snapshots__/oblik-cap-resources.dev.yaml index 3d0008f58..c208d460c 100644 --- a/packages/kontinuous/tests/__snapshots__/oblik-cap-resources.dev.yaml +++ b/packages/kontinuous/tests/__snapshots__/oblik-cap-resources.dev.yaml @@ -97,7 +97,9 @@ metadata: namespace: test-oblik-cap-resources-feature-branch-1 annotations: oblik.socialgouv.io/max-limit-cpu: 200m + oblik.socialgouv.io/max-request-memory: 100Mi oblik.socialgouv.io/min-limit-memory: 100Mi + oblik.socialgouv.io/min-request-cpu: 50m kontinuous/chartPath: project.fabrique.contrib.app kontinuous/source: project/charts/fabrique/charts/contrib/charts/app/templates/deployment.yaml kontinuous/deployment: test-oblik-cap-resources-feature-branch-1-ffac537e6cb-5kms108e @@ -184,7 +186,9 @@ spec: limits: cpu: 0.2 memory: 104857600 - requests: {} + requests: + cpu: 0.05 + memory: 104857600 lifecycle: postStart: exec: @@ -223,7 +227,9 @@ metadata: namespace: test-oblik-cap-resources-feature-branch-1 annotations: oblik.socialgouv.io/max-limit-cpu: 200m + oblik.socialgouv.io/max-request-memory: 100Mi oblik.socialgouv.io/min-limit-memory: 100Mi + oblik.socialgouv.io/min-request-cpu: 50m kontinuous/chartPath: project.fabrique.contrib.app kontinuous/source: project/charts/fabrique/charts/contrib/charts/app/templates/service.yaml kontinuous/deployment: test-oblik-cap-resources-feature-branch-1-ffac537e6cb-5kms108e diff --git a/packages/kontinuous/tests/samples/oblik-cap-resources/values.yaml b/packages/kontinuous/tests/samples/oblik-cap-resources/values.yaml index d6c2291a1..8a1efab29 100644 --- a/packages/kontinuous/tests/samples/oblik-cap-resources/values.yaml +++ b/packages/kontinuous/tests/samples/oblik-cap-resources/values.yaml @@ -1,8 +1,13 @@ app: annotations: oblik.socialgouv.io/max-limit-cpu: 200m + oblik.socialgouv.io/min-request-cpu: 50m oblik.socialgouv.io/min-limit-memory: 100Mi + oblik.socialgouv.io/max-request-memory: 100Mi resources: limits: cpu: "10" memory: 10Mi + requests: + cpu: "10m" + memory: 150Mi diff --git a/plugins/contrib/patches/81-oblik-cap-limits.js b/plugins/contrib/patches/81-oblik-cap-limits.js index e3c66dd73..5899003e7 100644 --- a/plugins/contrib/patches/81-oblik-cap-limits.js +++ b/plugins/contrib/patches/81-oblik-cap-limits.js @@ -66,6 +66,10 @@ function parseQuantity(quantity) { module.exports = async function oblikCapLimits(manifests, _options, _context) { const resourcesToCheck = ["Deployment", "StatefulSet", "CronJob"] const annotationsToCheck = [ + "oblik.socialgouv.io/min-request-cpu", + "oblik.socialgouv.io/min-request-memory", + "oblik.socialgouv.io/max-request-cpu", + "oblik.socialgouv.io/max-request-memory", "oblik.socialgouv.io/min-limit-cpu", "oblik.socialgouv.io/min-limit-memory", "oblik.socialgouv.io/max-limit-cpu", @@ -84,7 +88,8 @@ module.exports = async function oblikCapLimits(manifests, _options, _context) { containers.forEach((container) => { const containerName = container.name - const { limits = {} } = container.resources || {} + const { requests = {}, limits = {} } = container.resources || {} + const containerResources = { requests, limits } annotationsToCheck.forEach((annotation) => { const defaultAnnotationValue = annotations[annotation] @@ -105,18 +110,25 @@ module.exports = async function oblikCapLimits(manifests, _options, _context) { if (annotationValue === null) { return } - const key = annotation.includes("cpu") ? "cpu" : "memory" + const resourceTarget = annotation.includes("cpu") ? "cpu" : "memory" + const resourceType = annotation.includes("request") + ? "requests" + : "limits" - const resourceValue = limits[key] ? parseQuantity(limits[key]) : null + const resourceValue = containerResources[resourceType][resourceTarget] + ? parseQuantity(containerResources[resourceType][resourceTarget]) + : null if (resourceValue !== null) { if ( (annotation.includes("min") && resourceValue < annotationValue) || (annotation.includes("max") && resourceValue > annotationValue) ) { - limits[key] = annotationValue + containerResources[resourceType][resourceTarget] = annotationValue container.resources ||= {} - container.resources.limits ||= limits + container.resources[resourceType] ||= {} + container.resources[resourceType][resourceTarget] ||= + containerResources[resourceType][resourceTarget] } } })