-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[Fleet] Run agent policy schema in batches during fleet setup + add xpack.fleet.setup.agentPolicySchemaUpgradeBatchSize
config
#150688
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -315,15 +315,22 @@ class AgentPolicyService { | |
soClient: SavedObjectsClientContract, | ||
options: ListWithKuery & { | ||
withPackagePolicies?: boolean; | ||
fields?: string[]; | ||
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. added the ability to restrict the agent policy fields returned to reduce payload size. |
||
} | ||
): Promise<{ items: AgentPolicy[]; total: number; page: number; perPage: number }> { | ||
): Promise<{ | ||
items: AgentPolicy[]; | ||
total: number; | ||
page: number; | ||
perPage: number; | ||
}> { | ||
const { | ||
page = 1, | ||
perPage = 20, | ||
sortField = 'updated_at', | ||
sortOrder = 'desc', | ||
kuery, | ||
withPackagePolicies = false, | ||
fields, | ||
} = options; | ||
|
||
const baseFindParams = { | ||
|
@@ -332,6 +339,7 @@ class AgentPolicyService { | |
sortOrder, | ||
page, | ||
perPage, | ||
...(fields ? { fields } : {}), | ||
}; | ||
const filter = kuery ? normalizeKuery(SAVED_OBJECT_TYPE, kuery) : undefined; | ||
let agentPoliciesSO; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,14 +10,16 @@ import type { SavedObjectsClientContract } from '@kbn/core/server'; | |
import { | ||
AGENT_POLICY_SAVED_OBJECT_TYPE, | ||
FLEET_AGENT_POLICIES_SCHEMA_VERSION, | ||
SO_SEARCH_LIMIT, | ||
} from '../../constants'; | ||
import { agentPolicyService } from '../agent_policy'; | ||
import { appContextService } from '../app_context'; | ||
|
||
function getOutdatedAgentPoliciesBatch(soClient: SavedObjectsClientContract) { | ||
const DEFAULT_BATCH_SIZE = 100; | ||
function getOutdatedAgentPoliciesBatch(soClient: SavedObjectsClientContract, batchSize: number) { | ||
return agentPolicyService.list(soClient, { | ||
perPage: SO_SEARCH_LIMIT, | ||
perPage: batchSize, | ||
kuery: `NOT ${AGENT_POLICY_SAVED_OBJECT_TYPE}.schema_version:${FLEET_AGENT_POLICIES_SCHEMA_VERSION}`, | ||
fields: ['id'], // we only need the ID of the agent policy | ||
}); | ||
} | ||
|
||
|
@@ -26,13 +28,23 @@ function getOutdatedAgentPoliciesBatch(soClient: SavedObjectsClientContract) { | |
// deploy outdated policies to .fleet-policies index | ||
// bump oudated SOs schema_version | ||
export async function upgradeAgentPolicySchemaVersion(soClient: SavedObjectsClientContract) { | ||
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. Great improvements! 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. It does take a long time (e.g 50s per 1000 agent policies in my local dev env) but its such an expensive operation I didn't dare put the default higher at risk of overwhelming elastic and kibana. My reasoning was that I suspect the vast majority of users have less than 100 agent policies anyway. |
||
let outdatedAgentPolicies = await getOutdatedAgentPoliciesBatch(soClient); | ||
const config = appContextService.getConfig(); | ||
const logger = appContextService.getLogger(); | ||
|
||
const batchSize = config?.setup?.agentPolicySchemaUpgradeBatchSize ?? DEFAULT_BATCH_SIZE; | ||
let outdatedAgentPolicies = await getOutdatedAgentPoliciesBatch(soClient, batchSize); | ||
logger.debug(`Found ${outdatedAgentPolicies.total} outdated agent policies`); | ||
while (outdatedAgentPolicies.total > 0) { | ||
const start = Date.now(); | ||
const outdatedAgentPolicyIds = outdatedAgentPolicies.items.map( | ||
(outdatedAgentPolicy) => outdatedAgentPolicy.id | ||
); | ||
await agentPolicyService.deployPolicies(soClient, outdatedAgentPolicyIds); | ||
outdatedAgentPolicies = await getOutdatedAgentPoliciesBatch(soClient); | ||
outdatedAgentPolicies = await getOutdatedAgentPoliciesBatch(soClient, batchSize); | ||
logger.debug( | ||
`Upgraded ${outdatedAgentPolicyIds.length} agent policies in ${Date.now() - start}ms, ${ | ||
outdatedAgentPolicies.total | ||
} remaining` | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is just changes to the test script to make creating envs with lots of agent policies easier.