Skip to content

Commit

Permalink
[Fleet] update component templates with deprecated setting (elastic#2…
Browse files Browse the repository at this point in the history
…10200)

## Summary

Closes elastic#209087

Added logic to Fleet setup that queries component templates with the
deprecated `_source.mode` setting.
Querying only `metrics-*` prefixed component templates, I think only
those use `_source.mode` setting.

Steps to verify:
- Upload
[apm-8.8.0.zip](https://github.com/user-attachments/files/18708082/apm-8.8.0.zip)
package to http://localhost:5601/app/integrations/create/upload
- Deprecation warnings should appear in Upgrade assistant
http://localhost:5601/app/management/stack/upgrade_assistant/es_deprecations
- Visit Fleet UI or run setup API to let setup logic run
- Verify that deprecations disappeared
- Verify that the component templates are updated

<img width="1190" alt="image"
src="https://github.com/user-attachments/assets/20c4265f-461e-46d1-b075-6ba1111ef9d2"
/>

<img width="2533" alt="image"
src="https://github.com/user-attachments/assets/6961c885-7a91-4d07-90b9-c01a8c133cc9"
/>

<img width="901" alt="image"
src="https://github.com/user-attachments/assets/6b712f94-fd9c-4038-8dc4-cfcf7650cca7"
/>

```
[2025-02-07T15:39:42.396+01:00][DEBUG][plugins.fleet] Update deprecated _source.mode in component templates
[2025-02-07T15:39:42.401+01:00][DEBUG][plugins.fleet] Updating component templates with deprecated _source.mode config: metrics-apm.app@package,metrics-apm.internal@package,metrics-apm.service_transaction.1m@package,metrics-apm.service_destination.60m@package,metrics-apm.transaction.1m@package,metrics-apm.service_summary.10m@package,metrics-apm.transaction.10m@package,metrics-apm.service_transaction.10m@package,metrics-apm.transaction.60m@package,metrics-apm.service_destination.10m@package,metrics-apm.service_summary.1m@package,metrics-apm.service_destination.1m@package,metrics-apm.service_transaction.60m@package,metrics-apm.service_summary.60m@package


GET _component_template/metrics-*

{
  "component_templates": [
    {
      "name": "metrics-apm.app@package",
      "component_template": {
        "template": {
          "settings": {
            "index": {
              "lifecycle": {
                "name": "metrics-apm.app_metrics-default_policy"
              },
              "default_pipeline": "metrics-apm.app-8.8.0",
              "mapping": {
                "total_fields": {
                  "limit": "1000"
                },
                "source": {
                  "mode": "synthetic"
                }
              }
            }
          },
          "mappings": {
            "dynamic": true,
            "_source": {},
            "dynamic_templates": [
              {
                "histogram": {
                  "mapping": {
                    "type": "histogram"
                  }
                }
              },
```


### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
juliaElastic authored Feb 10, 2025
1 parent bcb3d70 commit a61c729
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jest.mock('./download_source');
jest.mock('./epm/packages');
jest.mock('./setup/managed_package_policies');
jest.mock('./setup/upgrade_package_install_version');
jest.mock('./setup/update_deprecated_component_templates');
jest.mock('./epm/elasticsearch/template/install', () => {
return {
...jest.requireActual('./epm/elasticsearch/template/install'),
Expand Down
4 changes: 4 additions & 0 deletions x-pack/platform/plugins/shared/fleet/server/services/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
getPreconfiguredDeleteUnenrolledAgentsSettingFromConfig,
} from './preconfiguration/delete_unenrolled_agent_setting';
import { backfillPackagePolicySupportsAgentless } from './backfill_agentless';
import { updateDeprecatedComponentTemplates } from './setup/update_deprecated_component_templates';

export interface SetupStatus {
isInitialized: boolean;
Expand Down Expand Up @@ -309,6 +310,9 @@ async function createSetupSideEffects(
logger.debug('Backfilling package policy supports_agentless field');
await backfillPackagePolicySupportsAgentless(esClient);

logger.debug('Update deprecated _source.mode in component templates');
await updateDeprecatedComponentTemplates(esClient);

const nonFatalErrors = [
...preconfiguredPackagesNonFatalErrors,
...(messageSigningServiceNonFatalError ? [messageSigningServiceNonFatalError] : []),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
export { upgradePackageInstallVersion } from './upgrade_package_install_version';
export { upgradeAgentPolicySchemaVersion } from './upgrade_agent_policy_schema_version';
export { ensureAgentPoliciesFleetServerKeysAndPolicies } from './fleet_server_policies_enrollment_keys';
export { updateDeprecatedComponentTemplates } from './update_deprecated_component_templates';
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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 { updateDeprecatedComponentTemplates } from './update_deprecated_component_templates';

jest.mock('..', () => ({
appContextService: {
getLogger: () => ({
debug: jest.fn(),
}),
},
}));

describe('updateDeprecatedComponentTemplates', () => {
it('should update deprecated component templates', async () => {
const esClientMock: any = {
cluster: {
getComponentTemplate: jest.fn().mockResolvedValue({
component_templates: [
{
name: 'metrics-apm.app@package',
component_template: {
template: {
settings: {},
mappings: {
_source: {
mode: 'synthetic',
},
properties: {},
},
},
_meta: {
managed_by: 'fleet',
},
},
},
{
name: 'metrics-other',
component_template: {
template: {
settings: {},
mappings: {
properties: {},
},
},
_meta: {
managed_by: 'fleet',
},
},
},
],
}),
putComponentTemplate: jest.fn(),
},
};

await updateDeprecatedComponentTemplates(esClientMock);

expect(esClientMock.cluster.putComponentTemplate).toHaveBeenCalledTimes(1);
expect(esClientMock.cluster.putComponentTemplate).toHaveBeenCalledWith({
body: {
template: {
mappings: {
_source: {},
properties: {},
},
settings: {
index: {
mapping: {
source: {
mode: 'synthetic',
},
},
},
},
},
},
name: 'metrics-apm.app@package',
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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 pMap from 'p-map';
import type { ElasticsearchClient } from '@kbn/core/server';

import { appContextService } from '..';

export async function updateDeprecatedComponentTemplates(esClient: ElasticsearchClient) {
const componentTemplates = await esClient.cluster.getComponentTemplate({
name: 'metrics-*',
});

const deprecatedTemplates = componentTemplates.component_templates.filter(
(componentTemplate) =>
componentTemplate.component_template._meta?.managed_by === 'fleet' &&
!!componentTemplate.component_template.template.mappings?._source?.mode
);

appContextService
.getLogger()
.debug(
`Updating component templates with deprecated _source.mode config: ${deprecatedTemplates.map(
(template) => template.name
)}`
);

await pMap(
deprecatedTemplates,
async (componentTemplate) => {
const source = componentTemplate.component_template.template.mappings!._source;
const { mode, ...restOfSource } = source!;
const settings = componentTemplate.component_template.template.settings;
await esClient.cluster.putComponentTemplate({
name: componentTemplate.name,
body: {
template: {
settings: {
...settings,
index: {
...settings?.index,
mapping: {
...settings?.index?.mapping,
// @ts-expect-error Property 'source' does not exist on type 'IndicesMappingLimitSettings'
source: {
// @ts-expect-error Property 'source.mode' does not exist on type 'IndicesMappingLimitSettings'
...settings?.index?.mapping?.source,
mode,
},
},
},
},
mappings: {
...componentTemplate.component_template.template.mappings,
_source: restOfSource,
},
},
},
});
},
{
concurrency: 10,
}
);
}

0 comments on commit a61c729

Please sign in to comment.