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

Right Click Draft Validate #1123

Merged
merged 10 commits into from
Dec 20, 2024
Merged
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@
"command": "aks.aksKaito",
"title": "Install KAITO"
},
{
"command": "aks.aksDraftValidate",
"title": "Run Draft Validate for YAML"
},
{
"command": "aks.aksKaitoTest",
"title": "Test KAITO models"
Expand Down Expand Up @@ -356,6 +360,14 @@
{
"command": "aks.draftDeployment",
"when": "explorerResourceIsFolder"
},
{
"command": "aks.aksDraftValidate",
"when": "resourceExtname == .yaml || resourceExtname == .yml"
},
{
"command": "aks.aksDraftValidate",
"when": "explorerResourceIsFolder"
}
],
"view/item/context": [
Expand Down
14 changes: 14 additions & 0 deletions src/commands/draft/draftCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
FileType,
} from "vscode";
import { DraftDockerfileDataProvider, DraftDockerfilePanel } from "../../panels/draft/DraftDockerfilePanel";
import { DraftValidateDataProvider, DraftValidatePanel } from "../../panels/draft/DraftValidatePanel";
import { getExtension } from "../utils/host";
import { Errorable, failed, getErrorMessage, succeeded } from "../utils/errorable";
import { getDraftBinaryPath } from "../utils/helper/draftBinaryDownload";
Expand Down Expand Up @@ -169,6 +170,19 @@ export async function draftWorkflow(_context: IActionContext, target: unknown):
panel.show(dataProvider);
}

export async function draftValidate(_context: IActionContext, target: unknown): Promise<void> {
const params = getDraftDockerfileParams(target);
const commonDependencies = await getCommonDraftDependencies(params?.workspaceFolder);
if (commonDependencies === null) {
return;
}

const { workspaceFolder, extension, draftBinaryPath } = commonDependencies;
const panel = new DraftValidatePanel(extension.extensionUri);
const dataProvider = new DraftValidateDataProvider(workspaceFolder, draftBinaryPath, params?.initialLocation || "");
panel.show(dataProvider);
}

async function getCommonDraftDependencies(
suppliedWorkspaceFolder?: WorkspaceFolder,
): Promise<DraftDependencies | null> {
Expand Down
4 changes: 4 additions & 0 deletions src/commands/draft/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export type DraftCommandParamsTypes = {
workspaceFolder?: WorkspaceFolder;
initialSelection?: WorkflowInitialSelection;
};
"aks.draftValidate": {
workspaceFolder?: WorkspaceFolder;
initialLocation?: string;
};
};

export type DraftCommandName = keyof DraftCommandParamsTypes;
Expand Down
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
aksNodeHealth,
aksStorageDiagnostics,
} from "./commands/detectors/detectors";
import { draftDeployment, draftDockerfile, draftWorkflow } from "./commands/draft/draftCommands";
import { draftValidate, draftDeployment, draftDockerfile, draftWorkflow } from "./commands/draft/draftCommands";
import periscope from "./commands/periscope/periscope";
import refreshSubscription from "./commands/refreshSubscriptions";
import { getKubeconfigYaml, getManagedCluster } from "./commands/utils/clusters";
Expand Down Expand Up @@ -72,7 +72,6 @@ export async function activate(context: vscode.ExtensionContext) {
outputChannel: createAzExtOutputChannel("Azure Identity", ""),
prefix: "",
};

context.subscriptions.push(uiExtensionVariables.outputChannel);

registerUIExtensionVariables(uiExtensionVariables);
Expand Down Expand Up @@ -118,6 +117,7 @@ export async function activate(context: vscode.ExtensionContext) {
registerCommandWithTelemetry("aks.aksDeployManifest", aksDeployManifest);
registerCommandWithTelemetry("aks.aksOpenKubectlPanel", aksOpenKubectlPanel);
registerCommandWithTelemetry("aks.getAzureKubernetesServicePlugins", getPlugins);
registerCommandWithTelemetry("aks.aksDraftValidate", draftValidate);

await registerAzureServiceNodes(context);

Expand Down
73 changes: 73 additions & 0 deletions src/panels/draft/DraftValidatePanel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Uri, WorkspaceFolder, window } from "vscode";
import path from "path";
import { BasePanel, PanelDataProvider } from "../BasePanel";
import {
InitialState,
ToVsCodeMsgDef,
ToWebViewMsgDef,
} from "../../webview-contract/webviewDefinitions/draft/draftValidate";
import { TelemetryDefinition } from "../../webview-contract/webviewTypes";
import { MessageHandler, MessageSink } from "../../webview-contract/messaging";
import { ShellOptions, exec } from "../../commands/utils/shell";
import { failed } from "../../commands/utils/errorable";

