-
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
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),
);
}
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.
In fact, if a service is updated from having an inlined upstream to having no upstream, the EventType for the service is update
, while for the upstream it is delete
. I prefer to abstract the operations on the service separately.
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.
This is not allowed. The schema rejects such actions, and even if you disable configuration checks with --no-lint
, subsequent processes will not function correctly. All code operates under the assumption that upstream exists inline.
If the inline upstream does not exist (null or undefined), I suspect the ADC will crash. We do not guarantee that such destructive behavior will be ineffective. If you choose not to check, you must verify it yourself or bear the consequences of any failure.
Therefore, inline upstream changes caused by the service will follow the service itself. If the service is deleted, it will also be deleted; if the service remains in existence (created or updated), it will also remain.
What you need to do:
- If a service is deleted, cascade-delete its connected upstream resources.
- If a service is created, create an upstream and connect it.
- If a service is updated, check whether its inline upstreams have been updated. If so, update the split upstream resources. (You can find examples of how to derive this result from diffs in previous PRs.)
Description
Separate inline upstream in apisix backend as discussed in #319
Checklist