Skip to content
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] delete unenrolled agents task #195544

Merged
merged 22 commits into from
Oct 14, 2024

Conversation

juliaElastic
Copy link
Contributor

@juliaElastic juliaElastic commented Oct 9, 2024

Summary

Closes #189506

Testing steps:

  • enable deleting unenrolled agents by adding xpack.fleet.enableDeleteUnenrolledAgents: true to kibana.dev.yml or turn it on on the UI
  • add some unenroll agents with the helper script
cd x-pack/plugins/fleet
node scripts/create_agents/index.js --status unenrolled --count 10

 info Creating 10 agents with statuses:
 info    unenrolled: 10
 info Batch complete, created 10 agent docs, took 0, errors: false
 info All batches complete. Created 10 agents in total. Goodbye!
  • restart kibana or wait for the task to run and verify that the unenrolled agents were deleted
[2024-10-08T16:14:45.152+02:00][DEBUG][plugins.fleet.fleet:delete-unenrolled-agents-task:0.0.5] [DeleteUnenrolledAgentsTask] Executed deletion of 10 unenrolled agents
[2024-10-08T16:14:45.153+02:00][INFO ][plugins.fleet.fleet:delete-unenrolled-agents-task:0.0.5] [DeleteUnenrolledAgentsTask] runTask ended: success

Added to UI settings:
image

If the flag is preconfigured, disabled update on the UI with a tooltip:
image

The update is also prevented from the API:
image

Once the preconfiguration is removed, the UI update is allowed again.

Checklist

@juliaElastic juliaElastic added release_note:feature Makes this part of the condensed release notes backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) labels Oct 9, 2024
@juliaElastic juliaElastic self-assigned this Oct 9, 2024
export const VERSION = '1.0.0';
const TITLE = 'Fleet Delete Unenrolled Agents Task';
const SCOPE = ['fleet'];
const INTERVAL = '1h';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added 1h interval, it should be efficient to delete all unenrolled agents with deleteByQuery, no need to load them in memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a caveat with enabling both preconfig and UI setting, it can happen that the preconfigured value gets out of sync when changed from the UI.
We could disable changing from UI when preconfigured if this is an issue.

@juliaElastic juliaElastic marked this pull request as ready for review October 9, 2024 09:52
@juliaElastic juliaElastic requested review from a team as code owners October 9, 2024 09:52
… src/core/server/integration_tests/ci_checks'
@botelastic botelastic bot added the Team:Fleet Team label for Observability Data Collection Fleet team label Oct 9, 2024
@elasticmachine
Copy link
Contributor

Pinging @elastic/fleet (Team:Fleet)

kibanamachine and others added 3 commits October 9, 2024 11:13
…t --include-path /api/status --include-path /api/alerting/rule/ --include-path /api/alerting/rules --include-path /api/actions --include-path /api/security/role --include-path /api/spaces --include-path /api/fleet --update'
@nchaulet nchaulet self-requested a review October 9, 2024 13:28
@juliaElastic juliaElastic requested a review from a team as a code owner October 9, 2024 14:21
@@ -195,6 +199,12 @@ async function createSetupSideEffects(
logger.debug('Setting up Space settings');
await ensureSpaceSettings(appContextService.getConfig()?.spaceSettings ?? []);

logger.debug('Setting up delete unenrolled agents setting');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to enable it then disable it via the kibana config and it stay enabled, it is expected? in my opinion if that settings is managed via the kibana config it should probably be the only source of truth

xpack.fleet.enableDeleteUnenrolledAgents: false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, it was a bug, it is fixed now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it's clearer to have one single source of truth. Are we ok to keep this setting in preconfiguration only, and remove from the UI @kpollich @nimarezainia ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, similar to most of the configuration we should allow to configure it both from UI or kibana config, but if it's configured in preconfiguration this should become the only way to configure it (until it's removed from the config)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case I have to introduce an is_preconfigured flag to know if the delete flag was preconfigured.

this.logger.debug(`[DeleteUnenrolledAgentsTask] Fetching unenrolled agents`);

const response = await esClient.deleteByQuery({
index: AGENTS_INDEX,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the abortcontroller be passed into this request so the request is cancelled if aborted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for raising, fixed

@@ -181,6 +182,16 @@ export const getSavedObjectTypes = (
},
],
},
3: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: @juliaElastic this SO type (GLOBAL_SETTINGS_SAVED_OBJECT_TYPE) isn't registered as encrypted saved object, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it's not an encrypted SO.

Copy link
Member

@azasypkin azasypkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, since the added migration is for the ingest_manager_settings SO type, which isn’t an encrypted saved object.

Copy link
Member

@nchaulet nchaulet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally and it worked as expected, LGTM 🚀

Copy link
Contributor

@jloleysens jloleysens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API and mappings changes LGTM

Copy link
Contributor

@ymao1 ymao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Response Ops changes LGTM. Reviewed for new recurring task type that runs every hour with a timeout of 1 minute and respects cancellation signals.

juliaElastic and others added 6 commits October 10, 2024 14:06
…t --include-path /api/status --include-path /api/alerting/rule/ --include-path /api/alerting/rules --include-path /api/actions --include-path /api/security/role --include-path /api/spaces --include-path /api/fleet --update'
@elasticmachine
Copy link
Contributor

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #51 / discover/esql discover esql view switch modal should not show switch modal when switching to a data view while a saved search is open

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
fleet 1210 1211 +1

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
fleet 1.7MB 1.7MB +2.4KB

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
fleet 171.6KB 171.6KB +38.0B

History

cc @juliaElastic

@juliaElastic juliaElastic merged commit c53b2a8 into elastic:main Oct 14, 2024
28 checks passed
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.x

https://github.com/elastic/kibana/actions/runs/11324350540

@kibanamachine
Copy link
Contributor

💔 All backports failed

Status Branch Result
8.x Backport failed because of merge conflicts

Manual backport

To create the backport manually run:

node scripts/backport --pr 195544

Questions ?

Please refer to the Backport tool documentation

juliaElastic added a commit to juliaElastic/kibana that referenced this pull request Oct 14, 2024
Closes elastic#189506

Testing steps:
- enable deleting unenrolled agents by adding
`xpack.fleet.enableDeleteUnenrolledAgents: true` to `kibana.dev.yml` or
turn it on on the UI
- add some unenroll agents with the helper script
```
cd x-pack/plugins/fleet
node scripts/create_agents/index.js --status unenrolled --count 10

 info Creating 10 agents with statuses:
 info    unenrolled: 10
 info Batch complete, created 10 agent docs, took 0, errors: false
 info All batches complete. Created 10 agents in total. Goodbye!
```
- restart kibana or wait for the task to run and verify that the
unenrolled agents were deleted
```
[2024-10-08T16:14:45.152+02:00][DEBUG][plugins.fleet.fleet:delete-unenrolled-agents-task:0.0.5] [DeleteUnenrolledAgentsTask] Executed deletion of 10 unenrolled agents
[2024-10-08T16:14:45.153+02:00][INFO ][plugins.fleet.fleet:delete-unenrolled-agents-task:0.0.5] [DeleteUnenrolledAgentsTask] runTask ended: success
```

Added to UI settings:
<img width="1057" alt="image"
src="https://github.com/user-attachments/assets/2c9279f9-86a8-4630-a6cd-5aaa42e05fe7">

If the flag is preconfigured, disabled update on the UI with a tooltip:
<img width="1009" alt="image"
src="https://github.com/user-attachments/assets/45041020-6447-4295-995e-6848f0238f88">

The update is also prevented from the API:
<img width="2522" alt="image"
src="https://github.com/user-attachments/assets/cfbc8e21-e062-4e7f-9d08-9767fa387752">

Once the preconfiguration is removed, the UI update is allowed again.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: kibanamachine <[email protected]>
juliaElastic added a commit that referenced this pull request Oct 14, 2024
Backport #195544 to 8.x

---------

Co-authored-by: kibanamachine <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) release_note:feature Makes this part of the condensed release notes Team:Fleet Team label for Observability Data Collection Fleet team v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Fleet] Clean up documents in .fleet* indices related to agents that were already unenrolled
7 participants