-
Notifications
You must be signed in to change notification settings - Fork 14
feat(api7): add server-side configuration validator #432
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e374db9
feat: add validate command for API7 EE backend
jarvis9443 1054b40
fix: use 'uris' instead of 'paths' in E2E test (ADC type)
jarvis9443 5106e64
fix: remove unused imports in E2E test
jarvis9443 627f08e
fix: expect plural resource_type 'routes' from validate API
jarvis9443 f07f059
fix: include resource_id and index in validation error messages
jarvis9443 01eae10
f
jarvis9443 a98878b
refactor: accept events instead of config in validate API
jarvis9443 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { Listr } from 'listr2'; | ||
|
|
||
| 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); | ||
| } | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.