Skip to content

Commit d753fdd

Browse files
authored
fix(apisix-standalone): upstream populate default empty nodes (#346)
1 parent 264da61 commit d753fdd

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed

libs/backend-apisix-standalone/e2e/resources/service.e2e-spec.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { DifferV3 } from '@api7/adc-differ';
1+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2+
import { Differ } from '@api7/adc-differ';
23
import * as ADCSDK from '@api7/adc-sdk';
34

45
import { BackendAPISIXStandalone } from '../../src';
6+
import { rawConfig as rawConfigCache } from '../../src/cache';
57
import { server1, token1 } from '../support/constants';
68
import {
79
createEvent,
@@ -11,6 +13,7 @@ import {
1113
syncEvents,
1214
} from '../support/utils';
1315

16+
const cacheKey = 'default';
1417
describe('Service E2E', () => {
1518
let backend: BackendAPISIXStandalone;
1619

@@ -19,7 +22,7 @@ describe('Service E2E', () => {
1922
backend = new BackendAPISIXStandalone({
2023
server: server1,
2124
token: token1,
22-
cacheKey: 'default',
25+
cacheKey,
2326
});
2427
});
2528

@@ -51,10 +54,7 @@ describe('Service E2E', () => {
5154
expect(dumpConfiguration(backend)).resolves.not.toThrow());
5255

5356
it('Create services', async () =>
54-
syncEvents(
55-
backend,
56-
DifferV3.diff({ services: [service1, service2] }, {}),
57-
));
57+
syncEvents(backend, Differ.diff({ services: [service1, service2] }, {})));
5858

5959
it('Dump', async () => {
6060
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
@@ -68,7 +68,7 @@ describe('Service E2E', () => {
6868
newService.description = 'desc';
6969
await syncEvents(
7070
backend,
71-
DifferV3.diff(
71+
Differ.diff(
7272
{ services: [newService, service2] },
7373
await dumpConfiguration(backend),
7474
),
@@ -171,4 +171,36 @@ describe('Service E2E', () => {
171171
expect(result.services).toHaveLength(0);
172172
});
173173
});
174+
175+
describe('Sync service with upstream service discovery', () => {
176+
const registryName = 'consul';
177+
const serviceName = 'svc-upstream-sd';
178+
const service: ADCSDK.Service = {
179+
name: serviceName,
180+
upstream: {
181+
type: 'roundrobin',
182+
discovery_type: registryName,
183+
service_name: serviceName,
184+
},
185+
};
186+
187+
it('Create service', async () =>
188+
syncEvents(
189+
backend,
190+
Differ.diff({ services: [service] }, await dumpConfiguration(backend)),
191+
));
192+
193+
it('Check raw cache', async () => {
194+
const rawCache = rawConfigCache.get(cacheKey);
195+
expect(rawCache!.upstreams).toHaveLength(1);
196+
197+
const upstream = rawCache!.upstreams![0];
198+
expect(upstream.nodes).toBeUndefined();
199+
expect(upstream.discovery_type).toBe(registryName);
200+
expect(upstream.service_name).toBe(serviceName);
201+
});
202+
203+
it('Delete service', async () =>
204+
syncEvents(backend, Differ.diff({}, await dumpConfiguration(backend))));
205+
});
174206
});

libs/backend-apisix-standalone/src/operator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ export class Operator extends ADCSDK.backend.BackendEventSource {
439439
type: res.type,
440440
hash_on: res.hash_on,
441441
key: res.key,
442-
nodes: res.nodes ?? [], // fix optional to required convert
442+
nodes: res.nodes,
443443
scheme: res.scheme,
444444
retries: res.retries,
445445
retry_timeout: res.retry_timeout,

libs/backend-apisix-standalone/src/typing.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,22 @@ const RouteSchema = z.strictObject({
2626
...Metadata,
2727
uris: z.array(z.string()).min(1),
2828
hosts: z.array(z.string()).min(1).optional(),
29-
methods: z.array(z.string()).optional(),
29+
methods: z
30+
.array(
31+
z.enum([
32+
'GET',
33+
'POST',
34+
'PUT',
35+
'DELETE',
36+
'PATCH',
37+
'HEAD',
38+
'OPTIONS',
39+
'CONNECT',
40+
'TRACE',
41+
'PURGE',
42+
]),
43+
)
44+
.optional(),
3045
remote_addrs: z.array(z.string()).min(1).optional(),
3146
vars: z.array(z.unknown()).optional(),
3247
filter_func: z.string().optional(),
@@ -65,15 +80,17 @@ const upstreamHealthCheckType = z
6580
const UpstreamSchema = z.strictObject({
6681
...ModifiedIndex,
6782
...Metadata,
68-
nodes: z.array(
69-
z.strictObject({
70-
host: z.string(),
71-
port: Port,
72-
weight: z.int(),
73-
priority: z.int().optional(),
74-
metadata: z.record(z.string(), z.unknown()).optional(),
75-
}),
76-
),
83+
nodes: z
84+
.array(
85+
z.strictObject({
86+
host: z.string(),
87+
port: Port,
88+
weight: z.int(),
89+
priority: z.int().optional(),
90+
metadata: z.record(z.string(), z.unknown()).optional(),
91+
}),
92+
)
93+
.optional(),
7794
scheme: z
7895
.union([
7996
z.literal('http'),

0 commit comments

Comments
 (0)