diff --git a/packages/common/utils/get-repository-file.js b/packages/common/utils/get-repository-file.js index 689aec3d9e..7bda199465 100644 --- a/packages/common/utils/get-repository-file.js +++ b/packages/common/utils/get-repository-file.js @@ -40,10 +40,17 @@ module.exports = async ({ { cwd: tmpdir, env } ) - await asyncShell(`git checkout ${ref} -- ${file}`, { - cwd: tmpdir, - env, - }) + try { + await asyncShell(`git checkout ${ref} -- ${file}`, { + cwd: tmpdir, + env, + }) + } catch (err) { + if (err.message.includes("error: pathspec")) { + return false + } + throw err + } const content = await fs.readFile(`${tmpdir}/${file}`, { encoding: "utf-8", diff --git a/packages/kontinuous/tests/index.test.js b/packages/kontinuous/tests/index.test.js index b0bae54f56..532a66088e 100644 --- a/packages/kontinuous/tests/index.test.js +++ b/packages/kontinuous/tests/index.test.js @@ -97,6 +97,7 @@ describe("test build manifests with snapshots", () => { "fabrique/contrib/patches/janitor", "fabrique/contrib/patches/updateManifestsWithPreprodResources", "fabrique/contrib/valuesCompilers/getGitDefaultBranch", + "fabrique/patches/infraResources", ]), } const envFile = `${testdirPath}/.env` diff --git a/plugins/fabrique/kontinuous.yaml b/plugins/fabrique/kontinuous.yaml index 94b446afc3..619abbff62 100644 --- a/plugins/fabrique/kontinuous.yaml +++ b/plugins/fabrique/kontinuous.yaml @@ -5,6 +5,9 @@ patches: setUndefinedStorageClassnameCnpg: options: defaultStorageClassName: csi-cinder-high-speed + infraResources: + options: + repositoryUrl: https://github.com/SocialGouv/infra-resources.git dependencies: contrib: diff --git a/plugins/fabrique/patches/03-infra-resources.js b/plugins/fabrique/patches/03-infra-resources.js new file mode 100644 index 0000000000..9e48b918be --- /dev/null +++ b/plugins/fabrique/patches/03-infra-resources.js @@ -0,0 +1,63 @@ +module.exports = async (manifests, options, context) => { + const { utils, config } = context + const { getRepositoryFile, deepmerge, yaml } = utils + + const remoteConfigPath = `projects/${config.projectName}/${config.environment}/${config.repositoryName}.yaml` + + const fileContent = await getRepositoryFile({ + repositoryUrl: options.repositoryUrl, + ref: options.repositoryRef || "main", + file: remoteConfigPath, + }) + + if (fileContent === false) { + return + } + + const resourcesConfig = yaml.load(fileContent) + + for (const manifest of manifests) { + const kind = manifest.kind.toLowerCase() + const apiGroup = manifest.apiVersion.split("/")[0].toLowerCase() + const key = `${kind}.${apiGroup}` + + const { name } = manifest.metadata + + switch (key) { + case "cluster.postgresql.cnpg.io": { + const mergeResources = + resourcesConfig[key]?.[name]?.resources || + resourcesConfig[kind]?.[name]?.resources || + {} + manifest.spec.resources = deepmerge( + manifest.spec.resources || {}, + mergeResources + ) + break + } + case "deployment.apps": + case "statefulset.apps": { + const mergeContainersResources = + resourcesConfig[key]?.[name]?.containers || + resourcesConfig[kind]?.[name]?.containers + + if (mergeContainersResources) { + for (const container of manifest.spec.template.spec.containers) { + const containerResources = mergeContainersResources[container.name] + if (containerResources) { + container.resources = deepmerge( + container.resources || {}, + containerResources + ) + } + } + } + break + } + default: { + // Handle other kinds if necessary + break + } + } + } +}