From b52ea0297d6486cc02a9b8bf8d3a39319fe5e66a Mon Sep 17 00:00:00 2001 From: devthejo Date: Wed, 22 May 2024 10:22:59 +0200 Subject: [PATCH] chore: clean --- plugins/contrib/config.schema.json | 38 +- .../20-default-min-resources-requests.js | 155 -------- .../21-default-max-resources-limits.js | 69 ---- .../default-min-resources-requests.test.js | 330 ------------------ plugins/fabrique/kontinuous.yaml | 14 - 5 files changed, 1 insertion(+), 605 deletions(-) delete mode 100644 plugins/contrib/patches/20-default-min-resources-requests.js delete mode 100644 plugins/contrib/patches/21-default-max-resources-limits.js delete mode 100644 plugins/contrib/patches/tests/default-min-resources-requests.test.js diff --git a/plugins/contrib/config.schema.json b/plugins/contrib/config.schema.json index c02bfa4b15..0e967407e0 100644 --- a/plugins/contrib/config.schema.json +++ b/plugins/contrib/config.schema.json @@ -297,42 +297,6 @@ } } }, - "defaultMinResourcesRequests": { - "type": "object", - "title": "defaultMinResourcesRequests", - "markdownDescription": "Configuration of the defaultMinResourcesRequests plugin\n\nSee [plugin source](https://github.com/SocialGouv/kontinuous/blob/master/plugins/contrib/patches/20-default-min-resources-requests.js)", - "properties": { - "enabled": { - "title": "defaultMinResourcesRequests.enabled", - "description": "Enable or disable this plugin", - "type": "boolean" - }, - "options": { - "title": "defaultMinResourcesRequests.options", - "markdownDescription": "Options of the defaultMinResourcesRequests plugin\n\nSee [plugin source](https://github.com/SocialGouv/kontinuous/blob/master/plugins/contrib/patches/20-default-min-resources-requests.js)", - "type": "object", - "properties": {} - } - } - }, - "defaultMaxResourcesLimits": { - "type": "object", - "title": "defaultMaxResourcesLimits", - "markdownDescription": "Configuration of the defaultMaxResourcesLimits plugin\n\nSee [plugin source](https://github.com/SocialGouv/kontinuous/blob/master/plugins/contrib/patches/21-default-max-resources-limits.js)", - "properties": { - "enabled": { - "title": "defaultMaxResourcesLimits.enabled", - "description": "Enable or disable this plugin", - "type": "boolean" - }, - "options": { - "title": "defaultMaxResourcesLimits.options", - "markdownDescription": "Options of the defaultMaxResourcesLimits plugin\n\nSee [plugin source](https://github.com/SocialGouv/kontinuous/blob/master/plugins/contrib/patches/21-default-max-resources-limits.js)", - "type": "object", - "properties": {} - } - } - }, "janitor": { "type": "object", "title": "janitor", @@ -971,4 +935,4 @@ } } } -} +} \ No newline at end of file diff --git a/plugins/contrib/patches/20-default-min-resources-requests.js b/plugins/contrib/patches/20-default-min-resources-requests.js deleted file mode 100644 index 6bdf351309..0000000000 --- a/plugins/contrib/patches/20-default-min-resources-requests.js +++ /dev/null @@ -1,155 +0,0 @@ -const runnableKinds = ["Deployment", "Job", "StatefulSet", "DaemonSet"] - -const { - getCpuAsNum, - getMemoryAsNum, - getCpuAsString, - getMemoryAsString, -} = require("../lib/kubernetes-resource-helpers") - -module.exports = (manifests, options, { logger }) => { - const { - requests = {}, - avoidOutOfpods = false, - avoidOutOfpodsMargin = {}, - nodeConfig = {}, - } = options - - const { - maxPods = 110, - cpu: cpuNodeConfig, - memory: memoryNodeConfig, - } = nodeConfig - - const { - cpu: cpuAvoidOutOfpodsMargin = 0, - memory: memoryAvoidOutOfpodsMargin = 0, - } = avoidOutOfpodsMargin - - let { cpu, memory } = requests - - if (cpu === undefined || cpu === null) { - if (avoidOutOfpods) { - if (!cpuNodeConfig) { - throw new Error( - `you have enable "avoidOutOfpods" and doesn't specified default cpu request, so it requires missing nodeConfig.cpu to make the calculation` - ) - } - const cpuNodeAsNum = getCpuAsNum(cpuNodeConfig) - const cpuMarginAsNum = getCpuAsNum(cpuAvoidOutOfpodsMargin) - - const minimumCpuNumber = cpuNodeAsNum / maxPods + cpuMarginAsNum - cpu = getCpuAsString(minimumCpuNumber) - logger.trace( - `calculated min cpu: ${cpuNodeConfig}/${maxPods}${ - cpuMarginAsNum ? ` + ${cpuAvoidOutOfpodsMargin}` : "" - } = ${cpu}` - ) - } else { - cpu = "0" - } - } - if (memory === undefined || memory === null) { - if (avoidOutOfpods) { - if (!memoryNodeConfig) { - throw new Error( - `you have enable "avoidOutOfpods" and doesn't specified default memory request, so it requires missing nodeConfig.memory to make the calculation` - ) - } - const memoryNodeAsNum = getMemoryAsNum(memoryNodeConfig) - const memoryMarginAsNum = getMemoryAsNum(memoryAvoidOutOfpodsMargin) - const minimumMemoryNumber = memoryNodeAsNum / maxPods + memoryMarginAsNum - memory = getMemoryAsString(minimumMemoryNumber) - logger.trace( - `calculated min memory: ${memoryNodeConfig}/${maxPods}${ - memoryMarginAsNum ? ` + ${memoryAvoidOutOfpodsMargin}` : "" - } = ${memory}` - ) - } else { - memory = 0 - } - } - - for (const manifest of manifests) { - const { kind } = manifest - - if (!runnableKinds.includes(kind)) { - continue - } - - const containers = manifest.spec?.template?.spec?.containers - if (containers && containers.length > 0) { - let cpuByContainer = cpu - let memoryByContainer = memory - if (containers.length > 1) { - const cpuByContainerNumber = - Math.ceil((getCpuAsNum(cpu) / containers.length) * 1000) / 1000 - const memoryByContainerNumber = Math.ceil( - getMemoryAsNum(memory) / containers.length - ) - cpuByContainer = getCpuAsString(cpuByContainerNumber) - memoryByContainer = getMemoryAsString(memoryByContainerNumber) - } - for (const container of containers) { - if (!container.resources) { - container.resources = {} - } - - // if limits are specified and are lower than request we adjust request up - const cpuLimit = container.resources?.limits?.cpu - if (cpuLimit) { - const cpuLimitNumber = getCpuAsNum(cpuLimit) - let cpuByContainerNumber = getCpuAsNum(cpuByContainer) - if (cpuLimitNumber < cpuByContainerNumber) { - cpuByContainerNumber = cpuLimitNumber - cpuByContainer = getCpuAsString(cpuByContainerNumber) - } - } - const memoryLimit = container.resources?.limits?.memory - if (memoryLimit) { - const memoryLimitNumber = getMemoryAsNum(memoryLimit) - let memoryByContainerNumber = getCpuAsNum(cpuByContainer) - if (memoryLimitNumber < memoryByContainerNumber) { - memoryByContainerNumber = memoryLimitNumber - memoryByContainer = getMemoryAsString(memoryByContainerNumber) - } - } - - if (!container.resources.requests) { - container.resources.requests = {} - } - const definedCpu = container.resources.requests?.cpu - if (definedCpu === undefined || definedCpu === null) { - container.resources.requests.cpu = cpuByContainer.toString() - } - const definedMemory = container.resources.requests?.memory - if (definedMemory === undefined || definedMemory === null) { - container.resources.requests.memory = memoryByContainer.toString() - } - } - } - - const initContainers = manifest.spec?.template?.spec?.initContainers || [] - const { initContainersResourcesRequests = {} } = options - for (const container of initContainers) { - if (!container.resources) { - container.resources = {} - } - if (!container.resources.requests) { - container.resources.requests = {} - } - const definedCpu = container.resources.requests?.cpu - if (definedCpu === undefined || definedCpu === null) { - container.resources.requests.cpu = - initContainersResourcesRequests.cpu || "0" - } - const definedMemory = container.resources.requests?.memory - if (definedMemory === undefined || definedMemory === null) { - container.resources.requests.memory = - initContainersResourcesRequests.memory || "0" - } - } - } - - return manifests -} diff --git a/plugins/contrib/patches/21-default-max-resources-limits.js b/plugins/contrib/patches/21-default-max-resources-limits.js deleted file mode 100644 index 8ff3969697..0000000000 --- a/plugins/contrib/patches/21-default-max-resources-limits.js +++ /dev/null @@ -1,69 +0,0 @@ -/* -see https://kubernetes.io/docs/concepts/workloads/pods/init-containers/#resources -Given the ordering and execution for init containers, the following rules for resource usage apply: -- The highest of any particular resource request or limit defined on all init containers is the effective init request/limit. If any resource has no resource limit specified this is considered as the highest limit. -- The Pod's effective request/limit for a resource is the higher of: - - the sum of all app containers request/limit for a resource - - the effective init request/limit for a resource -*/ - -const { - getCpuAsNum, - getMemoryAsNum, - getCpuAsString, - getMemoryAsString, -} = require("../lib/kubernetes-resource-helpers") - -module.exports = (manifests, options, { logger }) => { - const { cpu = 3, memory = "8Gi" } = options - - for (const manifest of manifests) { - const spec = manifest.spec?.template?.spec - const { kind, metadata } = manifest - const { name, namespace } = metadata - - const containers = spec?.containers || [] - - let sumOfContainersCpuLimit = 0 - let sumOfContainersMemoryLimit = 0 - - for (const container of containers) { - if (!container.resources) { - container.resources = {} - } - const { resources: { limits = {} } = {} } = container - if (limits.cpu === undefined || limits.cpu === null) { - logger.warn( - `defaulting cpu limit: ${namespace}/${kind}/${name} container ${container.name} does not have cpu limits defaulting to ${cpu}` - ) - limits.cpu = cpu - } - if (limits.memory === undefined || limits.memory === null) { - logger.warn( - `defaulting memory limit: ${namespace}/${kind}/${name} container ${container.name} does not have memory limits defaulting to ${memory}` - ) - limits.memory = memory - } - sumOfContainersCpuLimit += getCpuAsNum(limits.cpu) - sumOfContainersMemoryLimit += getMemoryAsNum(limits.memory) - } - - const initContainers = spec?.initContainers || [] - for (const initContainer of initContainers) { - if (!initContainer.resources) { - initContainer.resources = {} - } - const { - resources: { limits = {} }, - } = initContainer - if (limits.cpu === undefined || limits.cpu === null) { - limits.cpu = getCpuAsString(sumOfContainersCpuLimit) - } - if (limits.memory === undefined || limits.memory === null) { - limits.memory = getMemoryAsString(sumOfContainersMemoryLimit) - } - } - } - - return manifests -} diff --git a/plugins/contrib/patches/tests/default-min-resources-requests.test.js b/plugins/contrib/patches/tests/default-min-resources-requests.test.js deleted file mode 100644 index 958419540a..0000000000 --- a/plugins/contrib/patches/tests/default-min-resources-requests.test.js +++ /dev/null @@ -1,330 +0,0 @@ -const defaultMinRequests = require("../20-default-min-resources-requests") - -const samples = [ - { - title: "without initial requests without avoidOutOfpods", - options: { - avoidOutOfpods: false, - }, - manifests: [ - { - kind: "Deployment", - spec: { template: { spec: { containers: [{ name: "container1" }] } } }, - }, - ], - expected: { - containers: [ - { - cpu: "0", - memory: "0", - }, - ], - }, - }, - { - title: "with avoidOutOfpods", - options: { - avoidOutOfpods: true, - nodeConfig: { - cpu: "7820m", - memory: "24505448Ki", - }, - }, - manifests: [ - { - kind: "Deployment", - metadata: { - annotations: {}, - }, - spec: { - template: { - spec: { - containers: [ - { - name: "container1", - resources: { requests: { cpu: 0.5, memory: "64Mi" } }, - }, - ], - }, - }, - }, - }, - ], - expected: { - containers: [ - { - cpu: 0.5, - memory: "64Mi", - }, - ], - }, - }, - { - title: "with avoidOutOfpods and no requests", - options: { - avoidOutOfpods: true, - nodeConfig: { - cpu: "7820m", - memory: "24505448Ki", - }, - }, - manifests: [ - { - kind: "Deployment", - metadata: { - annotations: {}, - }, - spec: { - template: { - spec: { - containers: [ - { - name: "container1", - resources: { requests: {} }, - }, - ], - }, - }, - }, - }, - ], - expected: { - containers: [ - { - cpu: "71m", - memory: "218Mi", - }, - ], - }, - }, - { - title: "with avoidOutOfpods, multiple containers and initcontainers", - options: { - avoidOutOfpods: true, - nodeConfig: { - cpu: "7820m", - memory: "24505448Ki", - }, - }, - manifests: [ - { - kind: "Deployment", - metadata: { - annotations: {}, - }, - spec: { - template: { - spec: { - containers: [ - { - name: "container1", - resources: { requests: { cpu: 0.5, memory: "64Mi" } }, - }, - { - name: "container2", - resources: { requests: { cpu: 1, memory: "128Mi" } }, - }, - ], - initContainers: [ - { - name: "initcontainer1", - resources: { requests: { cpu: 0.5, memory: "64Mi" } }, - }, - { - name: "initcontainer2", - resources: { requests: { cpu: 1, memory: "128Mi" } }, - }, - ], - }, - }, - }, - }, - ], - expected: { - containers: [ - { - cpu: 0.5, - memory: "64Mi", - }, - { - cpu: 1, - memory: "128Mi", - }, - ], - initContainers: [ - { - cpu: 0.5, - memory: "64Mi", - }, - { - cpu: 1, - memory: "128Mi", - }, - ], - }, - }, - { - title: - "with avoidOutOfpods, multiple containers and initcontainers, no requests", - options: { - avoidOutOfpods: true, - nodeConfig: { - cpu: "7820m", - memory: "24505448Ki", - }, - }, - manifests: [ - { - kind: "Deployment", - metadata: { - annotations: {}, - }, - spec: { - template: { - spec: { - containers: [ - { - name: "container1", - resources: { requests: {} }, - }, - { - name: "container2", - resources: { requests: {} }, - }, - ], - initContainers: [ - { - name: "initcontainer1", - resources: { requests: {} }, - }, - { - name: "initcontainer2", - resources: { requests: {} }, - }, - ], - }, - }, - }, - }, - ], - expected: { - containers: [ - { - cpu: "36m", - memory: "109Mi", - }, - { - cpu: "36m", - memory: "109Mi", - }, - ], - initContainers: [ - { - cpu: "0", - memory: "0", - }, - { - cpu: "0", - memory: "0", - }, - ], - }, - }, - { - title: - "avoidOutOfpods: with minimize-dev-resources-requests-disable annotation", - options: { - avoidOutOfpods: true, - nodeConfig: { - cpu: "7820m", - memory: "24505448Ki", - }, - }, - manifests: [ - { - kind: "Deployment", - metadata: { - annotations: { - "patches.kontinuous/minimize-dev-resources-requests-disable": true, - }, - }, - spec: { - template: { - spec: { - containers: [ - { - name: "container1", - resources: { requests: { cpu: 0.5, memory: "64Mi" } }, - }, - { - name: "container2", - resources: { requests: { cpu: 1, memory: "128Mi" } }, - }, - ], - initContainers: [ - { - name: "initcontainer1", - resources: { requests: { cpu: 0.5, memory: "64Mi" } }, - }, - { - name: "initcontainer2", - resources: { requests: { cpu: 1, memory: "128Mi" } }, - }, - ], - }, - }, - }, - }, - ], - expected: { - containers: [ - { - cpu: 0.5, - memory: "64Mi", - }, - { - cpu: 1, - memory: "128Mi", - }, - ], - initContainers: [ - { - cpu: 0.5, - memory: "64Mi", - }, - { - cpu: 1, - memory: "128Mi", - }, - ], - }, - }, -] - -samples.forEach((sample) => { - test(`${sample.title}`, async () => { - const ctx = require("~common/ctx") - - const res = defaultMinRequests(sample.manifests, sample.options, { - config: { environment: "dev" }, - ctx, - logger: { - trace: () => {}, - }, - }) - expect( - res[0].spec.template.spec.containers.map( - (container) => container.resources.requests - ) - ).toEqual(sample.expected.containers) - - if (sample.expected.initContainers) { - // eslint-disable-next-line jest/no-conditional-expect - expect( - res[0].spec.template.spec.initContainers.map( - (container) => container.resources.requests - ) - ).toEqual(sample.expected.initContainers) - } - }) -}) diff --git a/plugins/fabrique/kontinuous.yaml b/plugins/fabrique/kontinuous.yaml index c1e94546b1..b383c9cddf 100644 --- a/plugins/fabrique/kontinuous.yaml +++ b/plugins/fabrique/kontinuous.yaml @@ -43,20 +43,6 @@ dependencies: enabled: false filterDisableKapp: enabled: true - defaultMinResourcesRequests: - options: - avoidOutOfpods: true - avoidOutOfpodsMargin: - cpu: 10m - memory: 10Mi - nodeConfig: - maxPods: 110 - cpu: 7820m - memory: 27717Mi - defaultMaxResourcesLimits: - options: - cpu: 1 - memory: 1Gi addJobsAffinityAndTolerations: enabled: true options: