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] Present users with a Kubernetes manifest for deploying agent in fleet managed mode for Kubernetes #127703

Merged
Merged
7 changes: 7 additions & 0 deletions x-pack/plugins/fleet/common/constants/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const EPM_API_ROOT = `${API_ROOT}/epm`;
export const DATA_STREAM_API_ROOT = `${API_ROOT}/data_streams`;
export const PACKAGE_POLICY_API_ROOT = `${API_ROOT}/package_policies`;
export const AGENT_POLICY_API_ROOT = `${API_ROOT}/agent_policies`;
export const K8S_API_ROOT = `${API_ROOT}/kubernetes`;

export const LIMITED_CONCURRENCY_ROUTE_TAG = 'ingest:limited-concurrency';

Expand Down Expand Up @@ -67,6 +68,12 @@ export const AGENT_POLICY_API_ROUTES = {
FULL_INFO_DOWNLOAD_PATTERN: `${AGENT_POLICY_API_ROOT}/{agentPolicyId}/download`,
};

// Kubernetes Manifest API routes
export const K8S_API_ROUTES = {
K8S_DOWNLOAD_PATTERN: `${K8S_API_ROOT}/download`,
K8S_INFO_PATTERN: `${K8S_API_ROOT}`,
};

// Output API routes
export const OUTPUT_API_ROUTES = {
LIST_PATTERN: `${API_ROOT}/outputs`,
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/fleet/common/services/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
OUTPUT_API_ROUTES,
SETTINGS_API_ROUTES,
APP_API_ROUTES,
K8S_API_ROUTES,
} from '../constants';

export const epmRouteService = {
Expand Down Expand Up @@ -137,6 +138,14 @@ export const agentPolicyRouteService = {
agentPolicyId
);
},

getK8sInfoPath: () => {
return K8S_API_ROUTES.K8S_INFO_PATTERN;
},

getK8sFullDownloadPath: () => {
return K8S_API_ROUTES.K8S_DOWNLOAD_PATTERN;
},
};

