Skip to content

Commit 3a2aeca

Browse files
authored
feat(apisix-standalone): use one of servers when no XLM header (#330)
1 parent 3fe939d commit 3a2aeca

File tree

4 files changed

+46
-32
lines changed

4 files changed

+46
-32
lines changed

libs/backend-apisix-standalone/e2e/cache.e2e-spec.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,16 @@ describe('Cache - Single APISIX', () => {
5858
expect(rawConfigCache.size).toEqual(1);
5959
// APISIX does not report when it was last updated (it has not yet gotten configured),
6060
// so fetcher will automatically populate empty objects as a default.
61-
expect(configCache.get(cacheKey)).toEqual({});
62-
expect(rawConfigCache.get(cacheKey)).toEqual({});
61+
expect(configCache.get(cacheKey)).toMatchObject({
62+
services: [],
63+
ssls: [],
64+
consumers: [],
65+
global_rules: {},
66+
plugin_metadata: {},
67+
});
68+
Object.values(
69+
rawConfigCache.get(cacheKey) as Record<string, unknown>,
70+
).forEach((item) => expect(item).toEqual(0));
6371
});
6472

6573
it('dump again (should use cache)', async () => {
@@ -207,8 +215,16 @@ describe('Cache - Multiple APISIX (completely new instances)', () => {
207215
expect(rawConfigCache.size).toEqual(1);
208216
// APISIX does not report when it was last updated (it has not yet gotten configured),
209217
// so fetcher will automatically populate empty objects as a default.
210-
expect(configCache.get(cacheKey)).toEqual({});
211-
expect(rawConfigCache.get(cacheKey)).toEqual({});
218+
expect(configCache.get(cacheKey)).toMatchObject({
219+
services: [],
220+
ssls: [],
221+
consumers: [],
222+
global_rules: {},
223+
plugin_metadata: {},
224+
});
225+
Object.values(
226+
rawConfigCache.get(cacheKey) as Record<string, unknown>,
227+
).forEach((item) => expect(item).toEqual(0));
212228
});
213229

214230
const config = {
@@ -388,10 +404,6 @@ describe('Cache - Multiple APISIX (Partial new instances)', () => {
388404

389405
expect(configCache.size).toEqual(1);
390406
expect(rawConfigCache.size).toEqual(1);
391-
// For versions where last modified is not available (e.g. less than or equal to 3.13.0), the cache cannot be initialized.
392-
// The cache will be empty so that the ADC will perform a full synchronization.
393-
expect(configCache.get(cacheKey)).toEqual({});
394-
expect(rawConfigCache.get(cacheKey)).toEqual({});
395407
},
396408
);
397409

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,11 @@ describe('Service E2E - inline upstream', () => {
6666
expect(rawConfig?.upstreams?.[0].modifiedIndex).toEqual(100);
6767
expect(rawConfig?.services_conf_version).toEqual(100);
6868
expect(rawConfig?.upstreams_conf_version).toEqual(100);
69-
expect(rawConfig?.consumers_conf_version).toBeUndefined();
70-
expect(rawConfig?.global_rules_conf_version).toBeUndefined();
71-
expect(rawConfig?.plugin_metadata_conf_version).toBeUndefined();
72-
expect(rawConfig?.routes_conf_version).toBeUndefined();
73-
expect(rawConfig?.ssls_conf_version).toBeUndefined();
74-
expect(rawConfig?.stream_routes_conf_version).toBeUndefined();
69+
expect(rawConfig?.consumers_conf_version).toEqual(0);
70+
expect(rawConfig?.global_rules_conf_version).toEqual(0);
71+
expect(rawConfig?.plugin_metadata_conf_version).toEqual(0);
72+
expect(rawConfig?.routes_conf_version).toEqual(0);
73+
expect(rawConfig?.ssls_conf_version).toEqual(0);
7574

7675
const config = configCache.get(cacheKey);
7776
expect(config?.services).not.toBeUndefined();
@@ -100,12 +99,11 @@ describe('Service E2E - inline upstream', () => {
10099
expect(rawConfig?.services?.[0].modifiedIndex).toEqual(100);
101100
expect(rawConfig?.upstreams_conf_version).toEqual(200);
102101
expect(rawConfig?.services_conf_version).toEqual(100);
103-
expect(rawConfig?.consumers_conf_version).toBeUndefined();
104-
expect(rawConfig?.global_rules_conf_version).toBeUndefined();
105-
expect(rawConfig?.plugin_metadata_conf_version).toBeUndefined();
106-
expect(rawConfig?.routes_conf_version).toBeUndefined();
107-
expect(rawConfig?.ssls_conf_version).toBeUndefined();
108-
expect(rawConfig?.stream_routes_conf_version).toBeUndefined();
102+
expect(rawConfig?.consumers_conf_version).toEqual(0);
103+
expect(rawConfig?.global_rules_conf_version).toEqual(0);
104+
expect(rawConfig?.plugin_metadata_conf_version).toEqual(0);
105+
expect(rawConfig?.routes_conf_version).toEqual(0);
106+
expect(rawConfig?.ssls_conf_version).toEqual(0);
109107
});
110108

111109
it('Update inlined upstream again', async () => {
@@ -131,12 +129,11 @@ describe('Service E2E - inline upstream', () => {
131129
expect(rawConfig?.services?.[0].modifiedIndex).toEqual(100);
132130
expect(rawConfig?.upstreams_conf_version).toEqual(300);
133131
expect(rawConfig?.services_conf_version).toEqual(100);
134-
expect(rawConfig?.consumers_conf_version).toBeUndefined();
135-
expect(rawConfig?.global_rules_conf_version).toBeUndefined();
136-
expect(rawConfig?.plugin_metadata_conf_version).toBeUndefined();
137-
expect(rawConfig?.routes_conf_version).toBeUndefined();
138-
expect(rawConfig?.ssls_conf_version).toBeUndefined();
139-
expect(rawConfig?.stream_routes_conf_version).toBeUndefined();
132+
expect(rawConfig?.consumers_conf_version).toEqual(0);
133+
expect(rawConfig?.global_rules_conf_version).toEqual(0);
134+
expect(rawConfig?.plugin_metadata_conf_version).toEqual(0);
135+
expect(rawConfig?.routes_conf_version).toEqual(0);
136+
expect(rawConfig?.ssls_conf_version).toEqual(0);
140137
});
141138

142139
it('Delete service', async () => {

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
map,
88
max,
99
mergeMap,
10-
of,
1110
switchMap,
1211
tap,
1312
} from 'rxjs';
@@ -43,18 +42,22 @@ export class Fetcher extends ADCSDK.backend.BackendEventSource {
4342
logger(taskStateEvent('TASK_START'));
4443
return from(this.findLatest()).pipe(
4544
switchMap((server) => {
46-
if (!server) return of([{}, {}] as result);
45+
if (!server) server = this.opts.serverTokenMap.keys().next().value;
4746
return from(
4847
this.opts.client.get<typing.APISIXStandalone>(
4948
`${server}${ENDPOINT_CONFIG}`,
5049
{
5150
headers: {
52-
[HEADER_CREDENTIAL]: this.opts.serverTokenMap.get(server),
51+
[HEADER_CREDENTIAL]: this.opts.serverTokenMap.get(
52+
server as string,
53+
),
5354
},
5455
},
5556
),
5657
).pipe(
57-
tap((resp) => logger(this.debugLogEvent(resp))),
58+
tap((resp) =>
59+
logger(this.debugLogEvent(resp, 'Initialize configuration cache')),
60+
),
5861
map((resp) => [toADC(resp.data), resp.data] as result),
5962
);
6063
}),
@@ -78,7 +81,9 @@ export class Fetcher extends ADCSDK.backend.BackendEventSource {
7881
headers: { [HEADER_CREDENTIAL]: token },
7982
}),
8083
).pipe(
81-
tap((resp) => logger(this.debugLogEvent(resp))),
84+
tap((resp) =>
85+
logger(this.debugLogEvent(resp, 'Get latest updated instance')),
86+
),
8287
map((res) => ({
8388
server,
8489
timestamp: parseInt(res.headers[HEADER_LAST_MODIFIED] ?? 0),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export class BackendAPISIXStandalone implements ADCSDK.Backend {
170170
serverTokenMap: this.serverTokenMap,
171171
version,
172172
eventSubject: this.subject,
173-
oldRawConfiguration: rawConfigCache.get(this.opts.cacheKey)!,
173+
oldRawConfiguration: rawConfigCache.get(this.opts.cacheKey) ?? {},
174174
}).sync(events, opts);
175175
}),
176176
);

0 commit comments

Comments
 (0)