Skip to content

Commit

Permalink
feat: updateManifestsWithPreprodResources
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo committed Jun 5, 2024
1 parent dbb9f50 commit 42b2a19
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/kontinuous/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]),
}
Expand Down
2 changes: 2 additions & 0 deletions plugins/contrib/kontinuous.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ patches:
enabled: false
zeroDowntimeReadiness:
enabled: false
updateManifestsWithPreprodResources:
enabled: false

validators:
rancherProjectId:
Expand Down
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)
}
}
}
}
2 changes: 2 additions & 0 deletions plugins/fabrique/kontinuous.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ dependencies:
buildkitSvcCount: 6

patches:
updateManifestsWithPreprodResources:
enabled: true
zeroDowntimeReadiness:
enabled: true
certs:
Expand Down

0 comments on commit 42b2a19

Please sign in to comment.