Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(manager/flux): Update system manifests. #13675

Merged
merged 15 commits into from Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4,277 changes: 4,277 additions & 0 deletions lib/manager/flux/__fixtures__/system.yaml

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions lib/manager/flux/artifacts.spec.ts
@@ -0,0 +1,67 @@
import { exec, mockExecAll } from '../../../test/exec-util';
import { updateArtifacts } from '.';

jest.mock('child_process');

describe('manager/flux/artifacts', () => {
beforeEach(() => {
jest.resetAllMocks();
});

it('replaces existing value', async () => {
mockExecAll(exec, { stdout: 'test', stderr: '' });

const res = await updateArtifacts({
packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml',
updatedDeps: [{ newVersion: '1.0.1' }],
newPackageFileContent: undefined,
config: {},
});

expect(res).toEqual([
{
file: {
type: 'addition',
path: 'clusters/my-cluster/flux-system/gotk-components.yaml',
contents: 'test',
},
},
]);
});

it('ignores non-system manifests', async () => {
const res = await updateArtifacts({
packageFileName: 'not-a-system-manifest.yaml',
updatedDeps: [{ newVersion: '1.0.1' }],
newPackageFileContent: undefined,
config: {},
});

expect(res).toBeNull();
});

it('ignores system manifests without a new version', async () => {
const res = await updateArtifacts({
packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml',
updatedDeps: [{ newVersion: undefined }],
newPackageFileContent: undefined,
config: {},
});

expect(res).toBeNull();
});

it('failed to generate manifests', async () => {
mockExecAll(exec, new Error('failed'));
const res = await updateArtifacts({
packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml',
updatedDeps: [{ newVersion: '1.0.1' }],
newPackageFileContent: undefined,
config: {},
});

expect(res[0].artifactError.lockFile).toBe(
'clusters/my-cluster/flux-system/gotk-components.yaml'
);
});
});
50 changes: 50 additions & 0 deletions lib/manager/flux/artifacts.ts
@@ -0,0 +1,50 @@
import { logger } from '../../logger';
import { exec } from '../../util/exec';
import type { ExecOptions } from '../../util/exec/types';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types';
import { isSystemManifest } from './common';

