Skip to content

Commit

Permalink
[8.2] [Fleet] Add missing step showing K8 instructions for managed en…
Browse files Browse the repository at this point in the history
…rollment (#130295) (#130388)

* [Fleet] Add missing step showing K8 instructions for managed enrollment (#130295)

* [Fleet] Add missing step showing K85 instructions for managed enrollment

* Fix checks

* Remove duplicated text and clean up commands

* Fix failing check

(cherry picked from commit 92db72b)

# Conflicts:
#	x-pack/plugins/translations/translations/fr-FR.json
#	x-pack/plugins/translations/translations/ja-JP.json
#	x-pack/plugins/translations/translations/zh-CN.json

* Fix translations
  • Loading branch information
criamico authored Apr 15, 2022
1 parent ee68012 commit b227f03
Show file tree
Hide file tree
Showing 14 changed files with 328 additions and 293 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
* 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, { useEffect, useMemo, useState } from 'react';
import {
EuiText,
EuiButton,
EuiSpacer,
EuiCode,
EuiFlexGroup,
EuiFlexItem,
EuiCopy,
EuiCodeBlock,
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';
import { i18n } from '@kbn/i18n';
import semverMajor from 'semver/functions/major';
import semverMinor from 'semver/functions/minor';
import semverPatch from 'semver/functions/patch';

import { useGetSettings, useKibanaVersion, useStartServices } from '../../hooks';

import { agentPolicyRouteService } from '../../../common';

import { sendGetK8sManifest } from '../../hooks/use_request/k8s';

interface Props {
hasFleetServer: boolean;
isK8s?: string;
enrollmentAPIKey?: string;
}

export const DownloadInstructions: React.FunctionComponent<Props> = ({
hasFleetServer,
isK8s,
enrollmentAPIKey,
}) => {
const kibanaVersion = useKibanaVersion();
const core = useStartServices();
const settings = useGetSettings();
const kibanaVersionURLString = useMemo(
() =>
`${semverMajor(kibanaVersion)}-${semverMinor(kibanaVersion)}-${semverPatch(kibanaVersion)}`,
[kibanaVersion]
);
const { notifications } = core;

const [yaml, setYaml] = useState<string>('');
const [fleetServer, setFleetServer] = useState<string | ''>();

useEffect(() => {
async function fetchK8sManifest() {
try {
if (isK8s !== 'IS_KUBERNETES') {
return;
}
const fleetServerHosts = settings.data?.item.fleet_server_hosts;
let host = '';
if (fleetServerHosts !== undefined && fleetServerHosts.length !== 0) {
setFleetServer(fleetServerHosts[0]);
host = fleetServerHosts[0];
}
const query = { fleetServer: host, enrolToken: enrollmentAPIKey };
const res = await sendGetK8sManifest(query);
if (res.error) {
throw res.error;
}

if (!res.data) {
throw new Error('No data while fetching agent manifest');
}

setYaml(res.data.item);
} catch (error) {
notifications.toasts.addError(error, {
title: i18n.translate('xpack.fleet.agentEnrollment.loadk8sManifestErrorTitle', {
defaultMessage: 'Error while fetching agent manifest',
}),
});
}
}
fetchK8sManifest();
}, [isK8s, notifications.toasts, enrollmentAPIKey, settings.data?.item.fleet_server_hosts]);

const downloadDescription = hasFleetServer ? (
<FormattedMessage
id="xpack.fleet.agentEnrollment.downloadDescriptionForFleetServer"
defaultMessage="Fleet Server runs on an Elastic Agent. Install this agent on a centralized host so that other hosts you wish to monitor can connect to it. In production, we recommend using one or more dedicated hosts. You can download the Elastic Agent binaries and verification signatures from Elastic’s download page."
/>
) : isK8s === 'IS_KUBERNETES' ? (
<FormattedMessage
id="xpack.fleet.agentEnrollment.downloadDescriptionForK8s"
defaultMessage="Copy or download the Kubernetes manifest inside the Kubernetes cluster. Check {FleetUrlVariable} and {FleetTokenVariable} in the Daemonset environment variables and apply the manifest."
values={{
FleetUrlVariable: <EuiCode>FLEET_URL</EuiCode>,
FleetTokenVariable: <EuiCode>FLEET_ENROLLMENT_TOKEN</EuiCode>,
}}
/>
) : (
<FormattedMessage
id="xpack.fleet.agentEnrollment.downloadDescription"
defaultMessage="Install the Elastic Agent on the hosts you wish to monitor. Do not install this agent policy on a host containing Fleet Server. You can download the Elastic Agent binaries and verification signatures from Elastic’s download page."
/>
);

const linuxUsers =
isK8s !== 'IS_KUBERNETES' ? (
<FormattedMessage
id="xpack.fleet.agentEnrollment.downloadUseLinuxInstaller"
defaultMessage="Linux users: We recommend the installer (TAR) over system packages (RPM/DEB) because it lets you upgrade your agent in Fleet."
/>
) : (
''
);

const k8sCopyYaml =
isK8s === 'IS_KUBERNETES' ? (
<EuiCopy textToCopy={yaml}>
{(copy) => (
<EuiButton onClick={copy} iconType="copyClipboard">
<FormattedMessage
id="xpack.fleet.agentEnrollment.copyPolicyButton"
defaultMessage="Copy to clipboard"
/>
</EuiButton>
)}
</EuiCopy>
) : (
''
);

const k8sYaml =
isK8s === 'IS_KUBERNETES' ? (
<EuiCodeBlock language="yaml" style={{ maxHeight: 300 }} fontSize="m">
{yaml}
</EuiCodeBlock>
) : (
''
);

const downloadLink =
isK8s === 'IS_KUBERNETES'
? core.http.basePath.prepend(
`${agentPolicyRouteService.getK8sFullDownloadPath()}?fleetServer=${fleetServer}&enrolToken=${enrollmentAPIKey}`
)
: `https://www.elastic.co/downloads/past-releases/elastic-agent-${kibanaVersionURLString}`;

const downloadMsg =
isK8s === 'IS_KUBERNETES' ? (
<FormattedMessage
id="xpack.fleet.agentEnrollment.downloadManifestButtonk8s"
defaultMessage="Download Manifest"
/>
) : (
<FormattedMessage
id="xpack.fleet.agentEnrollment.downloadLink"
defaultMessage="Go to download page"
/>
);

return (
<>
<EuiText>{downloadDescription}</EuiText>
<EuiSpacer size="s" />
<EuiText size="s">
<>{linuxUsers}</>
</EuiText>
<EuiSpacer size="m" />
<EuiFlexGroup gutterSize="m">
<EuiFlexItem grow={false}>
<EuiButton href={downloadLink} target="_blank" iconSide="right" iconType="popout">
<>{downloadMsg}</>
</EuiButton>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<>{k8sCopyYaml}</>
</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer size="m" />
<>{k8sYaml}</>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/
import React, { useMemo } from 'react';
import { EuiText, EuiLink } from '@elastic/eui';
import { EuiText, EuiLink, EuiSpacer } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';

import semverMajor from 'semver/functions/major';
Expand All @@ -14,7 +14,13 @@ import semverPatch from 'semver/functions/patch';

import { useKibanaVersion } from '../../hooks';

export const InstallationMessage: React.FunctionComponent = () => {
import type { K8sMode } from './types';

interface Props {
isK8s?: K8sMode;
}

export const InstallationMessage: React.FunctionComponent<Props> = ({ isK8s }) => {
const kibanaVersion = useKibanaVersion();
const kibanaVersionURLString = useMemo(
() =>
Expand All @@ -23,37 +29,47 @@ export const InstallationMessage: React.FunctionComponent = () => {
);

return (
<EuiText>
<FormattedMessage
id="xpack.fleet.enrollmentInstructions.installationMessage"
defaultMessage="Select the appropriate platform and run commands to install, enroll, and start Elastic Agent. Reuse commands to set up agents on more than one host. For aarch64, see our {downloadLink}. For additional guidance, see our {installationLink}."
values={{
downloadLink: (
<EuiLink
target="_blank"
external
href={`https://www.elastic.co/downloads/past-releases/elastic-agent-${kibanaVersionURLString}`}
>
<FormattedMessage
id="xpack.fleet.enrollmentInstructions.downloadLink"
defaultMessage="downloads page"
/>
</EuiLink>
),
installationLink: (
<EuiLink
target="_blank"
external
href="https://www.elastic.co/guide/en/fleet/current/elastic-agent-installation.html"
>
<FormattedMessage
id="xpack.fleet.enrollmentInstructions.installationMessage.link"
defaultMessage="installation docs"
/>
</EuiLink>
),
}}
/>
</EuiText>
<>
<EuiText>
{isK8s === 'IS_KUBERNETES' ? (
<FormattedMessage
id="xpack.fleet.agentEnrollment.stepRunAgentDescriptionk8s"
defaultMessage="From the directory where the Kubernetes manifest is downloaded, run the apply command."
/>
) : (
<FormattedMessage
id="xpack.fleet.enrollmentInstructions.installationMessage"
defaultMessage="Select the appropriate platform and run commands to install, enroll, and start Elastic Agent. Reuse commands to set up agents on more than one host. For aarch64, see our {downloadLink}. For additional guidance, see our {installationLink}."
values={{
downloadLink: (
<EuiLink
target="_blank"
external
href={`https://www.elastic.co/downloads/past-releases/elastic-agent-${kibanaVersionURLString}`}
>
<FormattedMessage
id="xpack.fleet.enrollmentInstructions.downloadLink"
defaultMessage="downloads page"
/>
</EuiLink>
),
installationLink: (
<EuiLink
target="_blank"
external
href="https://www.elastic.co/guide/en/fleet/current/elastic-agent-installation.html"
>
<FormattedMessage
id="xpack.fleet.enrollmentInstructions.installationMessage.link"
defaultMessage="installation docs"
/>
</EuiLink>
),
}}
/>
)}
</EuiText>
<EuiSpacer size="l" />
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { FullAgentPolicy } from '../../../../common/types/models/agent_poli

import { fullAgentPolicyToYaml, agentPolicyRouteService } from '../../../services';

import { StandaloneInstructions } from '../../enrollment_instructions/standalone';
import { StandaloneInstructions, ManualInstructions } from '../../enrollment_instructions';

import {
useGetOneEnrollmentAPIKey,
Expand Down Expand Up @@ -45,6 +45,7 @@ import {
AgentEnrollmentConfirmationStep,
InstallManagedAgentStep,
IncomingDataConfirmationStep,
DownloadStep,
} from '.';

export const StandaloneSteps: React.FunctionComponent<InstructionProps> = ({
Expand Down Expand Up @@ -163,7 +164,6 @@ export const StandaloneSteps: React.FunctionComponent<InstructionProps> = ({
InstallStandaloneAgentStep({
installCommand: standaloneInstallCommands,
isK8s,
selectedPolicyId: selectedPolicy?.id,
})
);

Expand Down Expand Up @@ -203,19 +203,24 @@ export const ManagedSteps: React.FunctionComponent<InstructionProps> = ({
onClickViewAgents,
isK8s,
installedPackagePolicy,
isFleetServerPolicySelected,
}) => {
const kibanaVersion = useKibanaVersion();
const core = useStartServices();
const { docLinks } = core;
const link = docLinks.links.fleet.troubleshooting;
const [agentDataConfirmed, setAgentDataConfirmed] = useState<boolean>(false);

const apiKey = useGetOneEnrollmentAPIKey(selectedApiKeyId);
const apiKeyData = apiKey?.data;
const enrollToken = apiKey.data ? apiKey.data.item.api_key : '';

const enrolledAgentIds = usePollingAgentCount(selectedPolicy?.id || '');

const fleetServerHosts = useMemo(() => {
return settings?.fleet_server_hosts || [];
}, [settings]);
const installManagedCommands = ManualInstructions(enrollToken, fleetServerHosts, kibanaVersion);

const instructionsSteps = useMemo(() => {
const steps: EuiContainedStepProps[] = !agentPolicy
Expand Down Expand Up @@ -243,11 +248,17 @@ export const ManagedSteps: React.FunctionComponent<InstructionProps> = ({
);
}

if (isK8s === 'IS_KUBERNETES') {
steps.push(
DownloadStep(isFleetServerPolicySelected || false, isK8s || '', enrollToken || '')
);
}

steps.push(
InstallManagedAgentStep({
installCommand: installManagedCommands,
apiKeyData,
selectedApiKeyId,
fleetServerHosts,
isK8s,
})
);
Expand Down Expand Up @@ -282,14 +293,16 @@ export const ManagedSteps: React.FunctionComponent<InstructionProps> = ({
setSelectedPolicyId,
refreshAgentPolicies,
selectionType,
apiKeyData,
fleetServerHosts,
isK8s,
installManagedCommands,
apiKeyData,
enrolledAgentIds,
mode,
setMode,
isFleetServerPolicySelected,
enrollToken,
onClickViewAgents,
link,
enrolledAgentIds,
agentDataConfirmed,
installedPackagePolicy,
]);
Expand Down
Loading

0 comments on commit b227f03

Please sign in to comment.