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
@@ -1,7 +1,9 @@
import { DifferV3 } from '@api7/adc-differ';
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { Differ } from '@api7/adc-differ';
import * as ADCSDK from '@api7/adc-sdk';

import { BackendAPISIXStandalone } from '../../src';
import { rawConfig as rawConfigCache } from '../../src/cache';
import { server1, token1 } from '../support/constants';
import {
createEvent,
Expand All @@ -11,6 +13,7 @@ import {
syncEvents,
} from '../support/utils';

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

Expand All @@ -19,7 +22,7 @@ describe('Service E2E', () => {
backend = new BackendAPISIXStandalone({
server: server1,
token: token1,
cacheKey: 'default',
cacheKey,
});
});

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

it('Create services', async () =>
syncEvents(
backend,
DifferV3.diff({ services: [service1, service2] }, {}),
));
syncEvents(backend, Differ.diff({ services: [service1, service2] }, {})));

it('Dump', async () => {
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
Expand All @@ -68,7 +68,7 @@ describe('Service E2E', () => {
newService.description = 'desc';
await syncEvents(
backend,
DifferV3.diff(
Differ.diff(
{ services: [newService, service2] },
await dumpConfiguration(backend),
),
Expand Down Expand Up @@ -171,4 +171,36 @@ describe('Service E2E', () => {
expect(result.services).toHaveLength(0);
});
});

describe('Sync service with upstream service discovery', () => {
const registryName = 'consul';
const serviceName = 'svc-upstream-sd';
const service: ADCSDK.Service = {
name: serviceName,
upstream: {
type: 'roundrobin',
discovery_type: registryName,
service_name: serviceName,
},
};

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

it('Check raw cache', async () => {
const rawCache = rawConfigCache.get(cacheKey);
expect(rawCache!.upstreams).toHaveLength(1);

const upstream = rawCache!.upstreams![0];
expect(upstream.nodes).toBeUndefined();
expect(upstream.discovery_type).toBe(registryName);
expect(upstream.service_name).toBe(serviceName);
});

it('Delete service', async () =>
syncEvents(backend, Differ.diff({}, await dumpConfiguration(backend))));
});
});
2 changes: 1 addition & 1 deletion libs/backend-apisix-standalone/src/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ export class Operator extends ADCSDK.backend.BackendEventSource {
type: res.type,
hash_on: res.hash_on,
key: res.key,
nodes: res.nodes ?? [], // fix optional to required convert
nodes: res.nodes,
scheme: res.scheme,
retries: res.retries,
retry_timeout: res.retry_timeout,
Expand Down
37 changes: 27 additions & 10 deletions libs/backend-apisix-standalone/src/typing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,22 @@ const RouteSchema = z.strictObject({
...Metadata,
uris: z.array(z.string()).min(1),
hosts: z.array(z.string()).min(1).optional(),
methods: z.array(z.string()).optional(),
methods: z
.array(
z.enum([
'GET',
'POST',
'PUT',
'DELETE',
'PATCH',
'HEAD',
'OPTIONS',
'CONNECT',
'TRACE',
'PURGE',
]),
)
.optional(),
remote_addrs: z.array(z.string()).min(1).optional(),
vars: z.array(z.unknown()).optional(),
filter_func: z.string().optional(),
Expand Down Expand Up @@ -65,15 +80,17 @@ const upstreamHealthCheckType = z
const UpstreamSchema = z.strictObject({
...ModifiedIndex,
...Metadata,
nodes: z.array(
z.strictObject({
host: z.string(),
port: Port,
weight: z.int(),
priority: z.int().optional(),
metadata: z.record(z.string(), z.unknown()).optional(),
}),
),
nodes: z
.array(
z.strictObject({
host: z.string(),
port: Port,
weight: z.int(),
priority: z.int().optional(),
metadata: z.record(z.string(), z.unknown()).optional(),
}),
)
.optional(),
scheme: z
.union([
z.literal('http'),
Expand Down
Loading