Skip to content

Commit 0c2b6da

Browse files
committed
fix(apisix): transform upstream nodes to array
1 parent 473363f commit 0c2b6da

4 files changed

Lines changed: 29 additions & 11 deletions

File tree

.github/workflows/unit.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ jobs:
2323
run: npx nx run sdk:test
2424
- name: Run CLI unit tests
2525
run: npx nx run cli:test
26+
- name: Run APISIX Backend unit tests
27+
run: npx nx run backend-apisix:test
2628
- name: Run API7 Backend unit tests
2729
run: npx nx run backend-api7:test
2830
- name: Run OpenAPI Converter unit tests

libs/backend-apisix/src/transformer.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class ToADC {
4747

4848
hosts: service.hosts,
4949

50-
upstream: service.upstream,
50+
upstream: this.transformUpstream(service.upstream),
5151
upstreams: service.upstreams,
5252
plugins: service.plugins,
5353
} as ADCSDK.Service);
@@ -173,7 +173,9 @@ export class ToADC {
173173
} as ADCSDK.StreamRoute);
174174
}
175175

176-
public transformUpstream(upstream: typing.Upstream): ADCSDK.Upstream {
176+
public transformUpstream(
177+
upstream: typing.Upstream | typing.InlineUpstream,
178+
): ADCSDK.Upstream {
177179
const defaultPortMap: Record<string, number> = {
178180
http: 80,
179181
https: 443,
@@ -202,8 +204,8 @@ export class ToADC {
202204
[typing.ADC_UPSTREAM_SERVICE_ID_LABEL]: undefined,
203205
});
204206
return ADCSDK.utils.recursiveOmitUndefined({
205-
id: upstream.id,
206-
name: upstream.name ?? upstream.id,
207+
...{ id: 'id' in upstream ? upstream.id : undefined },
208+
name: upstream.name,
207209
description: upstream.desc,
208210
labels: Object.keys(labels).length > 0 ? labels : undefined,
209211

@@ -280,7 +282,7 @@ export class FromADC {
280282
name: service.name,
281283
desc: service.description,
282284
labels: FromADC.transformLabels(service.labels),
283-
upstream: service.upstream,
285+
upstream: this.transformUpstream(service.upstream),
284286
plugins: service.plugins,
285287
hosts: service.hosts,
286288
});

libs/backend-apisix/src/typing.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
GlobalRule as ADCGlobalRule,
33
PluginMetadata as ADCPluginMetadata,
4-
Upstream as ADCUpstream,
54
Expr,
65
Labels,
76
Plugins,
@@ -38,7 +37,7 @@ export interface Route {
3837
script_id?: string;
3938
plugins?: Plugins;
4039
plugin_config_id?: string;
41-
upstream?: ADCUpstream;
40+
upstream?: InlineUpstream;
4241
upstream_id?: string;
4342
service_id?: string;
4443
timeout?: UpstreamTimeout;
@@ -55,14 +54,14 @@ export interface Service {
5554
labels?: Labels;
5655

5756
hosts?: Array<string>;
58-
upstream?: ADCUpstream;
57+
upstream?: InlineUpstream;
5958
upstream_id?: string;
6059
plugins?: Plugins;
6160
script?: string;
6261
enable_websocket?: boolean;
6362

6463
// internal use only
65-
upstreams?: Array<ADCUpstream>;
64+
upstreams?: Array<InlineUpstream>;
6665
}
6766
export interface ConsumerCredential {
6867
id?: string;
@@ -128,7 +127,7 @@ export interface StreamRoute {
128127
server_addr?: string;
129128
server_port?: number;
130129
sni?: string;
131-
upstream?: ADCUpstream;
130+
upstream?: InlineUpstream;
132131
upstream_id?: string;
133132
service_id?: string;
134133

@@ -170,14 +169,15 @@ export interface Upstream {
170169
client_cert_id?: string;
171170
client_cert?: string;
172171
client_key?: string;
173-
verify: boolean;
172+
verify?: boolean;
174173
};
175174
keepalive_pool?: {
176175
size: number;
177176
idle_timeout: number;
178177
requests: number;
179178
};
180179
}
180+
export type InlineUpstream = Omit<Upstream, 'id'>;
181181

182182
export interface ListResponse<T> {
183183
list: Array<{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ToADC } from '../src/transformer';
2+
3+
describe('Transformer', () => {
4+
it('should transform upstream nodes to array', () => {
5+
const toADC = new ToADC();
6+
expect(
7+
toADC.transformUpstream({
8+
nodes: {
9+
'127.0.0.1:5432': 100,
10+
},
11+
}),
12+
).toEqual({ nodes: [{ host: '127.0.0.1', port: 5432, weight: 100 }] });
13+
});
14+
});

0 commit comments

Comments
 (0)