[Fleet] Prevent upgrades of agents already in updating state (#133763) (#133863)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit ba05c2c6a9)

Co-authored-by: Cristina Amico <criamico@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2022-06-08 07:19:27 -04:00 committed by GitHub
parent 65fd21d86d
commit 7dd091a996
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View file

@ -14,11 +14,15 @@ const getAgent = ({
upgradeable = false,
unenrolling = false,
unenrolled = false,
updating = false,
upgraded = false,
}: {
version: string;
upgradeable?: boolean;
unenrolling?: boolean;
unenrolled?: boolean;
updating?: boolean;
upgraded?: boolean;
}): Agent => {
const agent: Agent = {
id: 'de9006e1-54a7-4320-b24e-927e6fe518a8',
@ -94,6 +98,12 @@ const getAgent = ({
if (unenrolled) {
agent.unenrolled_at = '2020-10-01T14:43:27.255Z';
}
if (updating) {
agent.upgrade_started_at = new Date(Date.now()).toISOString();
}
if (upgraded) {
agent.upgraded_at = new Date(Date.now()).toISOString();
}
return agent;
};
describe('Fleet - isAgentUpgradeable', () => {
@ -186,4 +196,14 @@ describe('Fleet - isAgentUpgradeable', () => {
)
).toBe(false);
});
it('returns false if agent reports upgradeable, but is already updating', () => {
expect(
isAgentUpgradeable(getAgent({ version: '7.9.0', upgradeable: true, updating: true }), '8.0.0')
).toBe(false);
});
it('returns true if agent was recently upgraded', () => {
expect(
isAgentUpgradeable(getAgent({ version: '7.9.0', upgradeable: true, upgraded: true }), '8.0.0')
).toBe(true);
});
});

View file

@ -24,6 +24,10 @@ export function isAgentUpgradeable(agent: Agent, kibanaVersion: string, versionT
if (!agent.local_metadata.elastic.agent.upgradeable) {
return false;
}
// check that the agent is not already in the process of updating
if (agent.upgrade_started_at && !agent.upgraded_at) {
return false;
}
if (versionToUpgrade !== undefined) {
return (
isNotDowngrade(agentVersion, versionToUpgrade) &&