Skip to content

Commit b998f64

Browse files
committed
refactor: extract helm installation snippet from agent installation instructions
1 parent dfb4446 commit b998f64

File tree

11 files changed

+380
-189
lines changed

11 files changed

+380
-189
lines changed

src/components/Agents/InstalAgentInstruction/InstallAgentModal.tsx

Lines changed: 42 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
import { useMemo, useState } from "react";
1+
import HelmInstallationSnippets, {
2+
TemplateContextData
3+
} from "@flanksource-ui/ui/HelmSnippet/HelmInstallationSnippets";
4+
import { useMemo } from "react";
25
import { GeneratedAgent } from "../../../api/services/agents";
36
import { Button } from "../../../ui/Buttons/Button";
47
import { Modal } from "../../../ui/Modal";
5-
import { Tab, Tabs } from "../../../ui/Tabs/Tabs";
68
import { AgentFormValues } from "../Add/AddAgentForm";
7-
import CLIInstallAgent from "./CLIInstallAgent";
8-
import FluxInstallAgent from "./FluxInstallAgent";
99
import { useAgentsBaseURL } from "./useAgentsBaseURL";
1010

11-
export function WarningBox() {
12-
return (
13-
<div
14-
className="relative rounded border border-yellow-200 bg-yellow-100 px-4 py-3 text-yellow-700"
15-
role="alert"
16-
>
17-
<span className="block sm:inline">
18-
Access token will be shown only once. Please copy it and store it
19-
securely.
20-
</span>
21-
</div>
22-
);
23-
}
24-
2511
export function MoreInfoBox() {
2612
return (
2713
<div className="flex flex-col gap-2 px-2">
@@ -84,14 +70,6 @@ export function MoreInfoBox() {
8470
);
8571
}
8672

87-
export type TemplateContextData = {
88-
namespace?: string;
89-
chart?: string;
90-
repoName?: string;
91-
values?: string[];
92-
kubeValues?: string[];
93-
};
94-
9573
type Props = {
9674
isOpen: boolean;
9775
onClose: () => void;
@@ -105,7 +83,6 @@ export default function InstallAgentModal({
10583
generatedAgent,
10684
agentFormValues
10785
}: Props) {
108-
const [activeTab, setActiveTab] = useState<"cli" | "flux">("cli");
10986
const baseUrl = useAgentsBaseURL();
11087

11188
const data = useMemo(() => {
@@ -116,36 +93,60 @@ export default function InstallAgentModal({
11693
chart: "mission-control-agent",
11794
namespace: "mission-control-agent",
11895
repoName: "flanksource",
96+
createNamespace: true,
11997
// You can add more values here to be passed to the template for the
12098
// values section of the HelmRelease
12199
values: [
122-
`upstream.createSecret=true`,
123-
`upstream.host=${baseUrl}`,
124-
`upstream.username=token`,
125-
`upstream.password=${generatedAgent?.access_token}`,
126-
`upstream.agentName=${agentFormValues?.name}`,
100+
{
101+
key: "upstream.createSecret",
102+
value: "true"
103+
},
104+
{
105+
key: "upstream.host",
106+
value: baseUrl
107+
},
108+
{
109+
key: "upstream.username",
110+
value: "token"
111+
},
112+
{
113+
key: "upstream.password",
114+
value: generatedAgent?.access_token
115+
},
116+
{
117+
key: "upstream.agentName",
118+
value: agentFormValues?.name
119+
},
127120
...(pushTelemetry?.enabled
128121
? [
129-
`pushTelemetry.enabled=true`,
130-
`pushTelemetry.topologyName=${
131-
pushTelemetry.topologyName
132-
}-${agentFormValues?.name}`
122+
{
123+
key: "pushTelemetry.enabled",
124+
value: "true"
125+
},
126+
{
127+
key: "pushTelemetry.topologyName",
128+
value: `${pushTelemetry.topologyName}`
129+
}
133130
]
134131
: [])
135132
],
136133
// You can add more values here to be passed to the template for the
137134
// kubeValues section of the HelmRelease
138135
kubeValues: kubeOptions
139136
? [
140-
`clusterName: "${agentFormValues?.name}"`,
141-
`scraper.schedule: "${kubeOptions.schedule}"`
137+
{
138+
key: "clusterName",
139+
value: agentFormValues?.name
140+
},
141+
{
142+
key: "scraper.schedule",
143+
value: kubeOptions.schedule
144+
}
142145
]
143146
: []
144147
} satisfies TemplateContextData;
145148
}, [agentFormValues, baseUrl, generatedAgent]);
146149

147-
console.log(JSON.stringify(data, null, 2));
148-
149150
return (
150151
<Modal
151152
title={"Installation Instructions"}
@@ -159,23 +160,7 @@ export default function InstallAgentModal({
159160
Install the Mission Control agent using instructions below
160161
</h3>
161162
<div className="flex flex-col">
162-
<Tabs
163-
activeTab={activeTab}
164-
onSelectTab={(v) => setActiveTab(v as any)}
165-
>
166-
<Tab className="flex flex-col gap-4 p-4" label="Flux" value="flux">
167-
<FluxInstallAgent data={data} />
168-
<WarningBox />
169-
</Tab>
170-
<Tab
171-
className="flex flex-col gap-4 p-4"
172-
label="Helm CLI"
173-
value="cli"
174-
>
175-
<CLIInstallAgent data={data} />
176-
<WarningBox />
177-
</Tab>
178-
</Tabs>
163+
<HelmInstallationSnippets templateData={data} isWarningDisplayed />
179164
</div>
180165
<MoreInfoBox />
181166
</div>

src/components/Agents/InstalAgentInstruction/__tests__/CLIInstallAgent.unit.test.tsx

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,45 @@
11
import CodeBlock from "@flanksource-ui/ui/Code/CodeBlock";
22
import Handlebars from "handlebars";
33
import { useMemo } from "react";
4-
import { TemplateContextData } from "./InstallAgentModal";
4+
import { TemplateContextData } from "./HelmInstallationSnippets";
55

66
// This a Handlebars template for the Helm command to install the agent and the
77
// kubernetes agent if the user has enabled it.
8-
const helmCommand = `helm repo add {{ chart }} {{ repoName }}/{{ chart }}
8+
const helmCommand = `{{#if valueFile }}
9+
cat > values.yaml << EOF
10+
{{ valueFile }}
11+
EOF
912
13+
14+
{{/if}}
15+
{{#if createRepo }}
16+
helm repo add {{ chart }} {{ repoName }}/{{ chart }}
17+
{{/if}}
1018
helm repo update
1119
1220
helm install mc-agent flanksource/mission-control-agent -n "{{{ namespace }}}" \\
1321
{{#each values}}
14-
--set {{{ this }}} \\
22+
--set {{{ this.key }}}={{{ this.value }}} \\
1523
{{/each}}
16-
--create-namespace
24+
{{#if createNamespace }}
25+
--create-namespace \\
26+
{{/if}}
27+
{{#if valueFile }}
28+
--set-file values.yaml \\
29+
{{/if }}
30+
{{#if args}}
31+
{{#each args}}
32+
{{ this }} \\
33+
{{/each}}
34+
{{/if}}
35+
{{#if wait }}
36+
--wait \\
37+
{{/if}}
1738
1839
{{#if kubeValues }}
1940
helm install mc-agent-kubernetes flanksource/mission-control-kubernetes -n "{{{ namespace }}}" \\
2041
{{#each kubeValues}}
21-
--set {{{ this }}} \\
42+
--set {{{ this.key }}}={{{ this.value }}} \\
2243
{{/each}}
2344
{{/if}}
2445
`;
Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { JSONViewer } from "@flanksource-ui/ui/Code/JSONViewer";
22
import Handlebars from "handlebars";
33
import { useMemo } from "react";
4-
import { TemplateContextData } from "./InstallAgentModal";
4+
import { TemplateContextData } from "./HelmInstallationSnippets";
55

66
// This a Handlebars template for the HelmRelease to install the agent and the
77
// kubernetes agent if the user has enabled it.
8-
const fluxTemplate = `apiVersion: v1
8+
const fluxTemplate = `{{#if createNamespace}}
9+
apiVersion: v1
910
kind: Namespace
1011
metadata:
1112
name: {{ namespace }}
1213
---
14+
{{/if}}
1315
{{#if createRepository}}
1416
apiVersion: source.toolkit.fluxcd.io/v1beta1
1517
kind: HelmRepository
@@ -37,7 +39,7 @@ spec:
3739
interval: 5m0s
3840
values:
3941
{{#each values}}
40-
{{{ this }}}
42+
{{{ this.key }}}: {{{ this.value }}}
4143
{{/each}}
4244
4345
{{#if kubeValues}}
@@ -56,8 +58,11 @@ spec:
5658
name: flanksource
5759
namespace: mission-control-agent
5860
values:
61+
{{#if valueFile }}
62+
{{ valueFile }}
63+
{{/if}}
5964
{{#each kubeValues}}
60-
{{{ this }}}
65+
{{{ this.key }}}: {{{ this.value }}}
6166
{{/each}}
6267
{{/if}}
6368
`;
@@ -70,7 +75,17 @@ type Props = {
7075

7176
export default function FluxInstallAgent({ data }: Props) {
7277
const yaml = useMemo(() => {
73-
return template(data, {});
78+
console.log({ data });
79+
return template(
80+
{
81+
...data,
82+
valueFile: data?.valueFile
83+
? // Indent the valueFile content
84+
data?.valueFile?.replace(/^/gm, " ")
85+
: undefined
86+
},
87+
{}
88+
);
7489
}, [data]);
7590

7691
return (
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Meta, StoryFn } from "@storybook/react";
2+
import HelmInstallationSnippets from "./HelmInstallationSnippets";
3+
import {
4+
mockInput,
5+
mockInputWithKubOptions,
6+
mockInputWithValueFile
7+
} from "./__tests__/mocks/mocks";
8+
9+
export default {
10+
title: "ui/HelmInstallationSnippets",
11+
component: HelmInstallationSnippets
12+
} satisfies Meta<typeof HelmInstallationSnippets>;
13+
14+
const Template: StoryFn<typeof HelmInstallationSnippets> = (args) => (
15+
<HelmInstallationSnippets {...args} />
16+
);
17+
18+
export const Default = Template.bind({});
19+
20+
Default.args = {
21+
templateData: mockInput
22+
};
23+
24+
export const WithKubeOptions = Template.bind({});
25+
26+
WithKubeOptions.args = {
27+
templateData: mockInputWithKubOptions
28+
};
29+
30+
export const WithKubeOptionsAndValueFile = Template.bind({});
31+
32+
WithKubeOptionsAndValueFile.args = {
33+
templateData: mockInputWithValueFile
34+
};

0 commit comments

Comments
 (0)