export const dataStreamRouteService = {
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/fleet/common/types/rest_spec/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ export interface GetFullAgentPolicyResponse {
export interface GetFullAgentConfigMapResponse {
item: string;
}

export interface GetFullAgentManifestResponse {
item: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ export const OnPremInstructions: React.FC = () => {
className="eui-textLeft"
steps={[
AgentPolicySelectionStep({ policyId, setPolicyId }),
DownloadStep(true),
DownloadStep(true, '', ''),
deploymentModeStep({ deploymentMode, setDeploymentMode }),
addFleetServerHostStep({ addFleetServerHost }),
ServiceTokenStep({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import type { PackagePolicy } from '../../types';

import { Loading } from '..';

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

import { ManagedInstructions } from './managed_instructions';
import { StandaloneInstructions } from './standalone_instructions';
import { MissingFleetServerHostCallout } from './missing_fleet_server_host_callout';
Expand Down Expand Up @@ -93,6 +95,32 @@ export const AgentEnrollmentFlyout: React.FunctionComponent<Props> = ({
checkPolicyIsFleetServer();
}, [policyId]);

const [isK8s, setIsK8s] = useState<'IS_LOADING' | 'IS_KUBERNETES' | 'IS_NOT_KUBERNETES'>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can move this to it's own hook and use the same hook for standalone instructions

'IS_LOADING'
);
useEffect(() => {
async function checkifK8s() {
if (!policyId) {
setIsK8s('IS_LOADING');
return;
}
const agentPolicyRequest = await sendGetOneAgentPolicy(policyId);
const agentPol = agentPolicyRequest.data ? agentPolicyRequest.data.item : null;

if (!agentPol) {
setIsK8s('IS_NOT_KUBERNETES');
return;
}
const k8s = (pkg: PackagePolicy) => pkg.package?.name === FLEET_KUBERNETES_PACKAGE;
setIsK8s(
(agentPol.package_policies as PackagePolicy[]).some(k8s)
? 'IS_KUBERNETES'
: 'IS_NOT_KUBERNETES'
);
}
checkifK8s();
}, [policyId]);

const isLoadingInitialRequest = settings.isLoading && settings.isInitialRequest;

return (
Expand Down Expand Up @@ -154,10 +182,12 @@ export const AgentEnrollmentFlyout: React.FunctionComponent<Props> = ({
<ManagedInstructions
settings={settings.data?.item}
setSelectedPolicyId={setSelectedPolicyId}
policyId={policyId}
agentPolicy={agentPolicy}
agentPolicies={agentPolicies}
viewDataStep={viewDataStep}
isFleetServerPolicySelected={isFleetServerPolicySelected}
isK8s={isK8s}
refreshAgentPolicies={refreshAgentPolicies}
isLoadingAgentPolicies={isLoadingAgentPolicies}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ export const ManagedInstructions = React.memo<InstructionProps>(
agentPolicies,
viewDataStep,
setSelectedPolicyId,
policyId,
isFleetServerPolicySelected,
isK8s,
settings,
refreshAgentPolicies,
isLoadingAgentPolicies,
}) => {
const fleetStatus = useFleetStatus();

const [selectedApiKeyId, setSelectedAPIKeyId] = useState<string | undefined>();

const apiKey = useGetOneEnrollmentAPIKey(selectedApiKeyId);
const fleetServerInstructions = useFleetServerInstructions(apiKey?.data?.item?.policy_id);

Expand Down Expand Up @@ -111,6 +111,8 @@ export const ManagedInstructions = React.memo<InstructionProps>(
];
}, [fleetServerInstructions]);

const enrolToken = apiKey.data ? apiKey.data.item.api_key : '';

const steps = useMemo(() => {
const fleetServerHosts = settings?.fleet_server_hosts || [];
const baseSteps: EuiContainedStepProps[] = [
Expand All @@ -123,7 +125,7 @@ export const ManagedInstructions = React.memo<InstructionProps>(
refreshAgentPolicies,
})
: AgentEnrollmentKeySelectionStep({ agentPolicy, selectedApiKeyId, setSelectedAPIKeyId }),
DownloadStep(isFleetServerPolicySelected || false),
DownloadStep(isFleetServerPolicySelected || false, isK8s || '', enrolToken || ''),
];
if (isFleetServerPolicySelected) {
baseSteps.push(...fleetServerSteps);
Expand All @@ -133,7 +135,12 @@ export const ManagedInstructions = React.memo<InstructionProps>(
defaultMessage: 'Enroll and start the Elastic Agent',
}),
children: selectedApiKeyId && apiKey.data && (
<ManualInstructions apiKey={apiKey.data.item} fleetServerHosts={fleetServerHosts} />
<ManualInstructions
apiKey={apiKey.data.item}
fleetServerHosts={fleetServerHosts}
policyId={policyId}
isK8s={isK8s}
/>
),
});
}
Expand All @@ -155,6 +162,9 @@ export const ManagedInstructions = React.memo<InstructionProps>(
isFleetServerPolicySelected,
settings?.fleet_server_hosts,
viewDataStep,
enrolToken,
isK8s,
policyId,
]);

if (fleetStatus.isReady && settings?.fleet_server_hosts.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ export const StandaloneInstructions = React.memo<InstructionProps>(
downloadLink =
isK8s === 'IS_KUBERNETES'
? core.http.basePath.prepend(
`${agentPolicyRouteService.getInfoFullDownloadPath(selectedPolicyId)}?kubernetes=true`
`${agentPolicyRouteService.getInfoFullDownloadPath(
selectedPolicyId
)}?kubernetes=true&standalone=true`
)
: core.http.basePath.prepend(
`${agentPolicyRouteService.getInfoFullDownloadPath(selectedPolicyId)}?standalone=true`
Expand Down Expand Up @@ -188,7 +190,7 @@ export const StandaloneInstructions = React.memo<InstructionProps>(
refreshAgentPolicies,
})
: undefined,
DownloadStep(false),
DownloadStep(false, '', ''),
{
title: i18n.translate('xpack.fleet.agentEnrollment.stepConfigureAgentTitle', {
defaultMessage: 'Configure the agent',
Expand Down
Loading