Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/cli/src/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IngressSyncCommand } from './ingress-sync.command';
import { LintCommand } from './lint.command';
import { PingCommand } from './ping.command';
import { SyncCommand } from './sync.command';
import { ValidateCommand } from './validate.command';
import { configurePluralize } from './utils';

const versionCode = '0.24.3';
Expand Down Expand Up @@ -47,8 +48,8 @@ export const setupCommands = (): Command => {
.addCommand(DiffCommand)
.addCommand(SyncCommand)
.addCommand(ConvertCommand)
.addCommand(LintCommand);
//.addCommand(ValidateCommand)
.addCommand(LintCommand)
.addCommand(ValidateCommand);

if (process.env.NODE_ENV === 'development') program.addCommand(DevCommand);

Expand Down
77 changes: 77 additions & 0 deletions apps/cli/src/command/validate.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Listr } from 'listr2';
Comment thread
jarvis9443 marked this conversation as resolved.

import {
DiffResourceTask,
LintTask,
LoadLocalConfigurationTask,
ValidateTask,
} from '../tasks';
import { InitializeBackendTask } from '../tasks/init_backend';
import { SignaleRenderer } from '../utils/listr';
import { TaskContext } from './diff.command';
import { BackendCommand, NoLintOption } from './helper';
import { BackendOptions } from './typing';

export type ValidateOptions = BackendOptions & {
file: Array<string>;
lint: boolean;
};

export const ValidateCommand = new BackendCommand<ValidateOptions>(
'validate',
'validate the local configuration against the backend',
'Validate the configuration from the local file(s) against the backend without applying any changes.',
)
.option(
'-f, --file <file-path>',
'file to validate',
(filePath, files: Array<string> = []) => files.concat(filePath),
)
.addOption(NoLintOption)
.addExamples([
{
title: 'Validate configuration from a single file',
command: 'adc validate -f adc.yaml',
},
{
title: 'Validate configuration from multiple files',
command: 'adc validate -f service-a.yaml -f service-b.yaml',
},
{
title: 'Validate configuration against API7 EE backend',
command:
'adc validate -f adc.yaml --backend api7ee --gateway-group default',
},
{
title: 'Validate configuration without lint check',
command: 'adc validate -f adc.yaml --no-lint',
},
])
.handle(async (opts) => {
const tasks = new Listr<TaskContext, typeof SignaleRenderer>(
[
InitializeBackendTask(opts.backend, opts),
LoadLocalConfigurationTask(
opts.file,
opts.labelSelector,
opts.includeResourceType,
opts.excludeResourceType,
),
opts.lint ? LintTask() : { task: () => undefined },
DiffResourceTask(),
ValidateTask(),
],
{
renderer: SignaleRenderer,
rendererOptions: { verbose: opts.verbose },
ctx: { remote: {}, local: {}, diff: [] },
},
);

try {
await tasks.run();
} catch (err) {
if (opts.verbose === 2) console.log(err);
process.exit(1);
}
});
1 change: 1 addition & 0 deletions apps/cli/src/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from './load_local';
export * from './load_remote';
export * from './diff';
export * from './lint';
export * from './validate';
export * from './experimental';
48 changes: 48 additions & 0 deletions apps/cli/src/tasks/validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as ADCSDK from '@api7/adc-sdk';
import { ListrTask } from 'listr2';
import { lastValueFrom } from 'rxjs';

export const ValidateTask = (): ListrTask<{
backend: ADCSDK.Backend;
diff: ADCSDK.Event[];
}> => ({
title: 'Validate configuration against backend',
task: async (ctx) => {
if (!ctx.backend.supportValidate) {
throw new Error(
'Validate is not supported by the current backend',
);
}

const supported = await ctx.backend.supportValidate();
if (!supported) {
const version = await ctx.backend.version();
throw new Error(
`Validate is not supported by the current backend version (${version}). Please upgrade to a newer version.`,
);
}

const result = await lastValueFrom(ctx.backend.validate!(ctx.diff));
Comment on lines +17 to +25
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
const supported = await ctx.backend.supportValidate();
if (!supported) {
const version = await ctx.backend.version();
throw new Error(
`Validate is not supported by the current backend version (${version}). Please upgrade to a newer version.`,
);
}
const result = await lastValueFrom(ctx.backend.validate!(ctx.diff));
if (!ctx.backend.validate)
throw new Error(`Validate is not supported by the current backend.`);
const result = await lastValueFrom(ctx.backend.validate(ctx.diff));

ref: https://github.com/api7/adc/pull/432/changes#r3104831632

if (!result.success) {
const lines: string[] = [];
if (result.errorMessage) {
lines.push(result.errorMessage);
}
for (const e of result.errors) {
const parts: string[] = [e.resource_type];
if (e.resource_name) {
parts.push(`name="${e.resource_name}"`);
} else {
if (e.resource_id) parts.push(`id="${e.resource_id}"`);
if (e.index !== undefined) parts.push(`index=${e.index}`);
}
lines.push(` - [${parts.join(', ')}]: ${e.error}`);
}
const error = new Error(
`Configuration validation failed:\n${lines.join('\n')}`,
);
error.stack = '';
throw error;
}
},
});
Loading
Loading