Skip to content

Commit

Permalink
fix: oblik patch requests
Browse files Browse the repository at this point in the history
  • Loading branch information
matmut7 committed Jun 11, 2024
1 parent 5066e4e commit fb73ce0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -184,7 +186,9 @@ spec:
limits:
cpu: 0.2
memory: 104857600
requests: {}
requests:
cpu: 0.05
memory: 104857600
lifecycle:
postStart:
exec:
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
22 changes: 17 additions & 5 deletions plugins/contrib/patches/81-oblik-cap-limits.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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]
Expand All @@ -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]
}
}
})
Expand Down

0 comments on commit fb73ce0

Please sign in to comment.