export class DraftValidatePanel extends BasePanel<"draftValidate"> {
constructor(extensionUri: Uri) {
super(extensionUri, "draftValidate", {
validationResult: undefined,
});
}
}

export class DraftValidateDataProvider implements PanelDataProvider<"draftValidate"> {
readonly draftDirectory: string;
constructor(
readonly workspaceFolder: WorkspaceFolder,
readonly draftBinaryPath: string,
readonly initialLocation: string,
) {
this.draftDirectory = path.dirname(draftBinaryPath);
}

getTitle(): string {
return `Draft Validate in ${this.workspaceFolder.name}`;
}

getInitialState(): InitialState {
return {
validationResults: "Initializing validation...",
};
}

getTelemetryDefinition(): TelemetryDefinition<"draftValidate"> {
Tatsinnit marked this conversation as resolved.
Show resolved Hide resolved
return {
createDraftValidateRequest: true,
};
}

//Messages from Webview to Vscode
getMessageHandler(webview: MessageSink<ToWebViewMsgDef>): MessageHandler<ToVsCodeMsgDef> {
return {
createDraftValidateRequest: () => this.handleDraftValidateRequest(webview),
};
}

private async handleDraftValidateRequest(webview: MessageSink<ToWebViewMsgDef>) {
const command = `draft validate --manifest .${path.sep}${this.initialLocation}`;

const execOptions: ShellOptions = {
workingDir: this.workspaceFolder.uri.fsPath,
envPaths: [this.draftDirectory],
};

const draftResult = await exec(command, execOptions);
if (failed(draftResult)) {
window.showErrorMessage(draftResult.error);
return;
}

const validationResults = draftResult.result.stdout;

webview.postValidationResult({ result: validationResults });
}
}
17 changes: 17 additions & 0 deletions src/webview-contract/webviewDefinitions/draft/draftValidate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { WebviewDefinition } from "../../webviewTypes";

export interface InitialState {
validationResults: string;
}

export type ExistingFiles = string[];

export type ToVsCodeMsgDef = {
createDraftValidateRequest: string;
};

export type ToWebViewMsgDef = {
validationResult: { result: string };
};

