Skip to content
Snippets Groups Projects
Unverified Commit b3810b5f authored by Michael Kriese's avatar Michael Kriese Committed by GitHub
Browse files

fix(manager/flux): fix system manifest artifacts (#13912)

parent 73f9d8bf
No related branches found
No related tags found
No related merge requests found
import { exec, mockExecAll } from '../../../test/exec-util'; import { exec, mockExecAll } from '../../../test/exec-util';
import { fs } from '../../../test/util';
import { GlobalConfig } from '../../config/global';
import { updateArtifacts } from '.'; import { updateArtifacts } from '.';
jest.mock('child_process'); jest.mock('child_process');
jest.mock('../../util/fs');
describe('manager/flux/artifacts', () => { describe('manager/flux/artifacts', () => {
beforeEach(() => { beforeAll(() => {
jest.resetAllMocks(); GlobalConfig.set({
localDir: '',
});
}); });
it('replaces existing value', async () => { it('replaces existing value', async () => {
mockExecAll(exec, { stdout: 'test', stderr: '' }); mockExecAll(exec, { stdout: '', stderr: '' });
fs.readLocalFile.mockResolvedValueOnce('old');
fs.readLocalFile.mockResolvedValueOnce('test');
const res = await updateArtifacts({ const res = await updateArtifacts({
packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml', packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml',
...@@ -40,6 +47,19 @@ describe('manager/flux/artifacts', () => { ...@@ -40,6 +47,19 @@ describe('manager/flux/artifacts', () => {
expect(res).toBeNull(); expect(res).toBeNull();
}); });
it('ignores unchanged system manifests', async () => {
fs.readLocalFile.mockResolvedValueOnce('old');
fs.readLocalFile.mockResolvedValueOnce('old');
const res = await updateArtifacts({
packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml',
updatedDeps: [{ newVersion: '1.0.1' }],
newPackageFileContent: undefined,
config: {},
});
expect(res).toBeNull();
});
it('ignores system manifests without a new version', async () => { it('ignores system manifests without a new version', async () => {
const res = await updateArtifacts({ const res = await updateArtifacts({
packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml', packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml',
...@@ -51,7 +71,7 @@ describe('manager/flux/artifacts', () => { ...@@ -51,7 +71,7 @@ describe('manager/flux/artifacts', () => {
expect(res).toBeNull(); expect(res).toBeNull();
}); });
it('failed to generate manifests', async () => { it('failed to generate system manifest', async () => {
mockExecAll(exec, new Error('failed')); mockExecAll(exec, new Error('failed'));
const res = await updateArtifacts({ const res = await updateArtifacts({
packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml', packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml',
...@@ -60,8 +80,34 @@ describe('manager/flux/artifacts', () => { ...@@ -60,8 +80,34 @@ describe('manager/flux/artifacts', () => {
config: {}, config: {},
}); });
expect(res[0].artifactError.lockFile).toBe( expect(res).toStrictEqual([
'clusters/my-cluster/flux-system/gotk-components.yaml' {
); artifactError: {
lockFile: 'clusters/my-cluster/flux-system/gotk-components.yaml',
stderr: 'failed',
},
},
]);
});
it('failed to read system manifest', async () => {
mockExecAll(exec, { stdout: '', stderr: 'Error' });
fs.readLocalFile.mockResolvedValueOnce('old');
fs.readLocalFile.mockResolvedValueOnce('');
const res = await updateArtifacts({
packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml',
updatedDeps: [{ newVersion: '1.0.1' }],
newPackageFileContent: undefined,
config: {},
});
expect(res).toStrictEqual([
{
artifactError: {
lockFile: 'clusters/my-cluster/flux-system/gotk-components.yaml',
stderr: 'Error',
},
},
]);
}); });
}); });
import { quote } from 'shlex';
import { logger } from '../../logger'; import { logger } from '../../logger';
import { exec } from '../../util/exec'; import { exec } from '../../util/exec';
import type { ExecOptions } from '../../util/exec/types'; import type { ExecOptions } from '../../util/exec/types';
import { readLocalFile } from '../../util/fs';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types'; import type { UpdateArtifact, UpdateArtifactsResult } from '../types';
import { isSystemManifest } from './common'; import { isSystemManifest } from './common';
...@@ -11,9 +13,10 @@ export async function updateArtifacts({ ...@@ -11,9 +13,10 @@ export async function updateArtifacts({
if (!isSystemManifest(packageFileName) || !updatedDeps[0]?.newVersion) { if (!isSystemManifest(packageFileName) || !updatedDeps[0]?.newVersion) {
return null; return null;
} }
const existingFileContent = await readLocalFile(packageFileName);
try { try {
logger.debug(`Updating Flux system manifests`); logger.debug(`Updating Flux system manifests`);
const cmd = 'flux install --export'; const cmd = `flux install --export > ${quote(packageFileName)}`;
const execOptions: ExecOptions = { const execOptions: ExecOptions = {
docker: { docker: {
image: 'sidecar', image: 'sidecar',
...@@ -27,12 +30,29 @@ export async function updateArtifacts({ ...@@ -27,12 +30,29 @@ export async function updateArtifacts({
}; };
const result = await exec(cmd, execOptions); const result = await exec(cmd, execOptions);
const newFileContent = await readLocalFile(packageFileName);
if (!newFileContent) {
logger.debug('Cannot read new flux file content');
return [
{
artifactError: {
lockFile: packageFileName,
stderr: result.stderr,
},
},
];
}
if (newFileContent === existingFileContent) {
logger.debug('Flux contents are unchanged');
return null;
}
return [ return [
{ {
file: { file: {
type: 'addition', type: 'addition',
path: packageFileName, path: packageFileName,
contents: result.stdout, contents: newFileContent,
}, },
}, },
]; ];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment