Skip to content

Commit

Permalink
feat(core): add remote state file as experimental feature (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored Jan 24, 2025
1 parent 42bca11 commit d3a9dd7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
27 changes: 21 additions & 6 deletions apps/cli/src/command/diff.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import {
type DiffOptions = BackendOptions & {
file: Array<string>;
lint: boolean;

// experimental feature
remoteStateFile: string;
};

export interface TaskContext {
Expand All @@ -34,6 +37,16 @@ export interface TaskContext {
defaultValue?: ADCSDK.DefaultValue;
}

export const ExperimentalRemoteStateFileTask = (file: string) => ({
task: async (ctx) => {
const fileContent =
(await readFile(file, {
encoding: 'utf-8',
})) ?? '';
ctx.remote = YAML.parse(fileContent);
},
});

export const LoadLocalConfigurationTask = (
files: Array<string>,
labelSelector?: BackendOptions['labelSelector'],
Expand Down Expand Up @@ -200,12 +213,14 @@ export const DiffCommand = new BackendCommand<DiffOptions>(
opts.excludeResourceType,
),
opts.lint ? LintTask() : { task: () => undefined },
LoadRemoteConfigurationTask({
backend,
labelSelector: opts.labelSelector,
includeResourceType: opts.includeResourceType,
excludeResourceType: opts.excludeResourceType,
}),
!opts.remoteStateFile
? LoadRemoteConfigurationTask({
backend,
labelSelector: opts.labelSelector,
includeResourceType: opts.includeResourceType,
excludeResourceType: opts.excludeResourceType,
})
: ExperimentalRemoteStateFileTask(opts.remoteStateFile),
DiffResourceTask(true, true),
{
title: 'Write detail diff result to file',
Expand Down
14 changes: 13 additions & 1 deletion apps/cli/src/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,23 @@ export const setupCommands = (): Command => {
const program = new Command('adc');

program
.description('API Declarative CLI (ADC) is a utility to manage API7 Enterprise and Apache APISIX declaratively.\n\nLearn more at: https://docs.api7.ai/enterprise/reference/adc')
.description(
'API Declarative CLI (ADC) is a utility to manage API7 Enterprise and Apache APISIX declaratively.\n\nLearn more at: https://docs.api7.ai/enterprise/reference/adc',
)
.configureHelp({ showGlobalOptions: true })
.passThroughOptions()
.version('0.17.0', '-v, --version', 'display ADC version');

if (
process.env.ADC_EXPERIMENTAL_FEATURE_FLAGS &&
process.env.ADC_EXPERIMENTAL_FEATURE_FLAGS.includes('remote-state-file')
) {
const desc =
'path of the remote state file, which will allow the ADC to skip the initial dump process and use the ADC configuration contained in the remote state file directly';
DiffCommand.option('--remote-state-file <file-path>', desc);
SyncCommand.option('--remote-state-file <file-path>', desc);
}

program
.addCommand(PingCommand)
.addCommand(DumpCommand)
Expand Down
18 changes: 12 additions & 6 deletions apps/cli/src/command/sync.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Listr } from 'listr2';
import { SignaleRenderer } from '../utils/listr';
import {
DiffResourceTask,
ExperimentalRemoteStateFileTask,
LoadLocalConfigurationTask,
TaskContext,
} from './diff.command';
Expand All @@ -15,6 +16,9 @@ import { loadBackend } from './utils';
type SyncOption = BackendOptions & {
file: Array<string>;
lint: boolean;

// experimental feature
remoteStateFile: string;
};

export const SyncCommand = new BackendCommand<SyncOption>(
Expand Down Expand Up @@ -76,12 +80,14 @@ export const SyncCommand = new BackendCommand<SyncOption>(
opts.excludeResourceType,
),
opts.lint ? LintTask() : { task: () => undefined },
LoadRemoteConfigurationTask({
backend,
labelSelector: opts.labelSelector,
includeResourceType: opts.includeResourceType,
excludeResourceType: opts.excludeResourceType,
}),
!opts.remoteStateFile
? LoadRemoteConfigurationTask({
backend,
labelSelector: opts.labelSelector,
includeResourceType: opts.includeResourceType,
excludeResourceType: opts.excludeResourceType,
})
: ExperimentalRemoteStateFileTask(opts.remoteStateFile),
DiffResourceTask(false, false),
{
title: 'Sync configuration',
Expand Down

0 comments on commit d3a9dd7

Please sign in to comment.