export async function updateArtifacts({
packageFileName,
updatedDeps,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
if (!isSystemManifest(packageFileName) || !updatedDeps[0]?.newVersion) {
danports marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
try {
logger.debug(`Updating Flux system manifests`);
const cmd = 'flux install --export';
const execOptions: ExecOptions = {
docker: {
image: 'sidecar',
},
toolConstraints: [
{
toolName: 'flux',
constraint: updatedDeps[0].newVersion,
},
],
};
const result = await exec(cmd, execOptions);

return [
{
file: {
type: 'addition',
path: packageFileName,
danports marked this conversation as resolved.
Show resolved Hide resolved
contents: result.stdout,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unfortunately also captures the output from install-tool

},
},
];
} catch (err) {
logger.debug({ err }, 'Error generating new Flux system manifests');
return [
{
artifactError: {
lockFile: packageFileName,
stderr: err.message,
},
},
];
}
}
6 changes: 6 additions & 0 deletions lib/manager/flux/common.ts
@@ -0,0 +1,6 @@
import { regEx } from '../../util/regex';

export const systemManifestRegex = '(^|/)flux-system/gotk-components\\.yaml$';
export function isSystemManifest(file: string): boolean {
return regEx(systemManifestRegex).test(file);
}
73 changes: 55 additions & 18 deletions lib/manager/flux/extract.spec.ts
Expand Up @@ -14,28 +14,56 @@ describe('manager/flux/extract', () => {

describe('extractPackageFile()', () => {
it('extracts multiple resources', () => {
const result = extractPackageFile(loadFixture('multidoc.yaml'));
const result = extractPackageFile(
loadFixture('multidoc.yaml'),
'multidoc.yaml'
);
expect(result).toEqual({
datasource: 'helm',
deps: [
{
currentValue: '1.7.0',
datasource: 'helm',
depName: 'external-dns',
registryUrls: ['https://kubernetes-sigs.github.io/external-dns/'],
},
],
});
});
it('extracts version from system manifests', () => {
const result = extractPackageFile(
loadFixture('system.yaml'),
'clusters/my-cluster/flux-system/gotk-components.yaml'
);
expect(result).toEqual({
deps: [
{
currentValue: 'v0.24.1',
datasource: 'github-releases',
depName: 'fluxcd/flux2',
},
],
});
});
it('ignores system manifests without a version', () => {
const result = extractPackageFile(
'not actually a system manifest!',
'clusters/my-cluster/flux-system/gotk-components.yaml'
);
expect(result).toBeNull();
});
it('extracts releases without repositories', () => {
const result = extractPackageFile(loadFixture('release.yaml'));
const result = extractPackageFile(
loadFixture('release.yaml'),
'release.yaml'
);
expect(result.deps[0].skipReason).toBe('unknown-registry');
});
it('ignores HelmRelease resources without an apiVersion', () => {
const result = extractPackageFile('kind: HelmRelease');
const result = extractPackageFile('kind: HelmRelease', 'test.yaml');
expect(result).toBeNull();
});
it('ignores HelmRepository resources without an apiVersion', () => {
const result = extractPackageFile('kind: HelmRepository');
const result = extractPackageFile('kind: HelmRepository', 'test.yaml');
expect(result).toBeNull();
});
it('ignores HelmRepository resources without metadata', () => {
Expand All @@ -44,7 +72,8 @@ describe('manager/flux/extract', () => {
---
apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: HelmRepository
`
`,
'test.yaml'
);
expect(result.deps[0].skipReason).toBe('unknown-registry');
});
Expand All @@ -62,7 +91,8 @@ spec:
kind: HelmRepository
name: sealed-secrets
version: "2.0.2"
`
`,
'test.yaml'
);
expect(result).toBeNull();
});
Expand All @@ -85,7 +115,8 @@ spec:
kind: HelmRepository
name: sealed-secrets
version: "2.0.2"
`
`,
'test.yaml'
);
expect(result.deps[0].skipReason).toBe('unknown-registry');
});
Expand All @@ -102,7 +133,8 @@ spec:
spec:
chart: sealed-secrets
version: "2.0.2"
`
`,
'test.yaml'
);
expect(result.deps[0].skipReason).toBe('unknown-registry');
});
Expand All @@ -120,7 +152,8 @@ spec:
kind: HelmRepository
name: sealed-secrets
version: "2.0.2"
`
`,
'test.yaml'
);
expect(result.deps[0].skipReason).toBe('unknown-registry');
});
Expand All @@ -132,7 +165,8 @@ apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: HelmRepository
metadata:
name: test
`
`,
'test.yaml'
);
expect(result.deps[0].skipReason).toBe('unknown-registry');
});
Expand All @@ -145,29 +179,32 @@ kind: HelmRepository
metadata:
name: sealed-secrets
namespace: kube-system
`
`,
'test.yaml'
);
expect(result.deps[0].skipReason).toBe('unknown-registry');
});
it('ignores resources of an unknown kind', () => {
const result = extractPackageFile(
`kind: SomethingElse
apiVersion: helm.toolkit.fluxcd.io/v2beta1`
apiVersion: helm.toolkit.fluxcd.io/v2beta1`,
'test.yaml'
);
expect(result).toBeNull();
});
it('ignores resources without a kind', () => {
const result = extractPackageFile(
'apiVersion: helm.toolkit.fluxcd.io/v2beta1'
'apiVersion: helm.toolkit.fluxcd.io/v2beta1',
'test.yaml'
);
expect(result).toBeNull();
});
it('ignores bad manifests', () => {
const result = extractPackageFile('"bad YAML');
const result = extractPackageFile('"bad YAML', 'test.yaml');
expect(result).toBeNull();
});
it('ignores null resources', () => {
const result = extractPackageFile('null');
const result = extractPackageFile('null', 'test.yaml');
expect(result).toBeNull();
});
});
Expand All @@ -180,11 +217,11 @@ apiVersion: helm.toolkit.fluxcd.io/v2beta1`
]);
expect(result).toEqual([
{
datasource: 'helm',
deps: [
{
depName: 'sealed-secrets',
currentValue: '2.0.2',
datasource: 'helm',
depName: 'sealed-secrets',
registryUrls: ['https://bitnami-labs.github.io/sealed-secrets'],
},
],
Expand Down