[8.x](backport #6568) Fix Fleet Enrollment Handling for Containerized Agent #6618
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
This PR introduces a fix for containerized Fleet-managed Elastic Agents to handle scenarios where:
It achieves the above by enhancing the agent's logic inside the
container
cmd to:The PR also adds a Kubernetes integration tests to verify the proper behaviour of enrollment and re-enrollment under various scenarios, including:
Key changes include:
shouldFleetEnroll
to centralize logic for fleet enrollment decisions.agent/{id}/acks
path of Fleet server with empty events to check if the agent API token is still valid. More than happy to introduce a separate path just for this cause on Fleet server, although it will be the same the ACKs one with empty events (as of now at least)You can easily see here in the CI run of the first commit in this PR that the Elastic Agent, before this PR, doesn't handle enrollment correctly and always resorts to using the Fleet token and URL stored in its state. This PR addresses that issue and ensures enrollment uses the correct configuration and token.
PS: the actual changes of this PR are this commit b6596d0 which is
+305 -25
thus I consider this PR aligned with the team policies 🙂Why is it important?
This fix ensures robust and predictable behavior for containerized Fleet-managed Elastic Agents and enhance user experience of managing elastic-agent in Kubernetes.
Checklist
./changelog/fragments
using the changelog toolDisruptive User Impact
The changes introduced in this PR are non-disruptive. They improve fleet enrollment handling and maintain backward compatibility.
How to test this PR locally
Related issues
This is an automatic backport of pull request #6568 done by [Mergify](https://mergify.com).