export type DraftValidateDefinition = WebviewDefinition<InitialState, ToVsCodeMsgDef, ToWebViewMsgDef>;
2 changes: 2 additions & 0 deletions src/webview-contract/webviewTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DetectorDefinition } from "./webviewDefinitions/detector";
import { DraftDeploymentDefinition } from "./webviewDefinitions/draft/draftDeployment";
import { DraftDockerfileDefinition } from "./webviewDefinitions/draft/draftDockerfile";
import { DraftWorkflowDefinition } from "./webviewDefinitions/draft/draftWorkflow";
import { DraftValidateDefinition } from "./webviewDefinitions/draft/draftValidate";
import { InspektorGadgetDefinition } from "./webviewDefinitions/inspektorGadget";
import { KaitoDefinition } from "./webviewDefinitions/kaito";
import { KaitoModelsDefinition } from "./webviewDefinitions/kaitoModels";
Expand Down Expand Up @@ -45,6 +46,7 @@ type AllWebviewDefinitions = {
draftDeployment: DraftDeploymentDefinition;
draftDockerfile: DraftDockerfileDefinition;
draftWorkflow: DraftWorkflowDefinition;
draftValidate: DraftValidateDefinition;
gadget: InspektorGadgetDefinition;
kubectl: KubectlDefinition;
aso: ASODefinition;
Expand Down
20 changes: 20 additions & 0 deletions webview-ui/src/Draft/DraftValidate/DraftValidate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { InitialState } from "../../../../src/webview-contract/webviewDefinitions/draft/draftValidate";
import { useStateManagement } from "../../utilities/state";
import { stateUpdater, vscode } from "./state";
import { useEffect } from "react";

export function DraftValidate(initialState: InitialState) {
const { state } = useStateManagement(stateUpdater, initialState, vscode);

//Request the validation results from the backend once when the component is mounted.
useEffect(() => {
vscode.postCreateDraftValidateRequest("");
}, []);

return (
<>
<h2>Draft Validate</h2>
<pre>{state.validationResults}</pre>
</>
);
}
33 changes: 33 additions & 0 deletions webview-ui/src/Draft/DraftValidate/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { WebviewStateUpdater } from "../../utilities/state";
import { getWebviewMessageContext } from "../../utilities/vscode";

export type EventDef = {
//Defines the events that can originate from the webview and be sent to the backend (ToVsCodeMsgDef).
draftValidateRequest: string;
};

export type DraftValidateState = {
validationResults: string;
};

export const stateUpdater: WebviewStateUpdater<"draftValidate", EventDef, DraftValidateState> = {
createState: (initialState) => ({
validationResults: initialState.validationResults,
}),
vscodeMessageHandler: {
// This handler updates the state when a message from the extension
validationResult: (state, response) => ({
...state,
validationResults: response.result,
}),
},
eventHandler: {
draftValidateRequest: (state) => ({
...state,
}),
},
};

export const vscode = getWebviewMessageContext<"draftValidate">({
createDraftValidateRequest: null,
});
3 changes: 2 additions & 1 deletion webview-ui/src/Draft/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DraftDockerfile } from "./DraftDockerfile/DraftDockerfile";
import { DraftDeployment } from "./DraftDeployment/DraftDeployment";
import { DraftWorkflow } from "./DraftWorkflow/DraftWorkflow";
import { DraftValidate } from "./DraftValidate/DraftValidate";

export { DraftDockerfile, DraftDeployment, DraftWorkflow };
export { DraftDockerfile, DraftDeployment, DraftWorkflow, DraftValidate };
3 changes: 2 additions & 1 deletion webview-ui/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AzureServiceOperator } from "./AzureServiceOperator/AzureServiceOperato
import { ClusterProperties } from "./ClusterProperties/ClusterProperties";
import { CreateCluster } from "./CreateCluster/CreateCluster";
import { Detector } from "./Detector/Detector";
import { DraftDeployment, DraftDockerfile, DraftWorkflow } from "./Draft";
import { DraftDeployment, DraftDockerfile, DraftWorkflow, DraftValidate } from "./Draft";
import { InspektorGadget } from "./InspektorGadget/InspektorGadget";
import { Kaito } from "./Kaito/Kaito";
import { KaitoModels } from "./KaitoModels/KaitoModels";
Expand Down Expand Up @@ -58,6 +58,7 @@ function getVsCodeContent(): JSX.Element {
draftDeployment: () => <DraftDeployment {...getInitialState()} />,
draftDockerfile: () => <DraftDockerfile {...getInitialState()} />,
draftWorkflow: () => <DraftWorkflow {...getInitialState()} />,
draftValidate: () => <DraftValidate {...getInitialState()} />,
gadget: () => <InspektorGadget {...getInitialState()} />,
kubectl: () => <Kubectl {...getInitialState()} />,
aso: () => <AzureServiceOperator {...getInitialState()} />,
Expand Down
1 change: 1 addition & 0 deletions webview-ui/src/manualTest/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const contentTestScenarios: Record<ContentId, Scenario[]> = {
draftDeployment: getDraftDeploymentScenarios(),
draftDockerfile: getDraftDockerfileScenarios(),
draftWorkflow: getDraftWorkflowScenarios(),
draftValidate: [],
ReinierCC marked this conversation as resolved.
Show resolved Hide resolved
gadget: getInspektorGadgetScenarios(),
kubectl: getKubectlScenarios(),
aso: getASOScenarios(),
Expand Down
Loading