Skip to content

Commit

Permalink
[EEM] Add versioning for entity definitions (#187692)
Browse files Browse the repository at this point in the history
This PR adds a `version` field to the `EntityDefinition` type, making it
required in the API calls. It must be a SemVer string.
The version is added to the ingest pipelines and transforms as part of
their metadata.
The version is included in the output documents alongside the schema
version.

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
miltonhultgren and kibanamachine authored Jul 9, 2024
1 parent b2e82f6 commit 47178a7
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/kbn-check-mappings-update-cli/current_fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@
"metrics",
"name",
"staticFields",
"type"
"type",
"version"
],
"entity-discovery-api-key": [
"apiKey"
Expand Down
3 changes: 3 additions & 0 deletions packages/kbn-check-mappings-update-cli/current_mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,9 @@
},
"type": {
"type": "keyword"
},
"version": {
"type": "keyword"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('checking migration metadata changes on all registered SO types', () =>
"endpoint:unified-user-artifact-manifest": "71c7fcb52c658b21ea2800a6b6a76972ae1c776e",
"endpoint:user-artifact-manifest": "1c3533161811a58772e30cdc77bac4631da3ef2b",
"enterprise_search_telemetry": "9ac912e1417fc8681e0cd383775382117c9e3d3d",
"entity-definition": "33fe0194bd896f0bfe479d55f6de20f8ba1d7713",
"entity-definition": "331a2ba0ee9f24936ef049683549c8af7e46f03a",
"entity-discovery-api-key": "c267a65c69171d1804362155c1378365f5acef88",
"epm-packages": "8042d4a1522f6c4e6f5486e791b3ffe3a22f88fd",
"epm-packages-assets": "7a3e58efd9a14191d0d1a00b8aaed30a145fd0b1",
Expand Down
6 changes: 6 additions & 0 deletions x-pack/packages/kbn-entities-schema/src/schema/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,9 @@ export const identityFieldsSchema = z
optional: z.boolean(),
})
.or(z.string().transform((value) => ({ field: value, optional: false })));

const semVerRegex = new RegExp(/^[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}$/);
export const semVerSchema = z.string().refine((maybeSemVer) => semVerRegex.test(maybeSemVer), {
message:
'The string does use the Semantic Versioning (Semver) format of {major}.{minor}.{patch} (e.g., 1.0.0), ensure each part contains only digits.',
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import {
filterSchema,
durationSchema,
identityFieldsSchema,
semVerSchema,
} from './common';

export const entityDefinitionSchema = z.object({
id: z.string().regex(/^[\w-]+$/),
version: semVerSchema,
name: z.string(),
description: z.optional(z.string()),
type: z.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { BUILT_IN_ID_PREFIX } from './constants';

export const builtInServicesEntityDefinition: EntityDefinition = entityDefinitionSchema.parse({
id: `${BUILT_IN_ID_PREFIX}services`,
version: '0.1.0',
name: 'Services from logs',
type: 'service',
managed: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export async function createAndInstallHistoryIngestPipeline(
esClient.ingest.putPipeline({
id: historyId,
processors: historyProcessors,
_meta: {
definitionVersion: definition.version,
},
}),
{ logger }
);
Expand All @@ -51,6 +54,9 @@ export async function createAndInstallLatestIngestPipeline(
esClient.ingest.putPipeline({
id: latestId,
processors: latestProcessors,
_meta: {
definitionVersion: definition.version,
},
}),
{ logger }
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { entityDefinitionSchema } from '@kbn/entities-schema';
export const entityDefinition = entityDefinitionSchema.parse({
id: 'admin-console-services',
version: '999.999.999',
name: 'Services for Admin Console',
type: 'service',
indexPatterns: ['kbn-data-forge-fake_stack.*'],
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { EntityDefinition } from '@kbn/entities-schema';
import { ENTITY_SCHEMA_VERSION_V1 } from '../../../../common/constants_entities';
import { generateHistoryIndexName } from '../helpers/generate_component_id';

function createIdTemplate(definition: EntityDefinition) {
Expand Down Expand Up @@ -62,6 +63,18 @@ export function generateHistoryProcessors(definition: EntityDefinition) {
value: definition.id,
},
},
{
set: {
field: 'entity.definitionVersion',
value: definition.version,
},
},
{
set: {
field: 'entity.schemaVersion',
value: ENTITY_SCHEMA_VERSION_V1,
},
},
{
set: {
field: 'entity.displayName',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { EntityDefinition } from '@kbn/entities-schema';
import { ENTITY_SCHEMA_VERSION_V1 } from '../../../../common/constants_entities';
import { generateLatestIndexName } from '../helpers/generate_component_id';

function mapDestinationToPainless(destination: string) {
Expand Down Expand Up @@ -56,6 +57,18 @@ export function generateLatestProcessors(definition: EntityDefinition) {
value: definition.id,
},
},
{
set: {
field: 'entity.definitionVersion',
value: definition.version,
},
},
{
set: {
field: 'entity.schemaVersion',
value: ENTITY_SCHEMA_VERSION_V1,
},
},
...(definition.staticFields != null
? Object.keys(definition.staticFields).map((field) => ({
set: { field, value: definition.staticFields![field] },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ const assertHasCreatedDefinition = (
expect(esClient.ingest.putPipeline).toBeCalledWith({
id: generateHistoryIngestPipelineId(builtInServicesEntityDefinition),
processors: expect.anything(),
_meta: {
definitionVersion: '0.1.0',
},
});
expect(esClient.ingest.putPipeline).toBeCalledWith({
id: generateLatestIngestPipelineId(builtInServicesEntityDefinition),
processors: expect.anything(),
_meta: {
definitionVersion: '0.1.0',
},
});

expect(esClient.transform.putTransform).toBeCalledTimes(2);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export function generateHistoryTransform(

return {
transform_id: generateHistoryTransformId(definition),
_meta: {
definitionVersion: definition.version,
},
defer_validation: true,
source: {
index: definition.indexPatterns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export function generateLatestTransform(
): TransformPutTransformRequest {
return {
transform_id: generateLatestTransformId(definition),
_meta: {
definitionVersion: definition.version,
},
defer_validation: true,
source: {
index: `${generateHistoryIndexName(definition)}.*`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const entityDefinition: SavedObjectsType = {
dynamic: false,
properties: {
id: { type: 'keyword' },
version: { type: 'keyword' },
name: { type: 'text' },
description: { type: 'text' },
type: { type: 'keyword' },
Expand All @@ -37,4 +38,16 @@ export const entityDefinition: SavedObjectsType = {
return `EntityDefinition: [${savedObject.attributes.name}]`;
},
},
modelVersions: {
'1': {
changes: [
{
type: 'mappings_addition',
addedMappings: {
version: { type: 'keyword' },
},
},
],
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ export const entitiesEntityComponentTemplateConfig: ClusterPutComponentTemplateR
ignore_above: 1024,
type: 'keyword',
},
definitionVersion: {
ignore_above: 1024,
type: 'keyword',
},
schemaVersion: {
ignore_above: 1024,
type: 'keyword',
},
lastSeenTimestamp: {
type: 'date',
},
Expand Down

0 comments on commit 47178a7

Please sign in to comment.