diff --git a/.github/workflows/tests-deploy.yaml b/.github/workflows/tests-deploy.yaml index e78725e373..1b5fd43bf9 100644 --- a/.github/workflows/tests-deploy.yaml +++ b/.github/workflows/tests-deploy.yaml @@ -57,12 +57,12 @@ jobs: steps: - uses: actions/checkout@v3 - + - uses: actions/setup-node@v3 with: node-version: ${{ matrix.nodeVersion }} - cache: 'yarn' - + cache: "yarn" + - name: Yarn install run: | yarn workspaces focus kontinuous --production @@ -73,16 +73,24 @@ jobs: shell: bash run: | mkdir -p $HOME/.kontinuous - + echo " links: socialgouv/kontinuous: ${GITHUB_WORKSPACE} + dependencies: + fabrique: + dependencies: + contrib: + preDeploy: + rancherNamespaces: + enabled: false " > $HOME/.kontinuous/config.yaml kubectl cluster-info kubectl version kubectl get pods -n kube-system kubectl create ns test-project-ci + kubectl create ns kontinuous - name: test deploy-via-github shell: bash @@ -100,7 +108,7 @@ jobs: KS_INLINE_CONFIG_SET: | dependencies.fabrique.dependencies.contrib.preDeploy.cleanOrphan.options.crdApiResources: [] run: ./kontinuous deploy - + - name: run test script shell: bash run: | diff --git a/packages/kontinuous/tests/index.test.js b/packages/kontinuous/tests/index.test.js index 0d371729f6..b0bae54f56 100644 --- a/packages/kontinuous/tests/index.test.js +++ b/packages/kontinuous/tests/index.test.js @@ -95,6 +95,7 @@ describe("test build manifests with snapshots", () => { "fabrique/contrib/validators/kubeconform", "fabrique/contrib/validators/sealedSecrets", "fabrique/contrib/patches/janitor", + "fabrique/contrib/patches/updateManifestsWithPreprodResources", "fabrique/contrib/valuesCompilers/getGitDefaultBranch", ]), } diff --git a/plugins/contrib/kontinuous.yaml b/plugins/contrib/kontinuous.yaml index 6b880ceaf8..3c2c1ce673 100644 --- a/plugins/contrib/kontinuous.yaml +++ b/plugins/contrib/kontinuous.yaml @@ -35,6 +35,8 @@ patches: enabled: false zeroDowntimeReadiness: enabled: false + updateManifestsWithPreprodResources: + enabled: false validators: rancherProjectId: diff --git a/plugins/contrib/patches/80-update-manifests-with-preprod-resources.js b/plugins/contrib/patches/80-update-manifests-with-preprod-resources.js new file mode 100644 index 0000000000..0ccee34a7b --- /dev/null +++ b/plugins/contrib/patches/80-update-manifests-with-preprod-resources.js @@ -0,0 +1,69 @@ +module.exports = async function updateManifestsWithPreprodResources( + manifests, + _options, + context +) { + const { config, logger, kubectl } = context + const { repositoryName, environment } = config + + if (environment !== "dev") { + return + } + + const preprodNamespace = `${repositoryName}-preprod` + + const { surviveOnBrokenCluster, kubeconfig, kubeconfigContext } = config + + async function getPreprodResources(name, kind) { + try { + const result = await kubectl( + `get -n ${preprodNamespace} ${kind} ${name} -o json`, + { + kubeconfig, + kubeconfigContext, + logInfo: false, + logError: false, + surviveOnBrokenCluster, + } + ) + const resourceData = JSON.parse(result) + return resourceData.spec.template.spec.containers.reduce( + (acc, container) => { + acc[container.name] = container.resources + return acc + }, + {} + ) + } catch (err) { + logger.warn({ err }, `Failed to get resources for ${kind} ${name}`) + return {} + } + } + + function updateResources(container, preprodResources) { + if (preprodResources[container.name]) { + container.resources = preprodResources[container.name] + } + } + + for (const manifest of manifests) { + if (["Deployment", "StatefulSet"].includes(manifest.kind)) { + const preprodResources = await getPreprodResources( + manifest.metadata.name, + manifest.kind.toLowerCase() + ) + for (const container of manifest.spec.template.spec.containers) { + updateResources(container, preprodResources) + } + } else if (manifest.kind === "CronJob") { + const preprodResources = await getPreprodResources( + manifest.metadata.name, + "cronjob" + ) + for (const container of manifest.spec.jobTemplate.spec.template.spec + .containers) { + updateResources(container, preprodResources) + } + } + } +} diff --git a/plugins/fabrique/kontinuous.yaml b/plugins/fabrique/kontinuous.yaml index c6f4f72165..93d0a48e89 100644 --- a/plugins/fabrique/kontinuous.yaml +++ b/plugins/fabrique/kontinuous.yaml @@ -21,6 +21,8 @@ dependencies: buildkitSvcCount: 6 patches: + updateManifestsWithPreprodResources: + enabled: true zeroDowntimeReadiness: enabled: true certs: