From 6578903c45fa2833c78bfc327e11c7acac47fcf1 Mon Sep 17 00:00:00 2001 From: Oliver King Date: Fri, 7 Apr 2023 14:11:26 -0400 Subject: [PATCH] add path to draft k8s resources without local kubeconfig (#87) --- .../runDraftTool/helper/runDraftHelper.ts | 2 +- .../runDraftTool/runDraftDeployment.ts | 107 +++++++----------- .../runDraftTool/runDraftDockerfile.ts | 6 +- src/commands/runDraftTool/runDraftIngress.ts | 17 +-- 4 files changed, 43 insertions(+), 89 deletions(-) diff --git a/src/commands/runDraftTool/helper/runDraftHelper.ts b/src/commands/runDraftTool/helper/runDraftHelper.ts index ffc9ab9..035f56d 100644 --- a/src/commands/runDraftTool/helper/runDraftHelper.ts +++ b/src/commands/runDraftTool/helper/runDraftHelper.ts @@ -31,7 +31,7 @@ export async function ensureDraftBinary(): Promise> { if (!latestReleaseTag) { return { succeeded: false, - error: `failed to get latest release tag` + error: `Failed to get latest release tag for downloading draft` }; } diff --git a/src/commands/runDraftTool/runDraftDeployment.ts b/src/commands/runDraftTool/runDraftDeployment.ts index 6cc7be2..70dea5f 100644 --- a/src/commands/runDraftTool/runDraftDeployment.ts +++ b/src/commands/runDraftTool/runDraftDeployment.ts @@ -93,13 +93,9 @@ export async function runDraftDeployment( const az: AzApi = new Az(getAzCreds); // Ensure Draft Binary - const downloadResult = await longRunning(`Downloading Draft.`, () => + await longRunning(`Downloading Draft.`, () => getAsyncResult(ensureDraftBinary()) ); - if (!downloadResult) { - vscode.window.showErrorMessage('Failed to download Draft'); - return undefined; - } const workspaceFolders = vscode.workspace.workspaceFolders; if (!outputFolder && workspaceFolders && workspaceFolders.length !== 0) { @@ -134,7 +130,6 @@ export async function runDraftDeployment( new PromptAcrTag(az) ]; const executeSteps: IExecuteStep[] = [ - new ExecuteCreateNamespace(k8s), new ExecuteDraft(), new ExecuteOpenFiles(), new ExecuteSaveState(state), @@ -225,38 +220,49 @@ class PromptNamespace extends AzureWizardPromptStep { } public async prompt(wizardContext: WizardContext): Promise { - const namespaces = getAsyncResult(this.k8s.listNamespaces()); - const newOption = 'New Namespace'; - const getOptions = async (): Promise => { - const namespaceOptions: vscode.QuickPickItem[] = ( - await namespaces - ).map((version) => ({ - label: version.metadata?.name || '' - })); - - return [ - {label: newOption}, - {label: '', kind: vscode.QuickPickItemKind.Separator}, - ...namespaceOptions, + try { + const namespaces = getAsyncResult(this.k8s.listNamespaces()); + const newOption = 'New Namespace'; + const getOptions = async (): Promise => { + const namespaceOptions: vscode.QuickPickItem[] = ( + await namespaces + ).map((version) => ({ + label: version.metadata?.name || '' + })); + + return [ + {label: newOption}, + {label: '', kind: vscode.QuickPickItemKind.Separator}, + ...namespaceOptions, + { + label: 'Existing namespaces', + kind: vscode.QuickPickItemKind.Separator + } + ]; + }; + const namespacePick = await wizardContext.ui.showQuickPick( + getOptions(), { - label: 'Existing namespaces', - kind: vscode.QuickPickItemKind.Separator + ignoreFocusOut, + stepName: 'Namespace', + placeHolder: 'Namespace' } - ]; - }; - const namespacePick = await wizardContext.ui.showQuickPick(getOptions(), { - ignoreFocusOut, - stepName: 'Namespace', - placeHolder: 'Namespace' - }); + ); - if (namespacePick.label === newOption) { + if (namespacePick.label === newOption) { + wizardContext.newNamespace = true; + return; + } + + wizardContext.newNamespace = false; + wizardContext.namespace = namespacePick.label; + } catch (err) { + console.error(`Error prompting namespaces: ${err}`); + + // make the user manually enter a namespace wizardContext.newNamespace = true; return; } - - wizardContext.newNamespace = false; - wizardContext.namespace = namespacePick.label; } public shouldPrompt(wizardContext: WizardContext): boolean { @@ -268,8 +274,8 @@ class PromptNewNamespace extends AzureWizardPromptStep { public async prompt(wizardContext: WizardContext): Promise { wizardContext.namespace = await wizardContext.ui.showInputBox({ ignoreFocusOut, - prompt: 'New Namespace', - stepName: 'New Namespace', + prompt: 'Namespace', + stepName: 'Namespace', validateInput: ValidateRfc1123, value: wizardContext.namespace || wizardContext.applicationName // application name is a reasonable autofill guess }); @@ -567,39 +573,6 @@ export class PromptPort extends AzureWizardPromptStep< } } -class ExecuteCreateNamespace extends AzureWizardExecuteStep { - public priority: number = 1; - - constructor(private k8s: KubernetesApi) { - super(); - } - - public async execute( - wizardContext: WizardContext, - progress: vscode.Progress<{ - message?: string | undefined; - increment?: number | undefined; - }> - ): Promise { - const {namespace} = wizardContext; - if (namespace === undefined) { - throw Error('Namespace is undefined'); - } - - const result = await this.k8s.createNamespace(namespace); - if (failed(result)) { - throw Error(result.error); - } - } - - public shouldExecute(wizardContext: WizardContext): boolean { - return ( - !!wizardContext.newNamespace && - wizardContext.format !== DraftFormat.Helm // Draft creates namespace manifest for Helm - ); - } -} - class ExecuteDraft extends AzureWizardExecuteStep { public priority: number = 2; diff --git a/src/commands/runDraftTool/runDraftDockerfile.ts b/src/commands/runDraftTool/runDraftDockerfile.ts index e4eae28..1d154f3 100644 --- a/src/commands/runDraftTool/runDraftDockerfile.ts +++ b/src/commands/runDraftTool/runDraftDockerfile.ts @@ -46,13 +46,9 @@ export async function runDraftDockerfile( const state: StateApi = State.construct(extensionContext); // Ensure Draft Binary - const downloadResult = await longRunning(`Downloading Draft.`, () => + await longRunning(`Downloading Draft.`, () => getAsyncResult(ensureDraftBinary()) ); - if (!downloadResult) { - vscode.window.showErrorMessage('Failed to download Draft'); - return undefined; - } const workspaceFolders = vscode.workspace.workspaceFolders; if (!sourceCodeFolder && workspaceFolders && workspaceFolders.length !== 0) { diff --git a/src/commands/runDraftTool/runDraftIngress.ts b/src/commands/runDraftTool/runDraftIngress.ts index 020167e..9a72103 100644 --- a/src/commands/runDraftTool/runDraftIngress.ts +++ b/src/commands/runDraftTool/runDraftIngress.ts @@ -69,13 +69,9 @@ export async function runDraftIngress( completedSteps: CompletedSteps ) { // Ensure Draft Binary - const downloadResult = await longRunning(`Downloading Draft`, () => + await longRunning(`Downloading Draft`, () => getAsyncResult(ensureDraftBinary()) ); - if (!downloadResult) { - vscode.window.showErrorMessage('Failed to download Draft'); - return undefined; - } const az: AzApi = new Az(getAzCreds); @@ -467,9 +463,6 @@ class ExecuteEnableAddOn extends AzureWizardExecuteStep { if (cluster.managedCluster.addonProfiles === undefined) { cluster.managedCluster.addonProfiles = {}; } - cluster.managedCluster.addonProfiles.httpApplicationRouting = { - enabled: true - }; cluster.managedCluster.addonProfiles.azureKeyvaultSecretsProvider = { config: {enableSecretRotation: 'true'}, enabled: true @@ -606,14 +599,6 @@ class ExecuteUpdateAddOn extends AzureWizardExecuteStep { throw Error('DNS Zone id is undefined'); } - if (cluster.managedCluster.addonProfiles === undefined) { - cluster.managedCluster.addonProfiles = {}; - } - cluster.managedCluster.addonProfiles.httpApplicationRouting = { - // eslint-disable-next-line @typescript-eslint/naming-convention - config: {HTTPApplicationRoutingZoneName: dnsZone}, - enabled: true - }; if (cluster.managedCluster.ingressProfile === undefined) { cluster.managedCluster.ingressProfile = {}; }