Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import { Differ } from '@api7/adc-differ';
import * as ADCSDK from '@api7/adc-sdk';

import { BackendAPISIXStandalone } from '../../src';
import {
config as configCache,
rawConfig as rawConfigCache,
} from '../../src/cache';
import * as typing from '../../src/typing';
import { server1, token1 } from '../support/constants';
import {
createEvent,
deleteEvent,
dumpConfiguration,
restartAPISIX,
syncEvents,
updateEvent,
} from '../support/utils';

const cacheKey = 'default';
describe('Service-Upstreams E2E', () => {
let backend: BackendAPISIXStandalone;

beforeAll(async () => {
await restartAPISIX();
backend = new BackendAPISIXStandalone({
server: server1,
token: token1,
tlsSkipVerify: true,
cacheKey,
});
});

describe('Sync and dump service with multiple upstreams', () => {
const upstreamND1Name = 'nd-upstream1';
const upstreamND1 = {
name: upstreamND1Name,
type: 'roundrobin',
scheme: 'https',
nodes: [
{
host: '1.1.1.1',
port: 443,
weight: 100,
},
],
} satisfies ADCSDK.Upstream;
const upstreamND2Name = 'nd-upstream2';
const upstreamND2 = {
//@ts-expect-error custom id
id: upstreamND2Name,
name: upstreamND2Name,
type: 'roundrobin',
scheme: 'https',
nodes: [
{
host: '1.0.0.1',
port: 443,
weight: 100,
},
],
} satisfies ADCSDK.Upstream;
const serviceName = 'test';
const service = {
name: serviceName,
upstream: {
type: 'roundrobin',
nodes: [
{
host: 'httpbin.org',
port: 443,
weight: 100,
},
],
},
upstreams: [upstreamND1, upstreamND2],
} satisfies ADCSDK.Service;

it('Initialize cache', () =>
expect(dumpConfiguration(backend)).resolves.not.toThrow());

it('Create', async () =>
syncEvents(
backend,
Differ.diff(
{
services: [service],
},
await dumpConfiguration(backend),
),
));

const checkOriginalConfig = () => {
const rawConfig = rawConfigCache.get(cacheKey);
expect(rawConfig?.services?.[0].id).toEqual(
ADCSDK.utils.generateId(serviceName),
);
expect(rawConfig?.upstreams).not.toBeUndefined();
expect(rawConfig?.upstreams).toHaveLength(3);
expect(rawConfig?.upstreams?.[0].name).toEqual(serviceName);
expect(rawConfig?.upstreams?.[1].name).toEqual(upstreamND1Name);
expect(rawConfig?.upstreams?.[2].name).toEqual(upstreamND2Name);
expect(rawConfig?.upstreams?.[0].labels).toBeUndefined();
expect(
rawConfig?.upstreams?.[1].labels?.[
typing.ADC_UPSTREAM_SERVICE_ID_LABEL
],
).toEqual(ADCSDK.utils.generateId(serviceName));
expect(
rawConfig?.upstreams?.[2].labels?.[
typing.ADC_UPSTREAM_SERVICE_ID_LABEL
],
).toEqual(ADCSDK.utils.generateId(serviceName));

const config = configCache.get(cacheKey);
expect(config?.services).not.toBeUndefined();
expect(config?.services).toHaveLength(1);
expect(config?.services?.[0].upstreams).toHaveLength(2);
expect(
config?.services?.[0].upstreams?.[0].labels?.[
typing.ADC_UPSTREAM_SERVICE_ID_LABEL
],
).toBeUndefined();
expect(
config?.services?.[0].upstreams?.[1].labels?.[
typing.ADC_UPSTREAM_SERVICE_ID_LABEL
],
).toBeUndefined();
};
it('Check cache', checkOriginalConfig);

it('Try update (without any change)', async () =>
syncEvents(
backend,
Differ.diff(
{
services: [service],
},
await dumpConfiguration(backend),
),
));

it('Check cache 2', checkOriginalConfig);

it('Try update', async () => {
const newService = structuredClone(service);
newService.upstreams[0].nodes[0].host = '8.8.8.8';
await syncEvents(
backend,
Differ.diff(
{
services: [newService],
},
await dumpConfiguration(backend),
),
);
});

it('Check updated cache', () => {
const rawConfig = rawConfigCache.get(cacheKey);
expect(rawConfig?.services?.[0].id).toEqual(
ADCSDK.utils.generateId(serviceName),
);
expect(rawConfig?.upstreams).not.toBeUndefined();
expect(rawConfig?.upstreams).toHaveLength(3);
expect(
rawConfig?.upstreams?.[1].labels?.[
typing.ADC_UPSTREAM_SERVICE_ID_LABEL
],
).toEqual(ADCSDK.utils.generateId(serviceName));
expect(rawConfig?.upstreams?.[1].nodes[0].host).toEqual('8.8.8.8');

const config = configCache.get(cacheKey);
expect(config?.services).not.toBeUndefined();
expect(config?.services).toHaveLength(1);
expect(config?.services?.[0].upstreams).toHaveLength(2);
expect(
config?.services?.[0].upstreams?.[0].labels?.[
typing.ADC_UPSTREAM_SERVICE_ID_LABEL
],
).toBeUndefined();
expect(config?.services?.[0].upstreams?.[0].nodes?.[0].host).toEqual(
'8.8.8.8',
);
});
});
});
15 changes: 12 additions & 3 deletions libs/backend-apisix-standalone/src/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as ADCSDK from '@api7/adc-sdk';
import { isEmpty } from 'lodash';
import { cloneDeep, isEmpty, unset } from 'lodash';

import * as typing from './typing';

Expand All @@ -12,7 +12,7 @@ export const toADC = (input: typing.APISIXStandalone) => {
upstream: Omit<typing.Upstream, 'id' | 'name' | 'modifiedIndex'> & {
name?: string;
},
) => ({
): ADCSDK.Upstream => ({
name: upstream.name,
description: upstream.desc,
labels: upstream.labels,
Expand Down Expand Up @@ -96,7 +96,16 @@ export const toADC = (input: typing.APISIXStandalone) => {
upstream.labels?.[typing.ADC_UPSTREAM_SERVICE_ID_LABEL] ===
service.id,
)
.map(transformUpstream)
.map((upstream) => {
const up = transformUpstream(
cloneDeep(upstream),
) as ADCSDK.Upstream & {
id: string;
};
up.id = upstream.id;
unset(up, `labels.${typing.ADC_UPSTREAM_SERVICE_ID_LABEL}`);
return up;
})
.map(ADCSDK.utils.recursiveOmitUndefined),
}))
.map(ADCSDK.utils.recursiveOmitUndefined) ?? [],
Expand Down
2 changes: 1 addition & 1 deletion libs/differ/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { DifferV3 } from './differv3.js';
export { DifferV3, DifferV3 as Differ } from './differv3.js';
Loading