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] display current upgrades #132379

Merged
merged 4 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions x-pack/plugins/fleet/common/services/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ export const agentRouteService = {
getUpgradePath: (agentId: string) =>
AGENT_API_ROUTES.UPGRADE_PATTERN.replace('{agentId}', agentId),
getBulkUpgradePath: () => AGENT_API_ROUTES.BULK_UPGRADE_PATTERN,
getCurrentUpgradesPath: () => AGENT_API_ROUTES.CURRENT_UPGRADES_PATTERN,
getCancelActionPath: (actionId: string) =>
AGENT_API_ROUTES.CANCEL_ACTIONS_PATTERN.replace('{actionId}', actionId),
getListPath: () => AGENT_API_ROUTES.LIST_PATTERN,
getStatusPath: () => AGENT_API_ROUTES.STATUS_PATTERN,
getIncomingDataPath: () => AGENT_API_ROUTES.DATA_PATTERN,
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/common/types/models/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export interface CurrentUpgrade {
complete: boolean;
nbAgents: number;
nbAgentsAck: number;
version: string;
}

// Generated from FleetServer schema.json
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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, { useCallback, useState } from 'react';
import { FormattedMessage } from '@kbn/i18n-react';
import {
EuiCallOut,
EuiLink,
EuiFlexGroup,
EuiFlexItem,
EuiButton,
EuiLoadingSpinner,
} from '@elastic/eui';

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

export interface CurrentBulkUpgradeCalloutProps {
currentUpgrade: CurrentUpgrade;
abortUpgrade: (currentUpgrade: CurrentUpgrade) => Promise<void>;
}

export const CurrentBulkUpgradeCallout: React.FunctionComponent<CurrentBulkUpgradeCalloutProps> = ({
currentUpgrade,
abortUpgrade,
}) => {
const { docLinks } = useStartServices();
const [isAborting, setIsAborting] = useState(false);
const onClickAbortUpgrade = useCallback(async () => {
try {
setIsAborting(true);
await abortUpgrade(currentUpgrade);
} finally {
setIsAborting(false);
}
}, [currentUpgrade, abortUpgrade]);

return (
<EuiCallOut color="primary">
<EuiFlexGroup
className="euiCallOutHeader__title"
justifyContent="spaceBetween"
alignItems="center"
gutterSize="none"
>
<EuiFlexItem grow={false}>
<div>
<EuiLoadingSpinner />
&nbsp;&nbsp;
<FormattedMessage
id="xpack.fleet.currentUpgrade.calloutTitle"
defaultMessage="Upgrading {nbAgents} agents to version {version}"
values={{
nbAgents: currentUpgrade.nbAgents - currentUpgrade.nbAgentsAck,
version: currentUpgrade.version,
}}
/>
</div>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiButton size="s" onClick={onClickAbortUpgrade} isLoading={isAborting}>
<FormattedMessage
id="xpack.fleet.currentUpgrade.abortUpgradeButtom"
defaultMessage="Abort upgrade"
/>
</EuiButton>
</EuiFlexItem>
</EuiFlexGroup>
<FormattedMessage
id="xpack.fleet.currentUpgrade.calloutDescription"
defaultMessage="For more information see the {guideLink}."
values={{
guideLink: (
<EuiLink href={docLinks.links.fleet.fleetServerAddFleetServer} target="_blank" external>
<FormattedMessage
id="xpack.fleet.currentUpgrade.guideLink"
defaultMessage="Fleet and Elastic Agent Guide"
/>
</EuiLink>
),
}}
/>
</EuiCallOut>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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.
*/

export { CurrentBulkUpgradeCallout } from './current_bulk_upgrade_callout';
export type { CurrentBulkUpgradeCalloutProps } from './current_bulk_upgrade_callout';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/

export { useCurrentUpgrades } from './use_current_upgrades';
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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 { useCallback, useEffect, useRef, useState } from 'react';
import { i18n } from '@kbn/i18n';

import { sendGetCurrentUpgrades, sendPostCancelAction, useStartServices } from '../../../../hooks';

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

const POLL_INTERVAL = 30 * 1000;

export function useCurrentUpgrades() {
const [currentUpgrades, setCurrentUpgrades] = useState<CurrentUpgrade[]>([]);
const currentTimeoutRef = useRef<NodeJS.Timeout>();
const isCancelledRef = useRef<boolean>(false);
const { notifications, overlays } = useStartServices();

const refreshUpgrades = useCallback(async () => {
try {
const res = await sendGetCurrentUpgrades();
if (isCancelledRef.current) {
return;
}
if (res.error) {
throw res.error;
}

if (!res.data) {
throw new Error('No data');
}

setCurrentUpgrades(res.data.items);
} catch (err) {
notifications.toasts.addError(err, {
title: i18n.translate('xpack.fleet.currentUpgrade.fetchRequestError', {
defaultMessage: 'An error happened while fetching current upgrades',
}),
});
}
}, [notifications.toasts]);

const abortUpgrade = useCallback(
async (currentUpgrade: CurrentUpgrade) => {
try {
const confirmRes = await overlays.openConfirm(
i18n.translate('xpack.fleet.currentUpgrade.confirmDescription', {
defaultMessage: 'This action will abort upgrade of {nbAgents} agents',
values: {
nbAgents: currentUpgrade.nbAgents - currentUpgrade.nbAgentsAck,
},
}),
{
title: i18n.translate('xpack.fleet.currentUpgrade.confirmTitle', {
defaultMessage: 'Abort upgrade?',
}),
}
);

if (!confirmRes) {
return;
}
await sendPostCancelAction(currentUpgrade.actionId);
await refreshUpgrades();
} catch (err) {
notifications.toasts.addError(err, {
title: i18n.translate('xpack.fleet.currentUpgrade.abortRequestError', {
defaultMessage: 'An error happened while aborting upgrade',
}),
});
}
},
[refreshUpgrades, notifications.toasts, overlays]
);

// Poll for upgrades
useEffect(() => {
isCancelledRef.current = false;

async function pollData() {
await refreshUpgrades();
if (isCancelledRef.current) {
return;
}
currentTimeoutRef.current = setTimeout(() => pollData(), POLL_INTERVAL);
}

pollData();

return () => {
isCancelledRef.current = true;

if (currentTimeoutRef.current) {
clearTimeout(currentTimeoutRef.current);
}
};
}, [refreshUpgrades]);

return {
currentUpgrades,
refreshUpgrades,
abortUpgrade,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ import {
} from '../components';
import { useFleetServerUnhealthy } from '../hooks/use_fleet_server_unhealthy';

import { CurrentBulkUpgradeCallout } from './components';
import { AgentTableHeader } from './components/table_header';
import type { SelectionMode } from './components/types';
import { SearchAndFilterBar } from './components/search_and_filter_bar';
import { Tags } from './components/tags';
import { TableRowActions } from './components/table_row_actions';
import { EmptyPrompt } from './components/empty_prompt';
import { useCurrentUpgrades } from './hooks';

const REFRESH_INTERVAL_MS = 30000;

Expand Down Expand Up @@ -335,6 +337,9 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
flyoutContext.openFleetServerFlyout();
}, [flyoutContext]);

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

const columns = [
{
field: 'local_metadata.host.hostname',
Expand Down Expand Up @@ -490,7 +495,6 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
/>
</EuiPortal>
)}

{agentToUpgrade && (
<EuiPortal>
<AgentUpgradeAgentModal
Expand All @@ -499,12 +503,12 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
onClose={() => {
setAgentToUpgrade(undefined);
fetchData();
refreshUpgrades();
}}
version={kibanaVersion}
/>
</EuiPortal>
)}

{isFleetServerUnhealthy && (
<>
{cloud?.deploymentUrl ? (
Expand All @@ -515,7 +519,13 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
<EuiSpacer size="l" />
</>
)}

{/* Current upgrades callout */}
{currentUpgrades.map((currentUpgrade) => (
<React.Fragment key={currentUpgrade.actionId}>
<CurrentBulkUpgradeCallout currentUpgrade={currentUpgrade} abortUpgrade={abortUpgrade} />
<EuiSpacer size="l" />
</React.Fragment>
))}
{/* Search and filter bar */}
<SearchAndFilterBar
agentPolicies={agentPolicies}
Expand All @@ -539,7 +549,6 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
refreshAgents={() => fetchData()}
/>
<EuiSpacer size="m" />

{/* Agent total, bulk actions and status bar */}
<AgentTableHeader
showInactive={showInactive}
Expand All @@ -557,7 +566,6 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
}}
/>
<EuiSpacer size="s" />

{/* Agent list table */}
<EuiBasicTable<Agent>
ref={tableRef}
Expand Down
15 changes: 15 additions & 0 deletions x-pack/plugins/fleet/public/hooks/use_request/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type {
PostBulkAgentUpgradeResponse,
PostNewAgentActionRequest,
PostNewAgentActionResponse,
GetCurrentUpgradesResponse,
} from '../../types';

import { useRequest, sendRequest } from './use_request';
Expand Down Expand Up @@ -177,3 +178,17 @@ export function sendPostBulkAgentUpgrade(
...options,
});
}

export function sendGetCurrentUpgrades() {
return sendRequest<GetCurrentUpgradesResponse>({
path: agentRouteService.getCurrentUpgradesPath(),
method: 'get',
});
}

export function sendPostCancelAction(actionId: string) {
return sendRequest<GetCurrentUpgradesResponse>({
path: agentRouteService.getCancelActionPath(actionId),
method: 'post',
});
}
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/public/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type {
Output,
DataStream,
Settings,
CurrentUpgrade,
GetFleetStatusResponse,
GetAgentPoliciesRequest,
GetAgentPoliciesResponse,
Expand Down Expand Up @@ -77,6 +78,7 @@ export type {
PostEnrollmentAPIKeyResponse,
PostLogstashApiKeyResponse,
GetOutputsResponse,
GetCurrentUpgradesResponse,
PutOutputRequest,
PutOutputResponse,
PostOutputRequest,
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/server/services/agents/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ async function _getUpgradeActions(esClient: ElasticsearchClient, now = new Date(
nbAgents: 0,
complete: false,
nbAgentsAck: 0,
version: hit._source.data?.version as string,
};
}

Expand Down