Skip to content

Commit d3a9dd7

Browse files
authored
feat(core): add remote state file as experimental feature (#229)
1 parent 42bca11 commit d3a9dd7

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

apps/cli/src/command/diff.command.ts

+21-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import {
2525
type DiffOptions = BackendOptions & {
2626
file: Array<string>;
2727
lint: boolean;
28+
29+
// experimental feature
30+
remoteStateFile: string;
2831
};
2932

3033
export interface TaskContext {
@@ -34,6 +37,16 @@ export interface TaskContext {
3437
defaultValue?: ADCSDK.DefaultValue;
3538
}
3639

40+
export const ExperimentalRemoteStateFileTask = (file: string) => ({
41+
task: async (ctx) => {
42+
const fileContent =
43+
(await readFile(file, {
44+
encoding: 'utf-8',
45+
})) ?? '';
46+
ctx.remote = YAML.parse(fileContent);
47+
},
48+
});
49+
3750
export const LoadLocalConfigurationTask = (
3851
files: Array<string>,
3952
labelSelector?: BackendOptions['labelSelector'],
@@ -200,12 +213,14 @@ export const DiffCommand = new BackendCommand<DiffOptions>(
200213
opts.excludeResourceType,
201214
),
202215
opts.lint ? LintTask() : { task: () => undefined },
203-
LoadRemoteConfigurationTask({
204-
backend,
205-
labelSelector: opts.labelSelector,
206-
includeResourceType: opts.includeResourceType,
207-
excludeResourceType: opts.excludeResourceType,
208-
}),
216+
!opts.remoteStateFile
217+
? LoadRemoteConfigurationTask({
218+
backend,
219+
labelSelector: opts.labelSelector,
220+
includeResourceType: opts.includeResourceType,
221+
excludeResourceType: opts.excludeResourceType,
222+
})
223+
: ExperimentalRemoteStateFileTask(opts.remoteStateFile),
209224
DiffResourceTask(true, true),
210225
{
211226
title: 'Write detail diff result to file',

apps/cli/src/command/index.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,23 @@ export const setupCommands = (): Command => {
1414
const program = new Command('adc');
1515

1616
program
17-
.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')
17+
.description(
18+
'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',
19+
)
1820
.configureHelp({ showGlobalOptions: true })
1921
.passThroughOptions()
2022
.version('0.17.0', '-v, --version', 'display ADC version');
2123

24+
if (
25+
process.env.ADC_EXPERIMENTAL_FEATURE_FLAGS &&
26+
process.env.ADC_EXPERIMENTAL_FEATURE_FLAGS.includes('remote-state-file')
27+
) {
28+
const desc =
29+
'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';
30+
DiffCommand.option('--remote-state-file <file-path>', desc);
31+
SyncCommand.option('--remote-state-file <file-path>', desc);
32+
}
33+
2234
program
2335
.addCommand(PingCommand)
2436
.addCommand(DumpCommand)

apps/cli/src/command/sync.command.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Listr } from 'listr2';
33
import { SignaleRenderer } from '../utils/listr';
44
import {
55
DiffResourceTask,
6+
ExperimentalRemoteStateFileTask,
67
LoadLocalConfigurationTask,
78
TaskContext,
89
} from './diff.command';
@@ -15,6 +16,9 @@ import { loadBackend } from './utils';
1516
type SyncOption = BackendOptions & {
1617
file: Array<string>;
1718
lint: boolean;
19+
20+
// experimental feature
21+
remoteStateFile: string;
1822
};
1923

2024
export const SyncCommand = new BackendCommand<SyncOption>(
@@ -76,12 +80,14 @@ export const SyncCommand = new BackendCommand<SyncOption>(
7680
opts.excludeResourceType,
7781
),
7882
opts.lint ? LintTask() : { task: () => undefined },
79-
LoadRemoteConfigurationTask({
80-
backend,
81-
labelSelector: opts.labelSelector,
82-
includeResourceType: opts.includeResourceType,
83-
excludeResourceType: opts.excludeResourceType,
84-
}),
83+
!opts.remoteStateFile
84+
? LoadRemoteConfigurationTask({
85+
backend,
86+
labelSelector: opts.labelSelector,
87+
includeResourceType: opts.includeResourceType,
88+
excludeResourceType: opts.excludeResourceType,
89+
})
90+
: ExperimentalRemoteStateFileTask(opts.remoteStateFile),
8591
DiffResourceTask(false, false),
8692
{
8793
title: 'Sync configuration',

0 commit comments

Comments
 (0)