Skip to content

Commit

Permalink
feat: update manifests with preprod resources (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo authored Jun 6, 2024
1 parent e4cfd13 commit d9ac7b0
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
18 changes: 13 additions & 5 deletions .github/workflows/tests-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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: |
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 @@ -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 d9ac7b0

Please sign in to comment.