-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fleet] Implement active agent soft limit (#161289)
- Loading branch information
Showing
11 changed files
with
168 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...pplications/fleet/sections/agents/agent_list_page/components/agent_soft_limit_callout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { EuiCallOut } from '@elastic/eui'; | ||
import { FormattedMessage, FormattedNumber } from '@kbn/i18n-react'; | ||
|
||
import { useConfig } from '../../../../hooks'; | ||
|
||
export const AgentSoftLimitCallout = () => { | ||
const config = useConfig(); | ||
|
||
return ( | ||
<EuiCallOut | ||
iconType="warning" | ||
color="warning" | ||
title={ | ||
<FormattedMessage | ||
id="xpack.fleet.agentSoftLimitCallout.calloutTitle" | ||
defaultMessage="Max number of online agents reached" | ||
/> | ||
} | ||
> | ||
<FormattedMessage | ||
id="xpack.fleet.agentSoftLimitCallout.calloutDescription" | ||
defaultMessage="Fleet supports a maximum of {nbAgents} active agents. You need to unenroll some agents to ensure that all active agents are able to connect and new agents can be enrolled." | ||
values={{ | ||
nbAgents: <FormattedNumber value={config.internal?.activeAgentsSoftLimit ?? 25000} />, | ||
}} | ||
/> | ||
</EuiCallOut> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...ic/applications/fleet/sections/agents/agent_list_page/hooks/use_agent_soft_limit.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { createFleetTestRendererMock } from '../../../../../../mock'; | ||
import { useConfig, sendGetAgents } from '../../../../hooks'; | ||
|
||
import { useAgentSoftLimit } from './use_agent_soft_limit'; | ||
|
||
jest.mock('../../../../hooks'); | ||
|
||
const mockedSendGetAgents = jest.mocked(sendGetAgents); | ||
const mockedUseConfig = jest.mocked(useConfig); | ||
|
||
describe('useAgentSoftLimit', () => { | ||
beforeEach(() => { | ||
mockedSendGetAgents.mockReset(); | ||
mockedUseConfig.mockReset(); | ||
}); | ||
it('should return shouldDisplayAgentSoftLimit:false if soft limit is not enabled in config', async () => { | ||
const renderer = createFleetTestRendererMock(); | ||
mockedUseConfig.mockReturnValue({} as any); | ||
const { result } = renderer.renderHook(() => useAgentSoftLimit()); | ||
|
||
expect(result.current.shouldDisplayAgentSoftLimit).toEqual(false); | ||
|
||
expect(mockedSendGetAgents).not.toBeCalled(); | ||
}); | ||
|
||
it('should return shouldDisplayAgentSoftLimit:false if soft limit is enabled in config and there is less online agents than the limit', async () => { | ||
const renderer = createFleetTestRendererMock(); | ||
mockedUseConfig.mockReturnValue({ internal: { activeAgentsSoftLimit: 10 } } as any); | ||
mockedSendGetAgents.mockResolvedValue({ | ||
data: { | ||
total: 5, | ||
}, | ||
} as any); | ||
const { result, waitForNextUpdate } = renderer.renderHook(() => useAgentSoftLimit()); | ||
await waitForNextUpdate(); | ||
|
||
expect(mockedSendGetAgents).toBeCalled(); | ||
expect(result.current.shouldDisplayAgentSoftLimit).toEqual(false); | ||
}); | ||
|
||
it('should return shouldDisplayAgentSoftLimit:true if soft limit is enabled in config and there is more online agents than the limit', async () => { | ||
const renderer = createFleetTestRendererMock(); | ||
mockedUseConfig.mockReturnValue({ internal: { activeAgentsSoftLimit: 10 } } as any); | ||
mockedSendGetAgents.mockResolvedValue({ | ||
data: { | ||
total: 15, | ||
}, | ||
} as any); | ||
const { result, waitForNextUpdate } = renderer.renderHook(() => useAgentSoftLimit()); | ||
await waitForNextUpdate(); | ||
|
||
expect(mockedSendGetAgents).toBeCalled(); | ||
expect(result.current.shouldDisplayAgentSoftLimit).toEqual(true); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
.../public/applications/fleet/sections/agents/agent_list_page/hooks/use_agent_soft_limit.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { useConfig, sendGetAgents } from '../../../../hooks'; | ||
|
||
async function fetchTotalOnlineAgents() { | ||
const response = await sendGetAgents({ | ||
kuery: 'status:online', | ||
perPage: 0, | ||
showInactive: false, | ||
}); | ||
|
||
if (response.error) { | ||
throw new Error(response.error.message); | ||
} | ||
|
||
return response.data?.total ?? 0; | ||
} | ||
|
||
export function useAgentSoftLimit() { | ||
const config = useConfig(); | ||
|
||
const softLimit = config.internal?.activeAgentsSoftLimit; | ||
|
||
const { data: totalAgents } = useQuery(['fetch-total-online-agents'], fetchTotalOnlineAgents, { | ||
enabled: softLimit !== undefined, | ||
}); | ||
|
||
return { | ||
shouldDisplayAgentSoftLimit: softLimit && totalAgents ? totalAgents > softLimit : false, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters