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: support custom resource id #203

Merged
merged 18 commits into from
Nov 11, 2024
Merged
7 changes: 7 additions & 0 deletions apps/cli/src/command/dump.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {

type DumpOptions = BackendOptions & {
output: string;
withId: boolean;
};

export interface LoadRemoteConfigurationTaskOptions {
Expand Down Expand Up @@ -74,6 +75,7 @@ export const DumpCommand = new BackendCommand<DumpOptions>(
'path of the file to save the configuration',
'adc.yaml',
)
.option('--with-id', 'dump remote resources id')
.addExamples([
{
title: 'Save backend configuration to the default adc.yaml file',
Expand All @@ -92,6 +94,10 @@ export const DumpCommand = new BackendCommand<DumpOptions>(
title: 'Save only the resources with the specified labels',
command: 'adc dump --label-selector app=catalog',
},
{
title: 'Save the remote resources id',
command: 'adc dump --with-id',
},
])
.handle(async (opts) => {
const backend = loadBackend(opts.backend, opts);
Expand All @@ -105,6 +111,7 @@ export const DumpCommand = new BackendCommand<DumpOptions>(
}),
{
// Remove output resource metadata fields
enabled: !opts.withId,
task: (ctx) => recursiveRemoveMetadataField(ctx.remote),
},
{
Expand Down
1 change: 1 addition & 0 deletions apps/cli/src/command/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export const filterResourceType = (

export const recursiveRemoveMetadataField = (c: ADCSDK.Configuration) => {
const removeMetadata = (obj: object) => {
if ('id' in obj) delete obj.id;
if ('metadata' in obj) delete obj.metadata;
};
Object.entries(c).forEach(([key, value]) => {
Expand Down
59 changes: 21 additions & 38 deletions apps/cli/src/differ/differv3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,19 @@ export class DifferV3 {
ADCSDK.ResourceType.SERVICE,
local?.services?.map((res) => [
res.name,
ADCSDK.utils.generateId(res.name),
res,
]) ?? [],
remote?.services?.map((res) => [
res.name,
ADCSDK.utils.generateId(res.name),
res.id ?? ADCSDK.utils.generateId(res.name),
res,
]) ?? [],
remote?.services?.map((res) => [res.name, res.id, res]) ?? [],
),
...differ.diffResource(
ADCSDK.ResourceType.SSL,
local?.ssls?.map((res) => [
res.snis.join(','),
ADCSDK.utils.generateId(res.snis.join(',')),
res,
]) ?? [],
remote?.ssls?.map((res) => [
res.snis.join(','),
ADCSDK.utils.generateId(res.snis.join(',')),
res.id ?? ADCSDK.utils.generateId(res.snis.join(',')),
res,
]) ?? [],
remote?.ssls?.map((res) => [res.snis.join(','), res.id, res]) ?? [],
),
...differ.diffResource(
ADCSDK.ResourceType.CONSUMER,
Expand Down Expand Up @@ -144,40 +136,29 @@ export class DifferV3 {
ADCSDK.ResourceType.ROUTE,
local?.routes?.map((res) => [
res.name,
ADCSDK.utils.generateId(generateResourceName(res.name)),
res,
]) ?? [],
remote?.routes?.map((res) => [
res.name,
ADCSDK.utils.generateId(generateResourceName(res.name)),
res.id ?? ADCSDK.utils.generateId(generateResourceName(res.name)),
res,
]) ?? [],
remote?.routes?.map((res) => [res.name, res.id, res]) ?? [],
),
...differ.diffResource(
ADCSDK.ResourceType.STREAM_ROUTE,
local?.stream_routes?.map((res) => [
res.name,
ADCSDK.utils.generateId(generateResourceName(res.name)),
res,
]) ?? [],
remote?.stream_routes?.map((res) => [
res.name,
ADCSDK.utils.generateId(generateResourceName(res.name)),
res.id ?? ADCSDK.utils.generateId(generateResourceName(res.name)),
res,
]) ?? [],
remote?.stream_routes?.map((res) => [res.name, res.id, res]) ?? [],
),
...differ.diffResource(
ADCSDK.ResourceType.CONSUMER_CREDENTIAL,
local?.consumer_credentials?.map((res) => [
res.name,
ADCSDK.utils.generateId(generateResourceName(res.name)),
res,
]) ?? [],
remote?.consumer_credentials?.map((res) => [
res.name,
ADCSDK.utils.generateId(generateResourceName(res.name)),
res.id ?? ADCSDK.utils.generateId(generateResourceName(res.name)),
res,
]) ?? [],
remote?.consumer_credentials?.map((res) => [res.name, res.id, res]) ??
[],
),
/* ...differ.diffResource(
ADCSDK.ResourceType.UPSTREAM,
Expand Down Expand Up @@ -321,22 +302,20 @@ export class DifferV3 {

const checkedRemoteId: Array<ADCSDK.ResourceId> = [];
remote.forEach(([remoteName, remoteId, remoteItem]) => {
const remoteMetadata = cloneDeep(
(remoteItem as { metadata: ADCSDK.ResourceMetadata })?.metadata,
);
unset(remoteItem, 'metadata');
const eventResourceId = remoteMetadata?.id ?? remoteId;
unset(remoteItem, 'id');

// Asserts that the remote resource should exist locally, and that
// non-existence means that the user deleted that resource.
const localItem = localIdMap[eventResourceId];
const localItem = localIdMap[remoteId];
unset(localItem, 'id');

// Exists remotely but not locally: resource deleted by user
if (!localItem) {
return result.push({
resourceType,
type: ADCSDK.EventType.DELETE,
resourceId: eventResourceId,
resourceId: remoteId,
resourceName: remoteName,
oldValue: remoteItem,

Expand All @@ -359,13 +338,13 @@ export class DifferV3 {
resourceType === ADCSDK.ResourceType.SERVICE
? remoteName
: undefined,
).map(this.postprocessSubEvent(remoteName, eventResourceId)),
).map(this.postprocessSubEvent(remoteName, remoteId)),
});
}

// Record the remote IDs that have been checked. It will be used
// to identify locally added resources.
checkedRemoteId.push(eventResourceId);
checkedRemoteId.push(remoteId);

const originalLocalItem = cloneDeep(localItem);

Expand Down Expand Up @@ -588,6 +567,10 @@ export class DifferV3 {
// Exists locally but not remotely: resource created by user
local.forEach(([localName, localId, localItem]) => {
if (checkedRemoteId.includes(localId)) return;

unset(localItem, 'metadata');
unset(localItem, 'id');

return result.push({
resourceType,
type: ADCSDK.EventType.CREATE,
Expand Down
Loading
Loading