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

fix: oblik patch requests #495

Merged
merged 1 commit into from
Jun 11, 2024
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 @@ -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
Loading