Skip to content

Commit

Permalink
feat(api7): get plugin metadata in one request (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored Jul 19, 2024
1 parent 9674289 commit 9361c3e
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 58 deletions.
96 changes: 60 additions & 36 deletions libs/backend-api7/src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import * as ADCSDK from '@api7/adc-sdk';
import { Axios } from 'axios';
import { ListrTask } from 'listr2';
import { isEmpty } from 'lodash';
import { SemVer, gte as semVerGTE } from 'semver';

import { ToADC } from './transformer';
import * as typing from './typing';
import { buildReqAndRespDebugOutput } from './utils';

type FetchTask = ListrTask<{
api7Version: SemVer;
gatewayGroupId: string;
remote: ADCSDK.Configuration;
}>;
Expand Down Expand Up @@ -133,42 +135,11 @@ export class Fetcher {
title: 'Fetch plugin metadata',
skip: this.isSkip([ADCSDK.ResourceType.PLUGIN_METADATA]),
task: async (ctx, task) => {
const resp = await this.client.get<Array<string>>(
'/apisix/admin/plugins/list',
{
params: { has_metadata: true },
},
);
task.output = buildReqAndRespDebugOutput(
resp,
'Get plugins that contain plugin metadata',
);

const plugins = resp.data;
const getMetadataConfig = plugins.map<
Promise<[string, typing.PluginMetadata]>
>(async (pluginName) => {
try {
const resp = await this.client.get<{
value: typing.PluginMetadata;
}>(`/apisix/admin/plugin_metadata/${pluginName}`, {
params: { gateway_group_id: ctx.gatewayGroupId },
});
task.output = buildReqAndRespDebugOutput(
resp,
`Get plugin metadata for "${pluginName}"`,
);
return [pluginName, resp?.data?.value];
} catch (err) {
return [pluginName, null];
}
});
const metadataObj = Object.fromEntries(
(await Promise.all(getMetadataConfig)).filter((item) => item[1]),
);

ctx.remote.plugin_metadata =
this.toADC.transformPluginMetadatas(metadataObj);
if (semVerGTE(ctx.api7Version, '3.2.14')) {
return this.listMetadatasGTE03021400(ctx, task);
} else {
return this.listMetadatasLT03021400(ctx, task);
}
},
};
}
Expand Down Expand Up @@ -208,6 +179,59 @@ export class Fetcher {
};
}

// Get plugin metadata on API7 3.2.14.0 and up
private listMetadatasGTE03021400: FetchTask['task'] = async (ctx, task) => {
const resp = await this.client.get<{
value: ADCSDK.Plugins;
}>('/apisix/admin/plugin_metadata', {
params: { gateway_group_id: ctx.gatewayGroupId },
});
task.output = buildReqAndRespDebugOutput(resp, 'Get plugin metadata');
ctx.remote.plugin_metadata = this.toADC.transformPluginMetadatas(
resp.data.value,
);
};

// Get plugin metadata below API7 3.2.14.0
private listMetadatasLT03021400: FetchTask['task'] = async (ctx, task) => {
const resp = await this.client.get<Array<string>>(
'/apisix/admin/plugins/list',
{
params: { has_metadata: true },
},
);
task.output = buildReqAndRespDebugOutput(
resp,
'Get plugins that contain plugin metadata',
);

const plugins = resp.data;
const getMetadataConfig = plugins.map<
Promise<[string, typing.PluginMetadata]>
>(async (pluginName) => {
try {
const resp = await this.client.get<{
value: typing.PluginMetadata;
}>(`/apisix/admin/plugin_metadata/${pluginName}`, {
params: { gateway_group_id: ctx.gatewayGroupId },
});
task.output = buildReqAndRespDebugOutput(
resp,
`Get plugin metadata for "${pluginName}"`,
);
return [pluginName, resp?.data?.value];
} catch (err) {
return [pluginName, null];
}
});
const metadataObj = Object.fromEntries(
(await Promise.all(getMetadataConfig)).filter((item) => item[1]),
);

ctx.remote.plugin_metadata =
this.toADC.transformPluginMetadatas(metadataObj);
};

private attachLabelSelector(
params: Record<string, string>,
): Record<string, string> {
Expand Down
22 changes: 22 additions & 0 deletions libs/backend-api7/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Listr, ListrTask } from 'listr2';
import { isEmpty, isNil } from 'lodash';
import { readFileSync } from 'node:fs';
import { AgentOptions, Agent as httpsAgent } from 'node:https';
import semver, { SemVer } from 'semver';

import { Fetcher } from './fetcher';
import { OperateContext, Operator } from './operator';
Expand All @@ -17,6 +18,7 @@ export class BackendAPI7 implements ADCSDK.Backend {
private readonly gatewayGroup: string;
private static logScope = ['API7'];

private version: SemVer;
private gatewayGroupId: string;
private defaultValue: ADCSDK.DefaultValue;

Expand Down Expand Up @@ -177,11 +179,30 @@ export class BackendAPI7 implements ADCSDK.Backend {
};
}

private getAPI7VersionTask(): ListrTask {
return {
enabled: (ctx) => !ctx.api7Version,
task: async (ctx, task) => {
if (this.version) {
ctx.api7Version = this.version;
return;
}

const resp = await this.client.get<{ value: string }>('/api/version');
task.output = buildReqAndRespDebugOutput(resp, `Get API7 version`);
ctx.api7Version = this.version = semver.coerce(
resp?.data.value || '0.0.0',
);
},
};
}

public async dump(): Promise<Listr<{ remote: ADCSDK.Configuration }>> {
const fetcher = new Fetcher(this.client, this.opts);

return new Listr<{ remote: ADCSDK.Configuration }>(
[
this.getAPI7VersionTask(),
this.getResourceDefaultValueTask(),
this.getGatewayGroupIdTask(this.gatewayGroup),
...fetcher.allTask(),
Expand All @@ -197,6 +218,7 @@ export class BackendAPI7 implements ADCSDK.Backend {
const operator = new Operator(this.client, this.gatewayGroup);
return new Listr<OperateContext>(
[
this.getAPI7VersionTask(),
this.getGatewayGroupIdTask(this.gatewayGroup),
this.syncPreprocessEventsTask(),
{
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@types/node": "18.16.9",
"@types/pluralize": "^0.0.33",
"@types/qs": "^6.9.15",
"@types/semver": "^7.5.8",
"@types/signale": "^1.4.7",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
Expand Down Expand Up @@ -61,6 +62,7 @@
"pluralize": "^8.0.0",
"reflect-metadata": "^0.1.14",
"rxjs": "^7.8.1",
"semver": "^7.6.3",
"signale": "^1.4.0",
"slugify": "^1.6.6",
"tslib": "^2.6.2",
Expand Down
Loading

0 comments on commit 9361c3e

Please sign in to comment.