Skip to content

Commit

Permalink
fix: 3.7.0 schema error
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 committed Nov 29, 2024
1 parent 48f067c commit 3e0b38e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 37 deletions.
17 changes: 11 additions & 6 deletions libs/backend-apisix/src/operator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as ADCSDK from '@api7/adc-sdk';
import { Axios } from 'axios';
import { ListrTask } from 'listr2';
import { SemVer, lt as semVerLT } from 'semver';
import { SemVer, gte as semVerGTE, lt as semVerLT } from 'semver';

import { FromADC } from './transformer';
import * as typing from './typing';
Expand Down Expand Up @@ -32,7 +32,7 @@ export class Operator {

const resp = await this.client.put(
`/apisix/admin/consumers/${event.parentId}/credentials/${event.resourceId}`,
this.fromADC(event),
this.fromADC(event, ctx.apisixVersion),
{
validateStatus: () => true,
},
Expand All @@ -43,7 +43,7 @@ export class Operator {
} else {
const resp = await this.client.put(
`/apisix/admin/${resourceTypeToAPIName(event.resourceType)}/${event.resourceId}`,
this.fromADC(event),
this.fromADC(event, ctx.apisixVersion),
{
validateStatus: () => true,
},
Expand Down Expand Up @@ -102,7 +102,7 @@ export class Operator {
)} ${event.resourceType}: "${event.resourceName}"`;
}

private fromADC(event: ADCSDK.Event) {
private fromADC(event: ADCSDK.Event, version: SemVer) {
const fromADC = new FromADC();
switch (event.resourceType) {
case ADCSDK.ResourceType.CONSUMER:
Expand All @@ -127,20 +127,25 @@ export class Operator {
return event.newValue;
case ADCSDK.ResourceType.ROUTE: {
(event.newValue as ADCSDK.Route).id = event.resourceId;
const route = fromADC.transformRoute(event.newValue as ADCSDK.Route);
const route = fromADC.transformRoute(
event.newValue as ADCSDK.Route,
event.parentId,
);
if (event.parentId) route.service_id = event.parentId;
return route;
}
case ADCSDK.ResourceType.SERVICE:
(event.newValue as ADCSDK.Service).id = event.resourceId;
return fromADC.transformService(event.newValue as ADCSDK.Service)[0];
return fromADC.transformService(event.newValue as ADCSDK.Service);
case ADCSDK.ResourceType.SSL:
(event.newValue as ADCSDK.SSL).id = event.resourceId;
return fromADC.transformSSL(event.newValue as ADCSDK.SSL);
case ADCSDK.ResourceType.STREAM_ROUTE: {
(event.newValue as ADCSDK.StreamRoute).id = event.resourceId;
const route = fromADC.transformStreamRoute(
event.newValue as ADCSDK.StreamRoute,
event.parentId,
semVerGTE(version, '3.8.0'),
);
if (event.parentId) route.service_id = event.parentId;
return route;
Expand Down
53 changes: 22 additions & 31 deletions libs/backend-apisix/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export class FromADC {
}, {});
}

public transformRoute(route: ADCSDK.Route): typing.Route {
public transformRoute(route: ADCSDK.Route, parentId: string): typing.Route {
return ADCSDK.utils.recursiveOmitUndefined<typing.Route>({
id: route.id,
name: route.name,
Expand All @@ -255,7 +255,7 @@ export class FromADC {
vars: route.vars,
filter_func: route.filter_func,

//service_id: '',
service_id: parentId,
enable_websocket: route.enable_websocket,
plugins: route.plugins,
priority: route.priority,
Expand All @@ -264,31 +264,16 @@ export class FromADC {
});
}

public transformService(
service: ADCSDK.Service,
): [typing.Service, Array<typing.Route>, Array<typing.StreamRoute>] {
const serviceId = ADCSDK.utils.generateId(service.name);
const routes: Array<typing.Route> =
service.routes
?.map(this.transformRoute)
.map((route) => ({ ...route, service_id: serviceId })) ?? [];
const streamRoutes: Array<typing.StreamRoute> =
service.stream_routes
?.map(this.transformStreamRoute)
.map((route) => ({ ...route, service_id: serviceId })) ?? [];
return [
ADCSDK.utils.recursiveOmitUndefined<typing.Service>({
id: service.id,
name: service.name,
desc: service.description,
labels: FromADC.transformLabels(service.labels),
upstream: service.upstream,
plugins: service.plugins,
hosts: service.hosts,
}),
routes,
streamRoutes,
];
public transformService(service: ADCSDK.Service): typing.Service {
return ADCSDK.utils.recursiveOmitUndefined<typing.Service>({
id: service.id,
name: service.name,
desc: service.description,
labels: FromADC.transformLabels(service.labels),
upstream: service.upstream,
plugins: service.plugins,
hosts: service.hosts,
});
}

public transformConsumer(consumer: ADCSDK.Consumer): typing.Consumer {
Expand Down Expand Up @@ -400,19 +385,25 @@ export class FromADC {

public transformStreamRoute(
streamRoute: ADCSDK.StreamRoute,
parentId: string,
injectName = true,
): typing.StreamRoute {
const labels = FromADC.transformLabels(streamRoute.labels);
return ADCSDK.utils.recursiveOmitUndefined({
id: undefined,
desc: streamRoute.description,
labels: {
...FromADC.transformLabels(streamRoute.labels),
__ADC_NAME: streamRoute.name,
},
labels: injectName
? labels
: {
...labels,
__ADC_NAME: streamRoute.name,
},
plugins: streamRoute.plugins,
remote_addr: streamRoute.remote_addr,
server_addr: streamRoute.server_addr,
server_port: streamRoute.server_port,
sni: streamRoute.sni,
service_id: parentId,
} as typing.StreamRoute);
}

Expand Down

0 comments on commit 3e0b38e

Please sign in to comment.