Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apisix): upstream nodes should be allowed to be null #222

Merged
merged 4 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 1 addition & 2 deletions libs/backend-apisix/e2e/resources/consumer.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as ADCSDK from '@api7/adc-sdk';
import { gte, lt } from 'semver';
import { gte } from 'semver';

import { BackendAPISIX } from '../../src';
import { server, token } from '../support/constants';
Expand Down Expand Up @@ -96,7 +96,6 @@ describe('Consumer E2E', () => {
backend,
)) as ADCSDK.Configuration;
expect(result.consumers).toHaveLength(1);
console.log(result.consumers[0]);
expect(result.consumers[0].credentials).toBeUndefined();
});

Expand Down
89 changes: 89 additions & 0 deletions libs/backend-apisix/e2e/resources/upstream.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import * as ADCSDK from '@api7/adc-sdk';
import { gte, lt } from 'semver';

import { BackendAPISIX } from '../../src';
import { server, token } from '../support/constants';
import { conditionalDescribe, semverCondition } from '../support/utils';
import {
createEvent,
deleteEvent,
dumpConfiguration,
syncEvents,
updateEvent,
} from '../support/utils';

describe('Consumer E2E', () => {
let backend: BackendAPISIX;

beforeAll(() => {
backend = new BackendAPISIX({
server,
token,
tlsSkipVerify: true,
});
});

describe('Sync and dump upstream (nodes = null)', () => {
const upstream = {
scheme: 'https',
discovery_type: 'kubernetes',
service_name: 'test',
} as ADCSDK.Upstream;
const service1Name = 'service1';
const service1 = {
name: service1Name,
upstream: structuredClone(upstream),
} as ADCSDK.Service;
const service2Name = 'service2';
const service2 = {
name: service2Name,
upstream: structuredClone(upstream),
} as ADCSDK.Service;

it('Create services', async () =>
syncEvents(backend, [
createEvent(ADCSDK.ResourceType.SERVICE, service1Name, service1),
createEvent(ADCSDK.ResourceType.SERVICE, service2Name, service2),
]));

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

it('Update service1', async () => {
service1.description = 'desc';
await syncEvents(backend, [
updateEvent(ADCSDK.ResourceType.SERVICE, service1Name, service1),
]);
});

it('Dump again (service1 updated)', async () => {
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
expect(result.services[1]).toMatchObject(service1);
});

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

it('Dump again (service1 should not exist)', async () => {
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
expect(result.services).toHaveLength(1);
expect(result.services[0]).toMatchObject(service2);
});

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

it('Dump again (service2 should not exist)', async () => {
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
expect(result.services).toHaveLength(0);
});
});
});
32 changes: 17 additions & 15 deletions libs/backend-apisix/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,23 @@ export class ToADC {
grpc: 80,
grpcs: 443,
};
const nodes = Array.isArray(upstream.nodes)
? upstream.nodes
: Object.keys(upstream.nodes).map<ADCSDK.UpstreamNode>((node) => {
const hostport = node.split(':');
return {
host: hostport[0],
port:
hostport.length === 2
? parseInt(hostport[1])
: defaultPortMap[upstream.scheme]
? defaultPortMap[upstream.scheme]
: 80,
weight: upstream.nodes[node],
};
});
const nodes = upstream.nodes
? Array.isArray(upstream.nodes)
? upstream.nodes
: Object.keys(upstream.nodes).map<ADCSDK.UpstreamNode>((node) => {
const hostport = node.split(':');
return {
host: hostport[0],
port:
hostport.length === 2
? parseInt(hostport[1])
: defaultPortMap[upstream.scheme]
? defaultPortMap[upstream.scheme]
: 80,
weight: upstream.nodes[node],
};
})
: undefined;
return ADCSDK.utils.recursiveOmitUndefined({
name: upstream.name ?? upstream.id,
description: upstream.desc,
Expand Down
Loading