Skip to content

Commit aa53fab

Browse files
authored
fix(apisix): upstream nodes should be allowed to be null (#222)
1 parent fe3bc49 commit aa53fab

File tree

3 files changed

+74
-17
lines changed

3 files changed

+74
-17
lines changed

libs/backend-apisix/e2e/resources/consumer.e2e-spec.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ADCSDK from '@api7/adc-sdk';
2-
import { gte, lt } from 'semver';
2+
import { gte } from 'semver';
33

44
import { BackendAPISIX } from '../../src';
55
import { server, token } from '../support/constants';
@@ -96,7 +96,6 @@ describe('Consumer E2E', () => {
9696
backend,
9797
)) as ADCSDK.Configuration;
9898
expect(result.consumers).toHaveLength(1);
99-
console.log(result.consumers[0]);
10099
expect(result.consumers[0].credentials).toBeUndefined();
101100
});
102101

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as ADCSDK from '@api7/adc-sdk';
2+
3+
import { BackendAPISIX } from '../../src';
4+
import { server, token } from '../support/constants';
5+
import {
6+
createEvent,
7+
deleteEvent,
8+
dumpConfiguration,
9+
syncEvents,
10+
} from '../support/utils';
11+
12+
describe('Upstream E2E', () => {
13+
let backend: BackendAPISIX;
14+
15+
beforeAll(() => {
16+
backend = new BackendAPISIX({
17+
server,
18+
token,
19+
tlsSkipVerify: true,
20+
});
21+
});
22+
23+
describe('Sync and dump upstream (nodes = null)', () => {
24+
const upstream = {
25+
scheme: 'https',
26+
discovery_type: 'kubernetes',
27+
service_name: 'test',
28+
} as ADCSDK.Upstream;
29+
const serviceName = 'service1';
30+
const service = {
31+
name: serviceName,
32+
upstream: structuredClone(upstream),
33+
} as ADCSDK.Service;
34+
35+
it('Create services', async () =>
36+
syncEvents(backend, [
37+
createEvent(ADCSDK.ResourceType.SERVICE, serviceName, service),
38+
]));
39+
40+
it('Dump', async () => {
41+
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
42+
expect(result.services).toHaveLength(1);
43+
expect(result.services[0]).toMatchObject(service);
44+
});
45+
46+
it('Delete service', async () =>
47+
syncEvents(backend, [
48+
deleteEvent(ADCSDK.ResourceType.SERVICE, serviceName),
49+
]));
50+
51+
it('Dump again', async () => {
52+
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
53+
expect(result.services).toHaveLength(0);
54+
});
55+
});
56+
});

libs/backend-apisix/src/transformer.ts

+17-15
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,23 @@ export class ToADC {
179179
grpc: 80,
180180
grpcs: 443,
181181
};
182-
const nodes = Array.isArray(upstream.nodes)
183-
? upstream.nodes
184-
: Object.keys(upstream.nodes).map<ADCSDK.UpstreamNode>((node) => {
185-
const hostport = node.split(':');
186-
return {
187-
host: hostport[0],
188-
port:
189-
hostport.length === 2
190-
? parseInt(hostport[1])
191-
: defaultPortMap[upstream.scheme]
192-
? defaultPortMap[upstream.scheme]
193-
: 80,
194-
weight: upstream.nodes[node],
195-
};
196-
});
182+
const nodes = upstream.nodes
183+
? Array.isArray(upstream.nodes)
184+
? upstream.nodes
185+
: Object.keys(upstream.nodes).map<ADCSDK.UpstreamNode>((node) => {
186+
const hostport = node.split(':');
187+
return {
188+
host: hostport[0],
189+
port:
190+
hostport.length === 2
191+
? parseInt(hostport[1])
192+
: defaultPortMap[upstream.scheme]
193+
? defaultPortMap[upstream.scheme]
194+
: 80,
195+
weight: upstream.nodes[node],
196+
};
197+
})
198+
: undefined;
197199
return ADCSDK.utils.recursiveOmitUndefined({
198200
name: upstream.name ?? upstream.id,
199201
description: upstream.desc,

0 commit comments

Comments
 (0)