forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fleet] add ilm policy per data stream (elastic#85492) (elastic#85865)
Co-authored-by: kevinlog <kevin.logan@elastic.co> Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: nnamdifrankie <56440728+nnamdifrankie@users.noreply.github.com> Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
c5bd996
commit 7fa2e9d
Showing
11 changed files
with
228 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
x-pack/plugins/fleet/server/services/epm/elasticsearch/datastream_ilm/install.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { SavedObjectsClientContract } from 'kibana/server'; | ||
import { | ||
ElasticsearchAssetType, | ||
EsAssetReference, | ||
InstallablePackage, | ||
RegistryDataStream, | ||
} from '../../../../../common/types/models'; | ||
import { CallESAsCurrentUser } from '../../../../types'; | ||
import { getInstallation } from '../../packages'; | ||
import { deleteIlmRefs, deleteIlms } from './remove'; | ||
import { saveInstalledEsRefs } from '../../packages/install'; | ||
import { getAsset } from '../transform/common'; | ||
|
||
interface IlmInstallation { | ||
installationName: string; | ||
content: string; | ||
} | ||
|
||
interface IlmPathDataset { | ||
path: string; | ||
dataStream: RegistryDataStream; | ||
} | ||
|
||
export const installIlmForDataStream = async ( | ||
registryPackage: InstallablePackage, | ||
paths: string[], | ||
callCluster: CallESAsCurrentUser, | ||
savedObjectsClient: SavedObjectsClientContract | ||
) => { | ||
const installation = await getInstallation({ savedObjectsClient, pkgName: registryPackage.name }); | ||
let previousInstalledIlmEsAssets: EsAssetReference[] = []; | ||
if (installation) { | ||
previousInstalledIlmEsAssets = installation.installed_es.filter( | ||
({ type, id }) => type === ElasticsearchAssetType.dataStreamIlmPolicy | ||
); | ||
} | ||
|
||
// delete all previous ilm | ||
await deleteIlms( | ||
callCluster, | ||
previousInstalledIlmEsAssets.map((asset) => asset.id) | ||
); | ||
// install the latest dataset | ||
const dataStreams = registryPackage.data_streams; | ||
if (!dataStreams?.length) return []; | ||
const dataStreamIlmPaths = paths.filter((path) => isDataStreamIlm(path)); | ||
let installedIlms: EsAssetReference[] = []; | ||
if (dataStreamIlmPaths.length > 0) { | ||
const ilmPathDatasets = dataStreams.reduce<IlmPathDataset[]>((acc, dataStream) => { | ||
dataStreamIlmPaths.forEach((path) => { | ||
if (isDatasetIlm(path, dataStream.path)) { | ||
acc.push({ path, dataStream }); | ||
} | ||
}); | ||
return acc; | ||
}, []); | ||
|
||
const ilmRefs = ilmPathDatasets.reduce<EsAssetReference[]>((acc, ilmPathDataset) => { | ||
if (ilmPathDataset) { | ||
acc.push({ | ||
id: getIlmNameForInstallation(ilmPathDataset), | ||
type: ElasticsearchAssetType.dataStreamIlmPolicy, | ||
}); | ||
} | ||
return acc; | ||
}, []); | ||
|
||
await saveInstalledEsRefs(savedObjectsClient, registryPackage.name, ilmRefs); | ||
|
||
const ilmInstallations: IlmInstallation[] = ilmPathDatasets.map( | ||
(ilmPathDataset: IlmPathDataset) => { | ||
return { | ||
installationName: getIlmNameForInstallation(ilmPathDataset), | ||
content: getAsset(ilmPathDataset.path).toString('utf-8'), | ||
}; | ||
} | ||
); | ||
|
||
const installationPromises = ilmInstallations.map(async (ilmInstallation) => { | ||
return handleIlmInstall({ callCluster, ilmInstallation }); | ||
}); | ||
|
||
installedIlms = await Promise.all(installationPromises).then((results) => results.flat()); | ||
} | ||
|
||
if (previousInstalledIlmEsAssets.length > 0) { | ||
const currentInstallation = await getInstallation({ | ||
savedObjectsClient, | ||
pkgName: registryPackage.name, | ||
}); | ||
|
||
// remove the saved object reference | ||
await deleteIlmRefs( | ||
savedObjectsClient, | ||
currentInstallation?.installed_es || [], | ||
registryPackage.name, | ||
previousInstalledIlmEsAssets.map((asset) => asset.id), | ||
installedIlms.map((installed) => installed.id) | ||
); | ||
} | ||
return installedIlms; | ||
}; | ||
|
||
async function handleIlmInstall({ | ||
callCluster, | ||
ilmInstallation, | ||
}: { | ||
callCluster: CallESAsCurrentUser; | ||
ilmInstallation: IlmInstallation; | ||
}): Promise<EsAssetReference> { | ||
await callCluster('transport.request', { | ||
method: 'PUT', | ||
path: `/_ilm/policy/${ilmInstallation.installationName}`, | ||
body: ilmInstallation.content, | ||
}); | ||
|
||
return { id: ilmInstallation.installationName, type: ElasticsearchAssetType.dataStreamIlmPolicy }; | ||
} | ||
|
||
const isDataStreamIlm = (path: string) => { | ||
return new RegExp('(?<package>.*)/data_stream/(?<dataset>.*)/elasticsearch/ilm/*.*').test(path); | ||
}; | ||
|
||
const isDatasetIlm = (path: string, datasetName: string) => { | ||
return new RegExp(`(?<package>.*)/data_stream\\/${datasetName}/elasticsearch/ilm/*.*`).test(path); | ||
}; | ||
|
||
const getIlmNameForInstallation = (ilmPathDataset: IlmPathDataset) => { | ||
const filename = ilmPathDataset?.path.split('/')?.pop()?.split('.')[0]; | ||
return `${ilmPathDataset.dataStream.type}-${ilmPathDataset.dataStream.package}.${ilmPathDataset.dataStream.path}-${filename}`; | ||
}; |
42 changes: 42 additions & 0 deletions
42
x-pack/plugins/fleet/server/services/epm/elasticsearch/datastream_ilm/remove.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { SavedObjectsClientContract } from 'kibana/server'; | ||
import { CallESAsCurrentUser, ElasticsearchAssetType, EsAssetReference } from '../../../../types'; | ||
import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../../../common/constants'; | ||
|
||
export const deleteIlms = async (callCluster: CallESAsCurrentUser, ilmPolicyIds: string[]) => { | ||
await Promise.all( | ||
ilmPolicyIds.map(async (ilmPolicyId) => { | ||
await callCluster('transport.request', { | ||
method: 'DELETE', | ||
path: `_ilm/policy/${ilmPolicyId}`, | ||
ignore: [404, 400], | ||
}); | ||
}) | ||
); | ||
}; | ||
|
||
export const deleteIlmRefs = async ( | ||
savedObjectsClient: SavedObjectsClientContract, | ||
installedEsAssets: EsAssetReference[], | ||
pkgName: string, | ||
installedEsIdToRemove: string[], | ||
currentInstalledEsIlmIds: string[] | ||
) => { | ||
const seen = new Set<string>(); | ||
const filteredAssets = installedEsAssets.filter(({ type, id }) => { | ||
if (type !== ElasticsearchAssetType.dataStreamIlmPolicy) return true; | ||
const add = | ||
(currentInstalledEsIlmIds.includes(id) || !installedEsIdToRemove.includes(id)) && | ||
!seen.has(id); | ||
seen.add(id); | ||
return add; | ||
}); | ||
return savedObjectsClient.update(PACKAGES_SAVED_OBJECT_TYPE, pkgName, { | ||
installed_es: filteredAssets, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...ckages/all_assets/0.1.0/data_stream/test_metrics/elasticsearch/ilm_policy/all_assets.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"policy": { | ||
"phases": { | ||
"hot": { | ||
"min_age": "0ms", | ||
"actions": { | ||
"rollover": { | ||
"max_size": "50gb", | ||
"max_age": "30d" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |