-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[APM] Syncs agent config settings to APM Fleet policies #100744
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ogupte
merged 13 commits into
elastic:master
from
ogupte:apm-95501-agent-config-fleet-sync
Jun 8, 2021
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c82917e
[APM] Syncs agent config settings to APM Fleet policies (#95501)
ogupte b2bb676
fixes eslint issues
ogupte 9ff99d1
fixes malformed line comment
ogupte c95249f
- consolidated logic that applies agent configurations to package pol…
ogupte 200b23f
Synchronizes agent configs whenever configuration is deleted.
ogupte 007cefc
PR feedback
ogupte 49d660a
Merge branch 'master' into apm-95501-agent-config-fleet-sync
ogupte a25497e
Merge branch 'master' into apm-95501-agent-config-fleet-sync
kibanamachine 6129f4f
nest agent_config within `apm-server` in the package policy input
ogupte 62379ad
Merge branch 'master' into apm-95501-agent-config-fleet-sync
kibanamachine 2d522ce
nests agent_config under the requried 'value' property of config['apm…
ogupte 6793d62
- externalizes getApmPackagePolicies for reusability
ogupte aaf5263
Merge branch 'master' into apm-95501-agent-config-fleet-sync
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -23,7 +23,8 @@ | |
| "security", | ||
| "ml", | ||
| "home", | ||
| "maps" | ||
| "maps", | ||
| "fleet" | ||
| ], | ||
| "server": true, | ||
| "ui": true, | ||
|
|
||
30 changes: 30 additions & 0 deletions
30
x-pack/plugins/apm/server/lib/fleet/get_apm_package_policies.ts
This file contains hidden or 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,30 @@ | ||
| /* | ||
| * 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 { | ||
| CoreSetup, | ||
| CoreStart, | ||
| SavedObjectsClientContract, | ||
| } from 'kibana/server'; | ||
| import { APMPluginStartDependencies } from '../../types'; | ||
| import { getInternalSavedObjectsClient } from '../helpers/get_internal_saved_objects_client'; | ||
|
|
||
| export async function getApmPackgePolicies({ | ||
| core, | ||
| fleetPluginStart, | ||
| }: { | ||
| core: { setup: CoreSetup; start: () => Promise<CoreStart> }; | ||
| fleetPluginStart: NonNullable<APMPluginStartDependencies['fleet']>; | ||
| }) { | ||
| // @ts-ignore | ||
| const savedObjectsClient: SavedObjectsClientContract = await getInternalSavedObjectsClient( | ||
| core.setup | ||
| ); | ||
| return await fleetPluginStart.packagePolicyService.list(savedObjectsClient, { | ||
| kuery: 'ingest-package-policies.package.name:apm', | ||
| }); | ||
| } |
134 changes: 134 additions & 0 deletions
134
x-pack/plugins/apm/server/lib/fleet/register_fleet_policy_callbacks.ts
This file contains hidden or 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,134 @@ | ||||||||||||
| /* | ||||||||||||
| * 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 { APMPlugin, APMRouteHandlerResources } from '../..'; | ||||||||||||
| import { listConfigurations } from '../settings/agent_configuration/list_configurations'; | ||||||||||||
| import { setupRequest } from '../helpers/setup_request'; | ||||||||||||
| import { APMPluginStartDependencies } from '../../types'; | ||||||||||||
| import { ExternalCallback } from '../../../../fleet/server'; | ||||||||||||
| import { AGENT_NAME } from '../../../common/elasticsearch_fieldnames'; | ||||||||||||
| import { AgentConfiguration } from '../../../common/agent_configuration/configuration_types'; | ||||||||||||
|
|
||||||||||||
| export async function registerFleetPolicyCallbacks({ | ||||||||||||
| plugins, | ||||||||||||
| ruleDataClient, | ||||||||||||
| config, | ||||||||||||
| logger, | ||||||||||||
| }: { | ||||||||||||
| plugins: APMRouteHandlerResources['plugins']; | ||||||||||||
| ruleDataClient: APMRouteHandlerResources['ruleDataClient']; | ||||||||||||
| config: NonNullable<APMPlugin['currentConfig']>; | ||||||||||||
| logger: NonNullable<APMPlugin['logger']>; | ||||||||||||
| }) { | ||||||||||||
| if (!plugins.fleet) { | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
| const fleetPluginStart = await plugins.fleet.start(); | ||||||||||||
|
|
||||||||||||
| // Registers a callback invoked when a policy is created to populate the APM | ||||||||||||
| // integration policy with pre-existing agent configurations | ||||||||||||
| registerAgentConfigExternalCallback({ | ||||||||||||
| fleetPluginStart, | ||||||||||||
| callbackName: 'packagePolicyCreate', | ||||||||||||
cauemarcondes marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
| plugins, | ||||||||||||
| ruleDataClient, | ||||||||||||
| config, | ||||||||||||
| logger, | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| // Registers a callback invoked when a policy is updated to populate the APM | ||||||||||||
| // integration policy with existing agent configurations | ||||||||||||
| registerAgentConfigExternalCallback({ | ||||||||||||
| fleetPluginStart, | ||||||||||||
| callbackName: 'packagePolicyUpdate', | ||||||||||||
| plugins, | ||||||||||||
| ruleDataClient, | ||||||||||||
| config, | ||||||||||||
| logger, | ||||||||||||
| }); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| type ExternalCallbackParams = Parameters<ExternalCallback[1]>; | ||||||||||||
| type PackagePolicy = ExternalCallbackParams[0]; | ||||||||||||
| type Context = ExternalCallbackParams[1]; | ||||||||||||
| type Request = ExternalCallbackParams[2]; | ||||||||||||
|
|
||||||||||||
| function registerAgentConfigExternalCallback({ | ||||||||||||
| fleetPluginStart, | ||||||||||||
| callbackName, | ||||||||||||
| plugins, | ||||||||||||
| ruleDataClient, | ||||||||||||
| config, | ||||||||||||
| logger, | ||||||||||||
| }: { | ||||||||||||
| fleetPluginStart: NonNullable<APMPluginStartDependencies['fleet']>; | ||||||||||||
| callbackName: ExternalCallback[0]; | ||||||||||||
| plugins: APMRouteHandlerResources['plugins']; | ||||||||||||
| ruleDataClient: APMRouteHandlerResources['ruleDataClient']; | ||||||||||||
| config: NonNullable<APMPlugin['currentConfig']>; | ||||||||||||
| logger: NonNullable<APMPlugin['logger']>; | ||||||||||||
| }) { | ||||||||||||
| const callbackFn: ExternalCallback[1] = async ( | ||||||||||||
| packagePolicy: PackagePolicy, | ||||||||||||
| context: Context, | ||||||||||||
| request: Request | ||||||||||||
| ) => { | ||||||||||||
| if (packagePolicy.package?.name !== 'apm') { | ||||||||||||
| return packagePolicy; | ||||||||||||
| } | ||||||||||||
| const setup = await setupRequest({ | ||||||||||||
| context: context as any, | ||||||||||||
| params: { query: { _inspect: false } }, | ||||||||||||
| core: null as any, | ||||||||||||
| plugins, | ||||||||||||
| request, | ||||||||||||
| config, | ||||||||||||
| logger, | ||||||||||||
| ruleDataClient, | ||||||||||||
| }); | ||||||||||||
| const agentConfigurations = await listConfigurations({ setup }); | ||||||||||||
| return getPackagePolicyWithAgentConfigurations( | ||||||||||||
| packagePolicy, | ||||||||||||
| agentConfigurations | ||||||||||||
| ); | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| fleetPluginStart.registerExternalCallback(callbackName, callbackFn); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| const APM_SERVER = 'apm-server'; | ||||||||||||
|
|
||||||||||||
| // Immutable function applies the given package policy with a set of agent configurations | ||||||||||||
| export function getPackagePolicyWithAgentConfigurations( | ||||||||||||
| packagePolicy: PackagePolicy, | ||||||||||||
| agentConfigurations: AgentConfiguration[] | ||||||||||||
| ) { | ||||||||||||
| const [firstInput, ...restInputs] = packagePolicy.inputs; | ||||||||||||
| const apmServerValue = firstInput?.config?.[APM_SERVER].value; | ||||||||||||
| return { | ||||||||||||
| ...packagePolicy, | ||||||||||||
| inputs: [ | ||||||||||||
| { | ||||||||||||
| ...firstInput, | ||||||||||||
| config: { | ||||||||||||
| [APM_SERVER]: { | ||||||||||||
|
Comment on lines
+117
to
+118
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ogupte Don't you need to add the others
Suggested change
|
||||||||||||
| value: { | ||||||||||||
| ...apmServerValue, | ||||||||||||
| agent_config: agentConfigurations.map((configuration) => ({ | ||||||||||||
| service: configuration.service, | ||||||||||||
| config: configuration.settings, | ||||||||||||
| etag: configuration.etag, | ||||||||||||
| [AGENT_NAME]: configuration.agent_name, | ||||||||||||
| })), | ||||||||||||
| }, | ||||||||||||
| }, | ||||||||||||
| }, | ||||||||||||
| }, | ||||||||||||
| ...restInputs, | ||||||||||||
| ], | ||||||||||||
| }; | ||||||||||||
| } | ||||||||||||
59 changes: 59 additions & 0 deletions
59
x-pack/plugins/apm/server/lib/fleet/sync_agent_configs_to_apm_package_policies.ts
This file contains hidden or 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,59 @@ | ||
| /* | ||
| * 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 { | ||
| CoreSetup, | ||
| CoreStart, | ||
| SavedObjectsClientContract, | ||
| } from 'kibana/server'; | ||
| import { APMPluginStartDependencies } from '../../types'; | ||
| import { getInternalSavedObjectsClient } from '../helpers/get_internal_saved_objects_client'; | ||
| import { Setup } from '../helpers/setup_request'; | ||
| import { listConfigurations } from '../settings/agent_configuration/list_configurations'; | ||
| import { getApmPackgePolicies } from './get_apm_package_policies'; | ||
| import { getPackagePolicyWithAgentConfigurations } from './register_fleet_policy_callbacks'; | ||
|
|
||
| export async function syncAgentConfigsToApmPackagePolicies({ | ||
| core, | ||
| fleetPluginStart, | ||
| setup, | ||
| }: { | ||
| core: { setup: CoreSetup; start: () => Promise<CoreStart> }; | ||
| fleetPluginStart: NonNullable<APMPluginStartDependencies['fleet']>; | ||
| setup: Setup; | ||
| }) { | ||
| const coreStart = await core.start(); | ||
| const esClient = coreStart.elasticsearch.client.asInternalUser; | ||
| const [ | ||
| savedObjectsClient, | ||
| agentConfigurations, | ||
| packagePolicies, | ||
| ] = await Promise.all([ | ||
| getInternalSavedObjectsClient(core.setup), | ||
| listConfigurations({ setup }), | ||
| getApmPackgePolicies({ | ||
| core, | ||
| fleetPluginStart, | ||
| }), | ||
| ]); | ||
|
|
||
| return Promise.all( | ||
| packagePolicies.items.map(async (item) => { | ||
| const { id, revision, updated_at, updated_by, ...packagePolicy } = item; // eslint-disable-line @typescript-eslint/naming-convention | ||
| const updatedPackagePolicy = getPackagePolicyWithAgentConfigurations( | ||
| packagePolicy, | ||
| agentConfigurations | ||
| ); | ||
| return fleetPluginStart.packagePolicyService.update( | ||
| (savedObjectsClient as unknown) as SavedObjectsClientContract, | ||
| esClient, | ||
| id, | ||
| updatedPackagePolicy | ||
| ); | ||
| }) | ||
| ); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.