diff --git a/libs/backend-apisix/src/operator.ts b/libs/backend-apisix/src/operator.ts index 2c2a83b..4457214 100644 --- a/libs/backend-apisix/src/operator.ts +++ b/libs/backend-apisix/src/operator.ts @@ -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'; @@ -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, }, @@ -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, }, @@ -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: @@ -127,13 +127,16 @@ 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); @@ -141,6 +144,8 @@ export class Operator { (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; diff --git a/libs/backend-apisix/src/transformer.ts b/libs/backend-apisix/src/transformer.ts index 3e9d1e0..5c29422 100644 --- a/libs/backend-apisix/src/transformer.ts +++ b/libs/backend-apisix/src/transformer.ts @@ -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({ id: route.id, name: route.name, @@ -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, @@ -264,31 +264,16 @@ export class FromADC { }); } - public transformService( - service: ADCSDK.Service, - ): [typing.Service, Array, Array] { - const serviceId = ADCSDK.utils.generateId(service.name); - const routes: Array = - service.routes - ?.map(this.transformRoute) - .map((route) => ({ ...route, service_id: serviceId })) ?? []; - const streamRoutes: Array = - service.stream_routes - ?.map(this.transformStreamRoute) - .map((route) => ({ ...route, service_id: serviceId })) ?? []; - return [ - ADCSDK.utils.recursiveOmitUndefined({ - 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({ + 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 { @@ -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); }