-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathtenant_index.ts
134 lines (128 loc) · 4.65 KB
/
tenant_index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright OpenSearch Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import {
OpenSearchClient,
ISavedObjectTypeRegistry,
Logger,
SavedObjectsSerializer,
} from '../../../../src/core/server';
import { IndexMapping } from '../../../../src/core/server/saved_objects/mappings';
import {
buildActiveMappings,
DocumentMigrator,
IndexMigrator,
MigrationOpenSearchClient,
} from '../../../../src/core/server/saved_objects/migrations/core';
import { createIndexMap } from '../../../../src/core/server/saved_objects/migrations/core/build_index_map';
import { mergeTypes } from '../../../../src/core/server/saved_objects/migrations/opensearch_dashboards/opensearch_dashboards_migrator';
import { SecurityClient } from '../backend/opensearch_security_client';
import { MAX_INTEGER } from '../../common';
export async function setupIndexTemplate(
esClient: OpenSearchClient,
opensearchDashboardsIndex: string,
typeRegistry: ISavedObjectTypeRegistry,
logger: Logger
) {
const mappings: IndexMapping = buildActiveMappings(mergeTypes(typeRegistry.getAllTypes()));
try {
await esClient.indices.putIndexTemplate({
name: 'tenant_template',
body: {
// Setting priority to the max value to avoid being overridden by custom index templates.
priority: MAX_INTEGER,
index_patterns: [
opensearchDashboardsIndex + '_-*_*',
opensearchDashboardsIndex + '_0*_*',
opensearchDashboardsIndex + '_1*_*',
opensearchDashboardsIndex + '_2*_*',
opensearchDashboardsIndex + '_3*_*',
opensearchDashboardsIndex + '_4*_*',
opensearchDashboardsIndex + '_5*_*',
opensearchDashboardsIndex + '_6*_*',
opensearchDashboardsIndex + '_7*_*',
opensearchDashboardsIndex + '_8*_*',
opensearchDashboardsIndex + '_9*_*',
],
template: {
settings: {
number_of_shards: 1,
},
mappings,
},
},
});
} catch (error) {
logger.error(error);
throw error;
}
}
export async function migrateTenantIndices(
opensearchDashboardsVersion: string,
migrationClient: MigrationOpenSearchClient,
securityClient: SecurityClient,
typeRegistry: ISavedObjectTypeRegistry,
serializer: SavedObjectsSerializer,
logger: Logger
) {
let tenantInfo: any;
try {
tenantInfo = await securityClient.getTenantInfoWithInternalUser();
} catch (error) {
logger.error(error);
throw error;
}
// follows the same approach in opensearch_dashboards_migrator.ts to initiate DocumentMigrator here
const documentMigrator = new DocumentMigrator({
opensearchDashboardsVersion,
typeRegistry,
log: logger,
});
for (const indexName of Object.keys(tenantInfo)) {
const indexMap = createIndexMap({
opensearchDashboardsIndexName: indexName,
indexMap: mergeTypes(typeRegistry.getAllTypes()),
registry: typeRegistry,
});
// follows the same aporach in opensearch_dashboards_mirator.ts to construct IndexMigrator
//
// FIXME: hard code batchSize, pollInterval, and scrollDuration for now
// they are used to fetched from `migration.xxx` config, which is not accessible from new playform
const indexMigrator = new IndexMigrator({
batchSize: 100,
client: migrationClient,
documentMigrator,
index: indexName,
log: logger,
mappingProperties: indexMap[indexName].typeMappings,
pollInterval: 1500, // millisec
scrollDuration: '15m',
serializer,
obsoleteIndexTemplatePattern: undefined,
convertToAliasScript: indexMap[indexName].script,
});
try {
await indexMigrator.migrate();
} catch (error) {
logger.error(error);
// fail early, exit the kibana process
// NOTE: according to https://github.com/elastic/kibana/issues/41983 ,
// PR https://github.com/elastic/kibana/pull/75819 , API to allow plugins
// to set status will be available in 7.10, for now, we fail OpenSearchDashboards
// process to indicate index migration error. Customer can fix their
// tenant indices in ES then restart OpenSearchDashboards.
process.exit(1);
}
}
}