Skip to content

Commit 9e9f9ac

Browse files
kibanamachinemrodm
andauthored
[9.0] Fix unenroll inactive agent tasks if first set of agents returned is equal to UNENROLLMENT_BATCH_SIZE (elastic#216283) (elastic#216482)
# Backport This will backport the following commits from `main` to `9.0`: - [Fix unenroll inactive agent tasks if first set of agents returned is equal to UNENROLLMENT_BATCH_SIZE (elastic#216283)](elastic#216283) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Mario Rodriguez Molins","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-03-31T13:55:14Z","message":"Fix unenroll inactive agent tasks if first set of agents returned is equal to UNENROLLMENT_BATCH_SIZE (elastic#216283)\n\n## Summary\n\nFix the unenrollment inactive agent task to be able to unenroll some\nagents when the number of agents returned by the first\n`getAgentsByKuery` trigger is equal to the limit of\n`UNENROLLMENT_BATCH_SIZE`.\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [ ] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n---------\n\nCo-authored-by: Elastic Machine <[email protected]>","sha":"8728a2328217408e0a377f037486abbc459acd2d","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Fleet","backport:prev-minor","backport:prev-major","v9.1.0"],"title":"Fix unenroll inactive agent tasks if first set of agents returned is equal to UNENROLLMENT_BATCH_SIZE","number":216283,"url":"https://github.com/elastic/kibana/pull/216283","mergeCommit":{"message":"Fix unenroll inactive agent tasks if first set of agents returned is equal to UNENROLLMENT_BATCH_SIZE (elastic#216283)\n\n## Summary\n\nFix the unenrollment inactive agent task to be able to unenroll some\nagents when the number of agents returned by the first\n`getAgentsByKuery` trigger is equal to the limit of\n`UNENROLLMENT_BATCH_SIZE`.\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [ ] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n---------\n\nCo-authored-by: Elastic Machine <[email protected]>","sha":"8728a2328217408e0a377f037486abbc459acd2d"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/216283","number":216283,"mergeCommit":{"message":"Fix unenroll inactive agent tasks if first set of agents returned is equal to UNENROLLMENT_BATCH_SIZE (elastic#216283)\n\n## Summary\n\nFix the unenrollment inactive agent task to be able to unenroll some\nagents when the number of agents returned by the first\n`getAgentsByKuery` trigger is equal to the limit of\n`UNENROLLMENT_BATCH_SIZE`.\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [ ] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n---------\n\nCo-authored-by: Elastic Machine <[email protected]>","sha":"8728a2328217408e0a377f037486abbc459acd2d"}}]}] BACKPORT--> Co-authored-by: Mario Rodriguez Molins <[email protected]>
1 parent 4f37bc0 commit 9e9f9ac

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

x-pack/platform/plugins/shared/fleet/server/tasks/unenroll_inactive_agents_task.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ describe('UnenrollInactiveAgentsTask', () => {
5757
let mockTaskManagerSetup: jest.Mocked<TaskManagerSetupContract>;
5858
const mockedUnenrollBatch = jest.mocked(unenrollBatch);
5959

60+
const unenrollBatchSize = 3;
6061
const agents = [
6162
{
6263
id: 'agent-1',
@@ -91,6 +92,7 @@ describe('UnenrollInactiveAgentsTask', () => {
9192
core: mockCore,
9293
taskManager: mockTaskManagerSetup,
9394
logFactory: loggingSystemMock.create(),
95+
unenrollBatchSize,
9496
});
9597
});
9698

x-pack/platform/plugins/shared/fleet/server/tasks/unenroll_inactive_agents_task.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ interface UnenrollInactiveAgentsTaskSetupContract {
4040
core: CoreSetup;
4141
taskManager: TaskManagerSetupContract;
4242
logFactory: LoggerFactory;
43+
unenrollBatchSize?: number;
4344
}
4445

4546
interface UnenrollInactiveAgentsTaskStartContract {
@@ -50,10 +51,13 @@ export class UnenrollInactiveAgentsTask {
5051
private logger: Logger;
5152
private wasStarted: boolean = false;
5253
private abortController = new AbortController();
54+
private unenrollBatchSize: number;
5355

5456
constructor(setupContract: UnenrollInactiveAgentsTaskSetupContract) {
55-
const { core, taskManager, logFactory } = setupContract;
57+
const { core, taskManager, logFactory, unenrollBatchSize } = setupContract;
5658
this.logger = logFactory.get(this.taskId);
59+
this.unenrollBatchSize =
60+
unenrollBatchSize !== undefined ? unenrollBatchSize : UNENROLLMENT_BATCHSIZE;
5761

5862
taskManager.registerTaskDefinitions({
5963
[TYPE]: {
@@ -140,14 +144,14 @@ export class UnenrollInactiveAgentsTask {
140144
kuery,
141145
showInactive: true,
142146
page: 1,
143-
perPage: UNENROLLMENT_BATCHSIZE,
147+
perPage: this.unenrollBatchSize,
144148
});
145149
if (!res.agents.length) {
146150
this.endRun('No inactive agents to unenroll');
147151
return;
148152
}
149153
agentCounter += res.agents.length;
150-
if (agentCounter >= UNENROLLMENT_BATCHSIZE) {
154+
if (agentCounter > this.unenrollBatchSize) {
151155
this.endRun('Reached the maximum amount of agents to unenroll, exiting.');
152156
return;
153157
}

0 commit comments

Comments
 (0)