Skip to content

Commit

Permalink
Migrate server-side ES domain to packages (#136297)
Browse files Browse the repository at this point in the history
* create es types package

* start moving client types to @kbn/core-elasticsearch-server

* move ElasticsearchClientConfig to package

* start adapting usages

* start fixing imports

* fix more imports

* just a bit more

* move service types

* [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix'

* fix more internal types

* move retry call cluster helpers outside of client package

* move client code and mocks to packages

* fix imports

* adapt external usages

* adapt more external usages

* adapt more external usages 2

* fix mocked module

* create empty domain packages

* more external usages fix

* move ALL the things (again)

* mock external import fix

* fix tests, add test dependencies

* fix some internal usages

* fix more internal usages

* [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix'

* fix cli_setup usage

* desperate times force desperate decisions

* fix misc stuff

* update snapshots (?!)

* fix mocked package

* self review

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
pgayvallet and kibanamachine committed Jul 20, 2022
1 parent 42f2bc2 commit 3508350
Show file tree
Hide file tree
Showing 274 changed files with 1,853 additions and 705 deletions.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@
"@kbn/core-doc-links-server": "link:bazel-bin/packages/core/doc-links/core-doc-links-server",
"@kbn/core-doc-links-server-internal": "link:bazel-bin/packages/core/doc-links/core-doc-links-server-internal",
"@kbn/core-doc-links-server-mocks": "link:bazel-bin/packages/core/doc-links/core-doc-links-server-mocks",
"@kbn/core-elasticsearch-client-server": "link:bazel-bin/packages/core/elasticsearch/core-elasticsearch-client-server",
"@kbn/core-elasticsearch-client-server-internal": "link:bazel-bin/packages/core/elasticsearch/core-elasticsearch-client-server-internal",
"@kbn/core-elasticsearch-client-server-mocks": "link:bazel-bin/packages/core/elasticsearch/core-elasticsearch-client-server-mocks",
"@kbn/core-elasticsearch-server": "link:bazel-bin/packages/core/elasticsearch/core-elasticsearch-server",
"@kbn/core-elasticsearch-server-internal": "link:bazel-bin/packages/core/elasticsearch/core-elasticsearch-server-internal",
"@kbn/core-elasticsearch-server-mocks": "link:bazel-bin/packages/core/elasticsearch/core-elasticsearch-server-mocks",
"@kbn/core-environment-server-internal": "link:bazel-bin/packages/core/environment/core-environment-server-internal",
"@kbn/core-environment-server-mocks": "link:bazel-bin/packages/core/environment/core-environment-server-mocks",
"@kbn/core-execution-context-browser": "link:bazel-bin/packages/core/execution-context/core-execution-context-browser",
Expand Down Expand Up @@ -757,6 +763,12 @@
"@types/kbn__core-doc-links-server": "link:bazel-bin/packages/core/doc-links/core-doc-links-server/npm_module_types",
"@types/kbn__core-doc-links-server-internal": "link:bazel-bin/packages/core/doc-links/core-doc-links-server-internal/npm_module_types",
"@types/kbn__core-doc-links-server-mocks": "link:bazel-bin/packages/core/doc-links/core-doc-links-server-mocks/npm_module_types",
"@types/kbn__core-elasticsearch-client-server": "link:bazel-bin/packages/core/elasticsearch/core-elasticsearch-client-server/npm_module_types",
"@types/kbn__core-elasticsearch-client-server-internal": "link:bazel-bin/packages/core/elasticsearch/core-elasticsearch-client-server-internal/npm_module_types",
"@types/kbn__core-elasticsearch-client-server-mocks": "link:bazel-bin/packages/core/elasticsearch/core-elasticsearch-client-server-mocks/npm_module_types",
"@types/kbn__core-elasticsearch-server": "link:bazel-bin/packages/core/elasticsearch/core-elasticsearch-server/npm_module_types",
"@types/kbn__core-elasticsearch-server-internal": "link:bazel-bin/packages/core/elasticsearch/core-elasticsearch-server-internal/npm_module_types",
"@types/kbn__core-elasticsearch-server-mocks": "link:bazel-bin/packages/core/elasticsearch/core-elasticsearch-server-mocks/npm_module_types",
"@types/kbn__core-environment-server-internal": "link:bazel-bin/packages/core/environment/core-environment-server-internal/npm_module_types",
"@types/kbn__core-environment-server-mocks": "link:bazel-bin/packages/core/environment/core-environment-server-mocks/npm_module_types",
"@types/kbn__core-execution-context-browser": "link:bazel-bin/packages/core/execution-context/core-execution-context-browser/npm_module_types",
Expand Down
10 changes: 10 additions & 0 deletions packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ filegroup(
"//packages/core/doc-links/core-doc-links-server-internal:build",
"//packages/core/doc-links/core-doc-links-server-mocks:build",
"//packages/core/doc-links/core-doc-links-server:build",
"//packages/core/elasticsearch/core-elasticsearch-client-server-internal:build",
"//packages/core/elasticsearch/core-elasticsearch-client-server-mocks:build",
"//packages/core/elasticsearch/core-elasticsearch-server-internal:build",
"//packages/core/elasticsearch/core-elasticsearch-server-mocks:build",
"//packages/core/elasticsearch/core-elasticsearch-server:build",
"//packages/core/environment/core-environment-server-internal:build",
"//packages/core/environment/core-environment-server-mocks:build",
"//packages/core/execution-context/core-execution-context-browser-internal:build",
Expand Down Expand Up @@ -248,6 +253,11 @@ filegroup(
"//packages/core/doc-links/core-doc-links-server-internal:build_types",
"//packages/core/doc-links/core-doc-links-server-mocks:build_types",
"//packages/core/doc-links/core-doc-links-server:build_types",
"//packages/core/elasticsearch/core-elasticsearch-client-server-internal:build_types",
"//packages/core/elasticsearch/core-elasticsearch-client-server-mocks:build_types",
"//packages/core/elasticsearch/core-elasticsearch-server-internal:build_types",
"//packages/core/elasticsearch/core-elasticsearch-server-mocks:build_types",
"//packages/core/elasticsearch/core-elasticsearch-server:build_types",
"//packages/core/environment/core-environment-server-internal:build_types",
"//packages/core/environment/core-environment-server-mocks:build_types",
"//packages/core/execution-context/core-execution-context-browser-internal:build_types",
Expand Down
2 changes: 2 additions & 0 deletions packages/core/base/core-base-common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ NPM_MODULE_EXTRA_FILES = [
]

RUNTIME_DEPS = [
"//packages/kbn-std",
]

TYPES_DEPS = [
"@npm//@types/node",
"@npm//@types/jest",
"//packages/kbn-std:npm_module_types",
"//packages/kbn-config:npm_module_types",
]

Expand Down
2 changes: 2 additions & 0 deletions packages/core/base/core-base-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
export type { PluginOpaqueId, PluginName, DiscoveredPlugin } from './plugins';
export { PluginType } from './plugins';
export { EUI_STYLES_GLOBAL } from './eui';
export { ServiceStatusLevels } from './service_status';
export type { ServiceStatus, ServiceStatusLevel } from './service_status';
101 changes: 101 additions & 0 deletions packages/core/base/core-base-common/src/service_status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { deepFreeze } from '@kbn/std';

/**
* The current status of a service at a point in time.
*
* @typeParam Meta - JSON-serializable object. Plugins should export this type to allow other plugins to read the `meta`
* field in a type-safe way.
* @public
*/
export interface ServiceStatus<Meta extends Record<string, any> | unknown = unknown> {
/**
* The current availability level of the service.
*/
level: ServiceStatusLevel;
/**
* A high-level summary of the service status.
*/
summary: string;
/**
* A more detailed description of the service status.
*/
detail?: string;
/**
* A URL to open in a new tab about how to resolve or troubleshoot the problem.
*/
documentationUrl?: string;
/**
* Any JSON-serializable data to be included in the HTTP API response. Useful for providing more fine-grained,
* machine-readable information about the service status. May include status information for underlying features.
*/
meta?: Meta;
}

/**
* The current "level" of availability of a service.
*
* @remarks
* The values implement `valueOf` to allow for easy comparisons between status levels with <, >, etc. Higher values
* represent higher severities. Note that the default `Array.prototype.sort` implementation does not correctly sort
* these values.
*
* A snapshot serializer is available in `src/core/server/test_utils` to ease testing of these values with Jest.
*
* @public
*/
export const ServiceStatusLevels = deepFreeze({
/**
* Everything is working!
*/
available: {
toString: () => 'available',
valueOf: () => 0,
toJSON() {
return this.toString();
},
},
/**
* Some features may not be working.
*/
degraded: {
toString: () => 'degraded',
valueOf: () => 1,
toJSON() {
return this.toString();
},
},
/**
* The service is unavailable, but other functions that do not depend on this service should work.
*/
unavailable: {
toString: () => 'unavailable',
valueOf: () => 2,
toJSON() {
return this.toString();
},
},
/**
* Block all user functions and display the status page, reserved for Core services only.
*/
critical: {
toString: () => 'critical',
valueOf: () => 3,
toJSON() {
return this.toString();
},
},
});

/**
* A convenience type that represents the union of each value in {@link ServiceStatusLevels}.
* @public
*/
export type ServiceStatusLevel = typeof ServiceStatusLevels[keyof typeof ServiceStatusLevels];
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
load("@npm//@bazel/typescript:index.bzl", "ts_config")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm", "pkg_npm_types", "ts_project")

PKG_DIRNAME = "core-elasticsearch-client-server-internal"
PKG_REQUIRE_NAME = "@kbn/core-elasticsearch-client-server-internal"

SOURCE_FILES = glob(
[
"src/**/*.ts",
],
exclude = [
"**/*.test.*",
"**/*.stories.*",
],
)

SRCS = SOURCE_FILES

filegroup(
name = "srcs",
srcs = SRCS,
)

NPM_MODULE_EXTRA_FILES = [
"package.json",
]

RUNTIME_DEPS = [
"@npm//moment",
"@npm//@elastic/elasticsearch",
"@npm//@elastic/numeral",
"//packages/kbn-std",
"//packages/kbn-es-errors",
"//packages/core/http/core-http-router-server-internal",
### test dependencies
"//packages/core/logging/core-logging-server-mocks",
"//packages/core/http/core-http-server-mocks",
]

TYPES_DEPS = [
"@npm//@types/node",
"@npm//@types/jest",
"@npm//moment",
"@npm//@elastic/elasticsearch",
"@npm//@elastic/numeral",
"//packages/kbn-utility-types:npm_module_types",
"//packages/kbn-std:npm_module_types",
"//packages/kbn-es-errors:npm_module_types",
"//packages/kbn-logging:npm_module_types",
"//packages/core/http/core-http-server:npm_module_types",
"//packages/core/http/core-http-router-server-internal:npm_module_types",
"//packages/core/elasticsearch/core-elasticsearch-server:npm_module_types",
]

jsts_transpiler(
name = "target_node",
srcs = SRCS,
build_pkg_name = package_name(),
)

ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = [
"//:tsconfig.base.json",
"//:tsconfig.bazel.json",
],
)

ts_project(
name = "tsc_types",
args = ['--pretty'],
srcs = SRCS,
deps = TYPES_DEPS,
declaration = True,
declaration_map = True,
emit_declaration_only = True,
out_dir = "target_types",
root_dir = "src",
tsconfig = ":tsconfig",
)

js_library(
name = PKG_DIRNAME,
srcs = NPM_MODULE_EXTRA_FILES,
deps = RUNTIME_DEPS + [":target_node"],
package_name = PKG_REQUIRE_NAME,
visibility = ["//visibility:public"],
)

pkg_npm(
name = "npm_module",
deps = [":" + PKG_DIRNAME],
)

filegroup(
name = "build",
srcs = [":npm_module"],
visibility = ["//visibility:public"],
)

pkg_npm_types(
name = "npm_module_types",
srcs = SRCS,
deps = [":tsc_types"],
package_name = PKG_REQUIRE_NAME,
tsconfig = ":tsconfig",
visibility = ["//visibility:public"],
)

filegroup(
name = "build_types",
srcs = [":npm_module_types"],
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @kbn/core-elasticsearch-client-server-internal

This package contains the internal implementation for Core's server-side elasticsearch client.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../../../..',
roots: ['<rootDir>/packages/core/elasticsearch/core-elasticsearch-client-server-internal'],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@kbn/core-elasticsearch-client-server-internal",
"private": true,
"version": "1.0.0",
"main": "./target_node/index.js",
"license": "SSPL-1.0 OR Elastic License 2.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
*/

import { duration } from 'moment';
import { ElasticsearchClientConfig, parseClientOptions } from './client_config';
import { DEFAULT_HEADERS } from '../default_headers';
import type { ElasticsearchClientConfig } from '@kbn/core-elasticsearch-server';
import { parseClientOptions } from './client_config';
import { DEFAULT_HEADERS } from './headers';

const createConfig = (
parts: Partial<ElasticsearchClientConfig> = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,8 @@ import { ConnectionOptions as TlsConnectionOptions } from 'tls';
import { URL } from 'url';
import { Duration } from 'moment';
import type { ClientOptions } from '@elastic/elasticsearch/lib/client';
import { ElasticsearchConfig } from '../elasticsearch_config';
import { DEFAULT_HEADERS } from '../default_headers';

/**
* Configuration options to be used to create a {@link IClusterClient | cluster client} using the
* {@link ElasticsearchServiceStart.createClient | createClient API}
*
* @public
*/
export type ElasticsearchClientConfig = Pick<
ElasticsearchConfig,
| 'customHeaders'
| 'maxSockets'
| 'compression'
| 'sniffOnStart'
| 'sniffOnConnectionFault'
| 'requestHeadersWhitelist'
| 'sniffInterval'
| 'hosts'
| 'username'
| 'password'
| 'serviceAccountToken'
> & {
pingTimeout?: ElasticsearchConfig['pingTimeout'] | ClientOptions['pingTimeout'];
requestTimeout?: ElasticsearchConfig['requestTimeout'] | ClientOptions['requestTimeout'];
ssl?: Partial<ElasticsearchConfig['ssl']>;
keepAlive?: boolean;
caFingerprint?: ClientOptions['caFingerprint'];
};
import type { ElasticsearchClientConfig } from '@kbn/core-elasticsearch-server';
import { DEFAULT_HEADERS } from './headers';

/**
* Parse the client options from given client config and `scoped` flag.
Expand Down
Loading

0 comments on commit 3508350

Please sign in to comment.