Skip to content

Commit 367f3c0

Browse files
committed
feat: attempt to migrate values on recovery from the values repo
1 parent 3ed93ab commit 367f3c0

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/cmd/migrate.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
applyChanges,
44
Changes,
55
filterChanges,
6+
needsMigration,
67
processDeletionEntry,
78
removeSopsArtifacts,
89
sopsMigration,
@@ -442,3 +443,33 @@ describe('processDeletionEntry', () => {
442443
expect(mockDeleteFile).not.toHaveBeenCalled()
443444
})
444445
})
446+
447+
describe('needsMigration', () => {
448+
const makeLoadYaml =
449+
(repoSpecVersion: number | undefined, defaultSpecVersion: number | undefined) =>
450+
async (path: string, _opts?: any): Promise<any> => {
451+
if (path.includes('versions.yaml')) return repoSpecVersion !== undefined ? { spec: { specVersion: repoSpecVersion } } : undefined
452+
if (path.includes('defaults.yaml')) return defaultSpecVersion !== undefined ? { versions: { specVersion: defaultSpecVersion } } : undefined
453+
return undefined
454+
}
455+
456+
it('returns true when repo specVersion differs from defaults specVersion', async () => {
457+
const result = await needsMigration({ loadYaml: makeLoadYaml(69, 70) })
458+
expect(result).toBe(true)
459+
})
460+
461+
it('returns false when repo specVersion matches defaults specVersion', async () => {
462+
const result = await needsMigration({ loadYaml: makeLoadYaml(70, 70) })
463+
expect(result).toBe(false)
464+
})
465+
466+
it('returns false when versions.yaml is missing', async () => {
467+
const result = await needsMigration({ loadYaml: makeLoadYaml(undefined, 70) })
468+
expect(result).toBe(false)
469+
})
470+
471+
it('returns false when defaults.yaml is missing', async () => {
472+
const result = await needsMigration({ loadYaml: makeLoadYaml(69, undefined) })
473+
expect(result).toBe(false)
474+
})
475+
})

src/cmd/migrate.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,15 @@ export const setAtPath = (path: string, values: Record<string, any>, value: stri
850850
paths.forEach((p) => set(values, p, Array.isArray(value) ? [...value] : value))
851851
}
852852

853+
export const needsMigration = async (deps = { loadYaml }): Promise<boolean> => {
854+
const defaults = await deps.loadYaml(`${rootDir}/helmfile.d/snippets/defaults.yaml`, { noError: true })
855+
const defaultSpecVersion: number = defaults?.versions?.specVersion
856+
const versions = await deps.loadYaml(`${env.ENV_DIR}/env/settings/versions.yaml`, { noError: true })
857+
const repoSpecVersion: number = versions?.spec?.specVersion
858+
if (!repoSpecVersion || !defaultSpecVersion) return false
859+
return repoSpecVersion !== defaultSpecVersion
860+
}
861+
853862
export const migrate = async (): Promise<boolean> => {
854863
const d = terminal(`cmd:${cmdName}:migrate`)
855864
const argv: Arguments = getParsedArgs()

src/operator/main.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import fs from 'fs'
33
import process from 'node:process'
44
import path from 'path'
55
import { retryInstallStep } from '../cmd/install'
6+
import { commit } from '../cmd/commit'
7+
import { needsMigration } from '../cmd/migrate'
68
import { terminal } from '../common/debug'
79
import { env } from '../common/envalid'
810
import { getStoredGitRepoConfig } from '../common/git-config'
11+
import { getDefaultValues, writeValues } from '../common/values'
912
import { AplOperations } from './apl-operations'
1013
import { AplOperator, AplOperatorConfig } from './apl-operator'
1114
import { GitRepository } from './git-repository'
@@ -79,6 +82,14 @@ async function main(): Promise<void> {
7982
await installer.ensureRecoveryPrerequisites()
8083
await installer.applyRecoveryManifests()
8184
await installer.recoverFromGit()
85+
if (await needsMigration()) {
86+
d.info('specVersion mismatch detected, running migration before install')
87+
const defaultValues = await getDefaultValues()
88+
await writeValues(defaultValues)
89+
await aplOps.migrate()
90+
const gitConfig = await getStoredGitRepoConfig()
91+
await commit(gitConfig)
92+
}
8293
d.info('Recovery installation completed, switching installation mode to standard')
8394
await installer.resetRecoveryModeToStandard()
8495
await installer.reconcileInstall()

0 commit comments

Comments
 (0)