-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update manifests with preprod resources (#490)
- Loading branch information
Showing
5 changed files
with
87 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
plugins/contrib/patches/80-update-manifests-with-preprod-resources.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters