Skip to content

Commit

Permalink
[Fleet] Fix rolling upgrade CANCEL and UI fixes (elastic#132625)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed May 20, 2022
1 parent d9f141a commit f70b4af
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { sendGetCurrentUpgrades, sendPostCancelAction, useStartServices } from '

import type { CurrentUpgrade } from '../../../../types';

const POLL_INTERVAL = 30 * 1000;
const POLL_INTERVAL = 2 * 60 * 1000; // 2 minutes

export function useCurrentUpgrades() {
export function useCurrentUpgrades(onAbortSuccess: () => void) {
const [currentUpgrades, setCurrentUpgrades] = useState<CurrentUpgrade[]>([]);
const currentTimeoutRef = useRef<NodeJS.Timeout>();
const isCancelledRef = useRef<boolean>(false);
Expand Down Expand Up @@ -65,7 +65,7 @@ export function useCurrentUpgrades() {
return;
}
await sendPostCancelAction(currentUpgrade.actionId);
await refreshUpgrades();
await Promise.all([refreshUpgrades(), onAbortSuccess()]);
} catch (err) {
notifications.toasts.addError(err, {
title: i18n.translate('xpack.fleet.currentUpgrade.abortRequestError', {
Expand All @@ -74,7 +74,7 @@ export function useCurrentUpgrades() {
});
}
},
[refreshUpgrades, notifications.toasts, overlays]
[refreshUpgrades, notifications.toasts, overlays, onAbortSuccess]
);

// Poll for upgrades
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
}, [flyoutContext]);

// Current upgrades
const { abortUpgrade, currentUpgrades, refreshUpgrades } = useCurrentUpgrades();
const { abortUpgrade, currentUpgrades, refreshUpgrades } = useCurrentUpgrades(fetchData);

const columns = [
{
Expand Down Expand Up @@ -545,7 +545,7 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
selectionMode={selectionMode}
currentQuery={kuery}
selectedAgents={selectedAgents}
refreshAgents={() => fetchData()}
refreshAgents={() => Promise.all([fetchData(), refreshUpgrades()])}
/>
<EuiSpacer size="m" />
{/* Agent total, bulk actions and status bar */}
Expand Down
30 changes: 30 additions & 0 deletions x-pack/plugins/fleet/server/services/agents/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import { elasticsearchServiceMock } from '@kbn/core/server/mocks';

import { cancelAgentAction } from './actions';
import { bulkUpdateAgents } from './crud';

jest.mock('./crud');

const mockedBulkUpdateAgents = bulkUpdateAgents as jest.Mock;

describe('Agent actions', () => {
describe('cancelAgentAction', () => {
Expand Down Expand Up @@ -67,5 +72,30 @@ describe('Agent actions', () => {
})
);
});

it('should cancel UPGRADE action', async () => {
const esClient = elasticsearchServiceMock.createInternalClient();
esClient.search.mockResolvedValue({
hits: {
hits: [
{
_source: {
type: 'UPGRADE',
action_id: 'action1',
agents: ['agent1', 'agent2'],
expiration: '2022-05-12T18:16:18.019Z',
},
},
],
},
} as any);
await cancelAgentAction(esClient, 'action1');

expect(mockedBulkUpdateAgents).toBeCalled();
expect(mockedBulkUpdateAgents).toBeCalledWith(expect.anything(), [
expect.objectContaining({ agentId: 'agent1' }),
expect.objectContaining({ agentId: 'agent2' }),
]);
});
});
});
14 changes: 14 additions & 0 deletions x-pack/plugins/fleet/server/services/agents/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import type {
import { AGENT_ACTIONS_INDEX, SO_SEARCH_LIMIT } from '../../../common/constants';
import { AgentActionNotFoundError } from '../../errors';

import { bulkUpdateAgents } from './crud';

const ONE_MONTH_IN_MS = 2592000000;

export async function createAgentAction(
Expand Down Expand Up @@ -131,6 +133,18 @@ export async function cancelAgentAction(esClient: ElasticsearchClient, actionId:
created_at: now,
expiration: hit._source.expiration,
});
if (hit._source.type === 'UPGRADE') {
await bulkUpdateAgents(
esClient,
hit._source.agents.map((agentId) => ({
agentId,
data: {
upgraded_at: null,
upgrade_started_at: null,
},
}))
);
}
}

return {
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/server/services/agents/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ async function _getCancelledActionId(
) {
const res = await esClient.search<FleetServerAgentAction>({
index: AGENT_ACTIONS_INDEX,
ignore_unavailable: true,
query: {
bool: {
must: [
Expand Down Expand Up @@ -296,6 +297,7 @@ async function _getCancelledActionId(
async function _getUpgradeActions(esClient: ElasticsearchClient, now = new Date().toISOString()) {
const res = await esClient.search<FleetServerAgentAction>({
index: AGENT_ACTIONS_INDEX,
ignore_unavailable: true,
query: {
bool: {
must: [
Expand Down

0 comments on commit f70b4af

Please sign in to comment.