Skip to content

Commit

Permalink
feat(api7): support route vars expr (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored Dec 27, 2024
1 parent 71c26e2 commit fed76ee
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
if: contains(github.event.pull_request.labels.*.name, 'test/api7') || github.event_name == 'push'
strategy:
matrix:
version: [3.2.14.6, 3.2.15.2, 3.2.16.2]
version: [3.2.14.6, 3.2.15.2, 3.2.16.7, 3.3.2]
env:
BACKEND_API7_VERSION: ${{ matrix.version }}
BACKEND_API7_DOWNLOAD_URL: https://run.api7.ai/api7-ee/api7-ee-v${{ matrix.version }}.tar.gz
Expand Down
52 changes: 52 additions & 0 deletions libs/backend-api7/e2e/resources/route.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import * as ADCSDK from '@api7/adc-sdk';
import { gte } from 'semver';

import { BackendAPI7 } from '../../src';
import {
conditionalDescribe,
createEvent,
deleteEvent,
dumpConfiguration,
semverCondition,
syncEvents,
} from '../support/utils';

Expand Down Expand Up @@ -72,4 +75,53 @@ describe('Route E2E', () => {
expect(result.services).toHaveLength(0);
});
});

conditionalDescribe(semverCondition(gte, '3.2.16'))('Vars', () => {
const serviceName = 'test';
const service = {
name: serviceName,
upstream: {
scheme: 'https',
nodes: [
{
host: 'httpbin.org',
port: 443,
weight: 100,
},
],
},
path_prefix: '/test',
strip_path_prefix: true,
} as ADCSDK.Service;
const route1Name = 'route1';
const route1 = {
name: route1Name,
uris: ['/route1'],
vars: [['remote_addr', '==', '1.1.1.1']],
} as ADCSDK.Route;

it('Create resources', async () =>
syncEvents(backend, [
createEvent(ADCSDK.ResourceType.SERVICE, serviceName, service),
createEvent(ADCSDK.ResourceType.ROUTE, route1Name, route1, serviceName),
]));

it('Dump', async () => {
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
expect(result.services).toHaveLength(1);
expect(result.services[0]).toMatchObject(service);
expect(result.services[0].routes).toHaveLength(1);
expect(result.services[0].routes[0]).toMatchObject(route1);
});

it('Delete', async () =>
syncEvents(backend, [
deleteEvent(ADCSDK.ResourceType.SERVICE, serviceName),
]));

it('Dump again (service should not exist)', async () => {
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
expect(result.services).toHaveLength(0);
});
});
});
4 changes: 3 additions & 1 deletion libs/backend-api7/src/operator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as ADCSDK from '@api7/adc-sdk';
import { Axios, AxiosResponse } from 'axios';
import { ListrTask } from 'listr2';
import { size } from 'lodash';
import { size, unset } from 'lodash';
import { SemVer, gte as semVerGTE } from 'semver';

import { FromADC } from './transformer';
Expand Down Expand Up @@ -41,6 +41,8 @@ export class Operator {
task.output = buildReqAndRespDebugOutput(resp);
} else if (event.resourceType === ADCSDK.ResourceType.ROUTE) {
// Create a route template instead of create route directly
const route = this.fromADC(event);
if (!semVerGTE(ctx.api7Version, '3.2.16')) unset(route, 'vars');
resp = await this.client.put(
`/api/routes/template/${event.resourceId}`,
this.fromADC(event),
Expand Down
2 changes: 2 additions & 0 deletions libs/backend-api7/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class ToADC {
plugins: route.plugins,
priority: route.priority,
timeout: route.timeout,
vars: route.vars,
});
}

Expand Down Expand Up @@ -148,6 +149,7 @@ export class FromADC {
paths: route.uris,
priority: route.priority,
timeout: route.timeout,
vars: route.vars,
});
}

Expand Down
2 changes: 2 additions & 0 deletions libs/backend-api7/src/typing.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
PluginMetadata as ADCPluginMetadata,
Upstream as ADCUpstream,
Expr,
Labels,
Plugins,
UpstreamBalancer,
Expand All @@ -25,6 +26,7 @@ export interface Route {
// matcher
paths: Array<string>;
methods?: Array<string>;
vars: Expr;

// misc
enable_websocket?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export * from './resource';
export type Labels = Record<string, string | Array<string>>;
export type Plugin = Record<string, unknown>;
export type Plugins = Record<string, Plugin>;
export type Expr = Array<string | Array<Expr>>;
export type Expr = Array<unknown>;

export interface Route {
id?: string;
Expand Down

0 comments on commit fed76ee

Please sign in to comment.