[8.16] Fix unenroll inactive agent tasks if first set of agents returned is equal to UNENROLLMENT_BATCH_SIZE (#216283) (#216478)

# Backport

This will backport the following commits from `main` to `8.16`:
- [Fix unenroll inactive agent tasks if first set of agents returned is
equal to UNENROLLMENT_BATCH_SIZE
(#216283)](https://github.com/elastic/kibana/pull/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":"mario.rodriguez@elastic.co"},"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 (#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
<elasticmachine@users.noreply.github.com>","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 (#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
<elasticmachine@users.noreply.github.com>","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 (#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
<elasticmachine@users.noreply.github.com>","sha":"8728a2328217408e0a377f037486abbc459acd2d"}}]}]
BACKPORT-->

Co-authored-by: Mario Rodriguez Molins <mario.rodriguez@elastic.co>
This commit is contained in:
Kibana Machine 2025-03-31 18:27:08 +02:00 committed by GitHub
parent a646c34ebe
commit 2a762a4304
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 3 deletions

View file

@ -57,6 +57,7 @@ describe('UnenrollInactiveAgentsTask', () => {
let mockTaskManagerSetup: jest.Mocked<TaskManagerSetupContract>;
const mockedUnenrollBatch = jest.mocked(unenrollBatch);
const unenrollBatchSize = 3;
const agents = [
{
id: 'agent-1',
@ -91,6 +92,7 @@ describe('UnenrollInactiveAgentsTask', () => {
core: mockCore,
taskManager: mockTaskManagerSetup,
logFactory: loggingSystemMock.create(),
unenrollBatchSize,
});
});

View file

@ -40,6 +40,7 @@ interface UnenrollInactiveAgentsTaskSetupContract {
core: CoreSetup;
taskManager: TaskManagerSetupContract;
logFactory: LoggerFactory;
unenrollBatchSize?: number;
}
interface UnenrollInactiveAgentsTaskStartContract {
@ -50,10 +51,13 @@ export class UnenrollInactiveAgentsTask {
private logger: Logger;
private wasStarted: boolean = false;
private abortController = new AbortController();
private unenrollBatchSize: number;
constructor(setupContract: UnenrollInactiveAgentsTaskSetupContract) {
const { core, taskManager, logFactory } = setupContract;
const { core, taskManager, logFactory, unenrollBatchSize } = setupContract;
this.logger = logFactory.get(this.taskId);
this.unenrollBatchSize =
unenrollBatchSize !== undefined ? unenrollBatchSize : UNENROLLMENT_BATCHSIZE;
taskManager.registerTaskDefinitions({
[TYPE]: {
@ -140,14 +144,14 @@ export class UnenrollInactiveAgentsTask {
kuery,
showInactive: true,
page: 1,
perPage: UNENROLLMENT_BATCHSIZE,
perPage: this.unenrollBatchSize,
});
if (!res.agents.length) {
this.endRun('No inactive agents to unenroll');
return;
}
agentCounter += res.agents.length;
if (agentCounter >= UNENROLLMENT_BATCHSIZE) {
if (agentCounter > this.unenrollBatchSize) {
this.endRun('Reached the maximum amount of agents to unenroll, exiting.');
return;
}