-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLEET] Adding support for installing ML models (#107710)
* adds support for saved object based ml models * adds es asset type and ml model install handler * wip: handle top level pipeline install * remove unnecessary mlModel savedObject type * add package manifest license check * get modelid from model path * add fleet api test for ml model * replace test mlModel for api test with smaller test model * cleanup install/remove and ensure pipelines are retained when upgrading * fix types - update test model id * fix types * remove hard coded ml category and check top level pipeline on upgrade * update ml model test file * ensure deduplicated asset refs are saved * Fix api integration update test Co-authored-by: Kibana Machine <[email protected]> Co-authored-by: Nicolas Chaulet <[email protected]>
- Loading branch information
1 parent
5fcc118
commit c240ccf
Showing
21 changed files
with
523 additions
and
49 deletions.
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
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
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/fleet/server/services/epm/elasticsearch/ml_model/common.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,8 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export { getAsset } from '../../archive'; |
9 changes: 9 additions & 0 deletions
9
x-pack/plugins/fleet/server/services/epm/elasticsearch/ml_model/index.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,9 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export { installMlModel } from './install'; | ||
export { deleteMlModel } from './remove'; |
87 changes: 87 additions & 0 deletions
87
x-pack/plugins/fleet/server/services/epm/elasticsearch/ml_model/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,87 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { ElasticsearchClient, SavedObjectsClientContract } from 'kibana/server'; | ||
import { ResponseError } from '@elastic/elasticsearch/lib/errors'; | ||
|
||
import { saveInstalledEsRefs } from '../../packages/install'; | ||
import { getPathParts } from '../../archive'; | ||
import { ElasticsearchAssetType } from '../../../../../common/types/models'; | ||
import type { EsAssetReference, InstallablePackage } from '../../../../../common/types/models'; | ||
|
||
import { getAsset } from './common'; | ||
|
||
interface MlModelInstallation { | ||
installationName: string; | ||
content: string; | ||
} | ||
|
||
export const installMlModel = async ( | ||
installablePackage: InstallablePackage, | ||
paths: string[], | ||
esClient: ElasticsearchClient, | ||
savedObjectsClient: SavedObjectsClientContract | ||
) => { | ||
const mlModelPath = paths.find((path) => isMlModel(path)); | ||
|
||
const installedMlModels: EsAssetReference[] = []; | ||
if (mlModelPath !== undefined) { | ||
const content = getAsset(mlModelPath).toString('utf-8'); | ||
const pathParts = mlModelPath.split('/'); | ||
const modelId = pathParts[pathParts.length - 1].replace('.json', ''); | ||
|
||
const mlModelRef = { | ||
id: modelId, | ||
type: ElasticsearchAssetType.mlModel, | ||
}; | ||
|
||
// get and save ml model refs before installing ml model | ||
await saveInstalledEsRefs(savedObjectsClient, installablePackage.name, [mlModelRef]); | ||
|
||
const mlModel: MlModelInstallation = { | ||
installationName: modelId, | ||
content, | ||
}; | ||
|
||
const result = await handleMlModelInstall({ esClient, mlModel }); | ||
installedMlModels.push(result); | ||
} | ||
return installedMlModels; | ||
}; | ||
|
||
const isMlModel = (path: string) => { | ||
const pathParts = getPathParts(path); | ||
|
||
return !path.endsWith('/') && pathParts.type === ElasticsearchAssetType.mlModel; | ||
}; | ||
|
||
async function handleMlModelInstall({ | ||
esClient, | ||
mlModel, | ||
}: { | ||
esClient: ElasticsearchClient; | ||
mlModel: MlModelInstallation; | ||
}): Promise<EsAssetReference> { | ||
try { | ||
await esClient.ml.putTrainedModel({ | ||
model_id: mlModel.installationName, | ||
defer_definition_decompression: true, | ||
timeout: '45s', | ||
body: mlModel.content, | ||
}); | ||
} catch (err) { | ||
// swallow the error if the ml model already exists. | ||
const isAlreadyExistError = | ||
err instanceof ResponseError && | ||
err?.body?.error?.type === 'resource_already_exists_exception'; | ||
if (!isAlreadyExistError) { | ||
throw err; | ||
} | ||
} | ||
|
||
return { id: mlModel.installationName, type: ElasticsearchAssetType.mlModel }; | ||
} |
23 changes: 23 additions & 0 deletions
23
x-pack/plugins/fleet/server/services/epm/elasticsearch/ml_model/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,23 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { ElasticsearchClient } from 'kibana/server'; | ||
|
||
import { appContextService } from '../../../app_context'; | ||
|
||
export const deleteMlModel = async (esClient: ElasticsearchClient, mlModelIds: string[]) => { | ||
const logger = appContextService.getLogger(); | ||
if (mlModelIds.length) { | ||
logger.info(`Deleting currently installed ml model ids ${mlModelIds}`); | ||
} | ||
await Promise.all( | ||
mlModelIds.map(async (modelId) => { | ||
await esClient.ml.deleteTrainedModel({ model_id: modelId }, { ignore: [404] }); | ||
logger.info(`Deleted: ${modelId}`); | ||
}) | ||
); | ||
}; |
Oops, something went wrong.