-
Notifications
You must be signed in to change notification settings - Fork 10
feat(apisix): separate inline upstream #354
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
base: main
Are you sure you want to change the base?
Conversation
d36ec42
to
b7d3d36
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please ensure that code is always formatted by Prettier and avoid making changes that serve no practical purpose.
this.subject = opts.eventSubject; | ||
} | ||
|
||
private operate(event: ADCSDK.Event) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that service is a special case, creating an operateService
function for it is a better choice. Let other resources continue using the simple operate
function, while services with specific upstream expansion logic use a separate function.
See the next comment actually only one function is enough.
private operate(event: ADCSDK.Event) {
// keep original codes
}
// handle only event for service
private operateService(event: ADCSDK.Event) {
const operateWithRetry = (op: () => Promise<AxiosResponse>) =>
defer(op).pipe(retry({ count: 3, delay: 100 /* or fn */ }));
const paths = ['/apisix/admin/upstreams/xxx', '/apisix/admin/services/xxx'];
const isUpdate = event.type !== ADCSDK.EventType.DELETE;
return from(isUpdate ? paths : paths.reverse()).pipe(
map(
(url) => () =>
this.client.request({
method: 'DELETE',
url,
...(isUpdate && {
method: 'PUT',
data: this.fromADC(event, this.opts.version),
}),
}),
),
concatMap(operateWithRetry),
);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Subsequently, it can be further simplified to a single operate
function.
private operate(event: ADCSDK.Event) {
const { type, resourceType, resourceId, parentId } = event;
const isUpdate = type !== ADCSDK.EventType.DELETE;
const PATH_PREFIX = '/apisix/admin';
const paths = [
`${PATH_PREFIX}/${
resourceType === ADCSDK.ResourceType.CONSUMER_CREDENTIAL
? `consumers/${parentId}/credentials/${resourceId}`
: `${resourceTypeToAPIName(resourceType)}/${resourceId}`
}`,
];
if (event.resourceType === ADCSDK.ResourceType.SERVICE) {
const path = `${PATH_PREFIX}/upstreams/${event.resourceId}`;
if (event.type === ADCSDK.EventType.DELETE)
paths.push(path); // services will be deleted before upstreams
else paths.unshift(path); // services will be created/updated after upstreams
}
const operateWithRetry = (op: () => Promise<AxiosResponse>) =>
defer(op).pipe(retry({ count: 3, delay: 100 /* or fn => timeout * 2 ** count */ }));
return from(paths).pipe(
map(
(path) => () =>
this.client.request({
method: 'DELETE',
url: path,
...(isUpdate && {
method: 'PUT',
data: this.fromADC(event, this.opts.version),
}),
}),
),
concatMap(operateWithRetry),
);
}
Description
Separate inline upstream in apisix backend as discussed in #319
Checklist