Skip to content

Commit

Permalink
feat: load resources from repo
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo committed Aug 5, 2024
1 parent 7a31112 commit 1491704
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/common/utils/get-repository-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/kontinuous/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
3 changes: 3 additions & 0 deletions plugins/fabrique/kontinuous.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ patches:
setUndefinedStorageClassnameCnpg:
options:
defaultStorageClassName: csi-cinder-high-speed
infraResources:
options:
repositoryUrl: https://github.com/SocialGouv/infra-resources.git

dependencies:
contrib:
Expand Down
63 changes: 63 additions & 0 deletions plugins/fabrique/patches/03-infra-resources.js
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}

0 comments on commit 1491704

Please sign in to comment.