-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Move elasticsearch client creation/access API from core setup to start contract #55397
Comments
Pinging @elastic/kibana-platform (Team:Platform) |
I've audited all the x-pack plugins which use the Elasticsearch client from CoreSetup:
OSS usages: |
Several plugins construct Elasticsearch clients with custom plugins and then inject these into their routes by registering a route handler context. To refactor these to get their Elasticsearch client from start gets awkward: --- a/x-pack/plugins/snapshot_restore/server/plugin.ts
+++ b/x-pack/plugins/snapshot_restore/server/plugin.ts
@@ -72,11 +72,18 @@ export class SnapshotRestoreServerPlugin implements Plugin<void, void, any, any>
}
);
- const esClientConfig = { plugins: [elasticsearchJsPlugin] };
- const snapshotRestoreESClient = elasticsearch.createClient('snapshotRestore', esClientConfig);
+ const getClient = async () => {
+ const esClientConfig = { plugins: [elasticsearchJsPlugin] };
+ const coreStart = (await getStartServices())[0];
+ return coreStart.elasticsearch.createClient('snapshotRestore', esClientConfig);
+ };
+
http.registerRouteHandlerContext('snapshotRestore', (ctx, request) => {
return {
- client: snapshotRestoreESClient.asScoped(request),
+ client: {
+ callAsCurrentUser: async (...args) =>
+ (await getClient()).asScoped(request).callAsCurrentUser(...args),
+ callAsInternalUser: async (...args) =>
+ (await getClient()).asScoped(request).callAsInternalUser(...args),
+ },
};
}); Although it would require downstream changes, the other alternative is to expose the client as an async function: --- a/x-pack/plugins/snapshot_restore/server/plugin.ts
+++ b/x-pack/plugins/snapshot_restore/server/plugin.ts
@@ -72,11 +72,15 @@ export class SnapshotRestoreServerPlugin implements Plugin<void, void, any, any>
}
);
- const esClientConfig = { plugins: [elasticsearchJsPlugin] };
- const snapshotRestoreESClient = elasticsearch.createClient('snapshotRestore', esClientConfig);
+ const getClient = async () => {
+ const esClientConfig = { plugins: [elasticsearchJsPlugin] };
+ const coreStart = (await getStartServices())[0];
+ return coreStart.elasticsearch.createClient('snapshotRestore', esClientConfig);
+ };
+
http.registerRouteHandlerContext('snapshotRestore', (ctx, request) => {
return {
- client: snapshotRestoreESClient.asScoped(request),
+ getClient,
};
}); |
I know it breaks the convention of "register in setup, do things in start", but I think it would make sense if we exposed the WDYT? |
It would work as long as we start listening on a port after plugins register their const depsPromise = doAsync();
core.registerRequestHandlerContext(() => {
async getFoo() {
return (await depsPromise).getFoo()
}
})
|
Yeah we could say |
http.registerRouteHandlerContext('snapshotRestore',
async (context, req, res)=> {...} could become something like http.registerRouteHandlerContext('snapshotRestore', coreStart => {
const snapshotRestoreESClient = coreStart.elasticsearch.createClient('snapshotRestore', esClientConfig);
return async (context, req, res) => ({
client: snapshotRestoreESClient.asScoped(request),
}) This is more complicated to implement probably, but would keep the 'register during setup' convention. We could even replace the |
One thing I'd like to note is that the |
Yea, the implementation would requires some low-level refactoring with the contextService. However as @rudolf said on slack yesterday
So probably this |
Reporting
This validation should be moved away from the setup, as we are deprecating async lifecycles. Added a comment to their migration issue #53898 (comment) Licensing#67291 migrates to elasticsearch client to the provided from the start contract. SecurityCreates a custom elasticsearch plugin and provides access to it via public API. Switching to elasticsearch client exposed from Observability & APM#67263 migrates to the context-provided elasticsearch client and elasticsearch start. MonitoringProvides creates a custom es client and provides it to alerting plugin. Added a comment to the migration issue #33671 (comment) File upload#67277 switches to es client provided via request handler context. Telemetry#67279 an attempt to migrate to es client provided from |
Not all the API provided from the setup contract has been migrated. The list of leftovers #55397 (comment) |
In #55012 we decided to remove any API that could allow to access/query savedObjects from core setup contract, and move them to the start contract instead.
#55396 plans to do the same thing regarding UiSettings.
Steps:
legacy
prefix for API exposed fromsetup
Deprecate es API exposed from setup contract #67596context.elasticsearch.adminClient/dataClient
->context.elasticsearch.legacy.client
Mark elasticsearch client exposed via request context as deprecated #67319Use ES API from start contract #66157
The text was updated successfully, but these errors were encountered: