-
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.
Browse files
Browse the repository at this point in the history
…01685) * [APM] Syncs agent config settings to APM Fleet policies (#95501) * fixes eslint issues * fixes malformed line comment * - consolidated logic that applies agent configurations to package policy objects - update package policy agent_configs to include etag, agent.name, and change settings -> config * Synchronizes agent configs whenever configuration is deleted. * PR feedback * nest agent_config within `apm-server` in the package policy input * nests agent_config under the requried 'value' property of config['apm-server'] in order to pass validation checks * - externalizes getApmPackagePolicies for reusability - parallelizes operations for improved performance Co-authored-by: Kibana Machine <[email protected]> Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
7b6c599
commit 8a21bd5
Showing
8 changed files
with
286 additions
and
17 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
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 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 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', | ||
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]: { | ||
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 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 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
Oops, something went wrong.