Skip to content

Commit

Permalink
Merge pull request #399 from juozasg/azure-fixes
Browse files Browse the repository at this point in the history
Azure fixes
  • Loading branch information
Kingdon Barrett authored Oct 26, 2022
2 parents 71960eb + 7d3b0f5 commit b6764f5
Show file tree
Hide file tree
Showing 61 changed files with 5,001 additions and 365 deletions.
6 changes: 4 additions & 2 deletions media/createSource.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<h2>Create Source on the <code id="cluster-name">""</code> cluster</h2>
<h2>Create Source on the <code id="cluster-name">""</code>cluster</h2>
<h3 id="cluster-provider"></h3>
<hr>





<div id="generic-form" hidden>
<div>
<label class="header-label" for="git-source-name">Source Name</label><input type="text" id="git-source-name">
Expand Down Expand Up @@ -67,7 +70,6 @@ <h3 id="cluster-provider"></h3>


<div id="azure-form" hidden>

<div>
<label class="header-label" for="az-scope-cluster">Specify scope of the operator to be 'namespace' or 'cluster' <code class="cli-argument">--scope</code></label>
<input type="radio" id="az-scope-cluster" name="az-scope" value="cluster" checked> <label for="az-scope-cluster">cluster</label>
Expand Down
42 changes: 25 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@
"onCommand:gitops.copyResourceName",
"onCommand:gitops.editor.openResource",
"onCommand:gitops.editor.showLogs",
"onCommand:gitops.editor.createSource",
"onCommand:gitops.output.show",
"onCommand:gitops.installFluxCli",
"onWebviewPanel:createSource",
"onView:gitops.views.clusters",
"onView:gitops.views.sources",
"onView:gitops.views.workloads",
Expand Down Expand Up @@ -179,7 +177,7 @@
},
{
"command": "gitops.views.createGitRepository",
"title": "Create Git Repository Source",
"title": "Create GitRepository Source",
"category": "GitOps"
},
{
Expand All @@ -189,6 +187,20 @@
"enablement": "!gitops:noClusterSelected && !gitops:currentClusterGitOpsNotEnabled",
"category": "GitOps"
},
{
"command": "gitops.addSource",
"title": "Add Source",
"category": "GitOps",
"icon": "$(add)",
"enablement": "!gitops:noClusterSelected && !gitops:currentClusterGitOpsNotEnabled"
},
{
"command": "gitops.addKustomization",
"title": "Add Kustomization",
"category": "GitOps",
"icon": "$(add)",
"enablement": "!gitops:noClusterSelected && !gitops:currentClusterGitOpsNotEnabled"
},
{
"command": "gitops.views.deleteWorkload",
"title": "Delete",
Expand All @@ -215,13 +227,6 @@
"title": "View Config",
"category": "GitOps"
},
{
"command": "gitops.editor.createSource",
"title": "Create Source",
"category": "GitOps",
"enablement": "!gitops:noClusterSelected && !gitops:currentClusterGitOpsNotEnabled",
"icon": "$(add)"
},
{
"command": "gitops.output.show",
"title": "Show Output",
Expand Down Expand Up @@ -341,12 +346,12 @@
"when": "view == gitops.views.clusters"
},
{
"command": "gitops.editor.createSource",
"command": "gitops.addSource",
"group": "navigation@0",
"when": "view == gitops.views.sources"
},
{
"command": "gitops.createKustomization",
"command": "gitops.addKustomization",
"group": "navigation@0",
"when": "view == gitops.views.workloads"
},
Expand Down Expand Up @@ -427,6 +432,11 @@
"when": "view == gitops.views.sources && viewItem =~ /GitRepository;/",
"group": "navigation@3"
},
{
"command": "gitops.addKustomization",
"when": "view == gitops.views.sources && viewItem =~ /GitRepository;/",
"group": "navigation@3"
},
{
"command": "gitops.editor.showLogs",
"when": "view =~ /^(gitops.views.clusters)$/ && viewItem =~ /(Deployment;)/"
Expand Down Expand Up @@ -520,10 +530,6 @@
"command": "gitops.views.showWorkloadsHelpMessage",
"when": "never"
},
{
"command": "gitops.editor.createSource",
"when": "never"
},
{
"command": "gitops.flux.trace",
"when": "never"
Expand Down Expand Up @@ -552,7 +558,9 @@
"watch-tests": "tsc -p . -w --outDir out",
"pretest": "npm run compile-tests && npm run compile && npm run lint",
"lint": "eslint src --ext ts",
"test": "node ./out/test/runTest.js"
"test": "node ./out/test/runTest.js",
"start:webview": "npm --prefix ./webview-ui run start",
"build:webview": "npm --prefix ./webview-ui run build"
},
"extensionDependencies": [
"ms-kubernetes-tools.vscode-kubernetes-tools"
Expand Down
27 changes: 20 additions & 7 deletions src/azure/azureTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export const enum AzureConstants {
FluxExtensionName = 'flux',
}


export type CreateSourceGitAzureArgs = Parameters<typeof azureTools['createSourceGit']>[0];


class AzureTools {

private async buildAzCommand(
Expand Down Expand Up @@ -180,7 +184,7 @@ class AzureTools {
* @param contextName target context name
* @param clusterProvider target cluster provider
*/
private async listFluxConfigurations(
public async listFluxConfigurations(
contextName: string,
clusterProvider: AzureClusterProvider,
): Promise<undefined | any[]> {
Expand Down Expand Up @@ -212,9 +216,12 @@ class AzureTools {
kustomizationPath: string,
contextName: string,
clusterProvider: AzureClusterProvider,
kustomizationDependsOn?: string,
) {
const dependsOnArg = kustomizationDependsOn ? ` --depends-on "${kustomizationDependsOn}"` : '';

const createKustomizationShellResult = await this.invokeAzCommand(
`az k8s-configuration flux kustomization create --kustomization-name ${kustomizationName} --name ${gitRepositoryName} --path "${kustomizationPath}" --prune true`,
`az k8s-configuration flux kustomization create --kustomization-name ${kustomizationName} --name ${gitRepositoryName} --path "${kustomizationPath}"${dependsOnArg} --prune true`,
contextName,
clusterProvider,
);
Expand All @@ -232,8 +239,8 @@ class AzureTools {
*/
async createSourceGit(args: {
sourceName: string;
sourceScope?: string;
sourceNamespace?: string;
azureScope?: string;
namespace?: string;
contextName: string;
clusterProvider: AzureClusterProvider;
url: string;
Expand Down Expand Up @@ -262,8 +269,8 @@ class AzureTools {
kustomizationForce?: boolean;
}) {
const urlArg = ` --url "${args.url}"`;
const scopeArg = args.sourceScope ? ` --scope "${args.sourceScope}"` : '';
const namespaceArg = args.sourceNamespace ? ` --namespace "${args.sourceNamespace}"` : '';
const scopeArg = args.azureScope ? ` --scope "${args.azureScope}"` : '';
const namespaceArg = args.namespace ? ` --namespace "${args.namespace}"` : '';
const branchArg = args.branch ? ` --branch "${args.branch}"` : '';
const tagArg = args.tag ? ` --tag "${args.tag}"` : '';
const semverArg = args.semver ? ` --semver "${args.semver}"` : '';
Expand Down Expand Up @@ -399,12 +406,13 @@ class AzureTools {


async deleteKustomization(
configName: string,
kustomizationName: string,
contextName: string,
clusterProvider: AzureClusterProvider,
) {
const deleteSourceShellResult = await this.invokeAzCommand(
`az k8s-configuration flux kustomization delete -n ${kustomizationName} --yes`,
`az k8s-configuration flux kustomization delete -n ${configName} -k ${kustomizationName} --yes`,
contextName,
clusterProvider,
);
Expand Down Expand Up @@ -460,6 +468,11 @@ class AzureTools {
}


getAzName(fluxConfigName: string, resourceName: string) {
return resourceName.replace(RegExp(`^${fluxConfigName}-`), '');
}


}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { commands, Disposable, ExtensionContext, Uri, window } from 'vscode';
import { copyResourceName } from './commands/copyResourceName';
import { createGitRepository } from './commands/createGitRepository';
import { createKustomization } from './commands/createKustomization';
import { addSource } from './commands/addSource';
import { addKustomization } from './commands/addKustomization';
import { deleteWorkload } from './commands/deleteWorkload';
import { deleteSource } from './commands/deleteSource';
import { fluxDisableGitOps, fluxEnableGitOps } from './commands/enableDisableGitOps';
Expand All @@ -11,7 +13,6 @@ import { fluxReconcileRepository } from './commands/fluxReconcileRepository';
import { fluxReconcileSourceCommand } from './commands/fluxReconcileSource';
import { fluxReconcileWorkload } from './commands/fluxReconcileWorkload';
import { installFluxCli } from './commands/installFluxCli';
import { openCreateSourceWebview } from './commands/openCreateSourceWebview';
import { openResource } from './commands/openResource';
import { pullGitRepository } from './commands/pullGitRepository';
import { resume } from './commands/resume';
Expand Down Expand Up @@ -73,13 +74,14 @@ export const enum CommandId {
DeleteWorkload = 'gitops.views.deleteWorkload',
DeleteSource = 'gitops.views.deleteSource',
CopyResourceName = 'gitops.copyResourceName',
AddSource = 'gitops.addSource',
AddKustomization = 'gitops.addKustomization',

// editor
EditorOpenResource = 'gitops.editor.openResource',

// webview
ShowLogs = 'gitops.editor.showLogs',
OpenCreateSourceWebview = 'gitops.editor.createSource',
ShowNewUserGuide = 'gitops.views.showNewUserGuide',

// output commands
Expand Down Expand Up @@ -130,13 +132,15 @@ export function registerCommands(context: ExtensionContext) {
registerCommand(CommandId.DeleteWorkload, deleteWorkload);
registerCommand(CommandId.DeleteSource, deleteSource);
registerCommand(CommandId.CopyResourceName, copyResourceName);
registerCommand(CommandId.AddSource, addSource);
registerCommand(CommandId.AddKustomization, addKustomization);


// editor
registerCommand(CommandId.EditorOpenResource, openResource);

// webview
registerCommand(CommandId.ShowLogs, showLogs);
registerCommand(CommandId.OpenCreateSourceWebview, openCreateSourceWebview);
registerCommand(CommandId.ShowNewUserGuide, showNewUserGuide);

// output
Expand Down
12 changes: 12 additions & 0 deletions src/commands/addKustomization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { openConfigureGitOpsPanel } from '../panels/configureGitOps';
import { GitRepositoryNode } from '../views/nodes/gitRepositoryNode';

/**
* Open ConfigureGitops webview with a source preselected (if user right-clicked a source node)
* @param sourceNode user right-clicked this in the Sources treeview
*/
export async function addKustomization(sourceNode?: GitRepositoryNode) {
const selectedSource = sourceNode?.resource?.metadata.name;

openConfigureGitOpsPanel(true, selectedSource);
}
8 changes: 8 additions & 0 deletions src/commands/addSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { openConfigureGitOpsPanel } from '../panels/configureGitOps';

/**
* Open ConfigureGitops webview with 'New Source' tab open
*/
export async function addSource() {
openConfigureGitOpsPanel(false);
}
10 changes: 5 additions & 5 deletions src/commands/createSource.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import gitUrlParse from 'git-url-parse';
import { commands, env, Uri, window } from 'vscode';
import { azureTools } from '../azure/azureTools';
import { azureTools, CreateSourceGitAzureArgs } from '../azure/azureTools';
import { CommandId } from '../commands';
import { telemetry } from '../extension';
import { fluxTools } from '../flux/fluxTools';
import { CreateSourceGitGenericArgs, fluxTools } from '../flux/fluxTools';
import { KubernetesObjectKinds } from '../kubernetes/kubernetesTypes';
import { TelemetryEventNames } from '../telemetry';
import { refreshSourcesTreeView, refreshWorkloadsTreeView } from '../views/treeViews';

export async function createGitRepositoryGenericCluster(args: Parameters<typeof fluxTools['createSourceGit']>[0]) {

export async function createGitRepositoryGenericCluster(args: CreateSourceGitGenericArgs) {
const parsedGitUrl = gitUrlParse(args.url);
if (isUrlSourceAzureDevops(parsedGitUrl.source)) {
// Azure devops git repo doesn't work with git implementation `go-git` and
Expand All @@ -23,6 +22,7 @@ export async function createGitRepositoryGenericCluster(args: Parameters<typeof
});

const deployKey = await fluxTools.createSourceGit(args);
refreshSourcesTreeView();

setTimeout(() => {
// Wait a bit for the repository to have a failed state in case of SSH url
Expand All @@ -31,7 +31,7 @@ export async function createGitRepositoryGenericCluster(args: Parameters<typeof
showDeployKeyNotificationIfNeeded(args.url, deployKey?.deployKey);
}

export async function createGitRepositoryAzureCluster(args: Parameters<typeof azureTools['createSourceGit']>[0]) {
export async function createGitRepositoryAzureCluster(args: CreateSourceGitAzureArgs) {

telemetry.send(TelemetryEventNames.CreateSource, {
kind: KubernetesObjectKinds.GitRepository,
Expand Down
4 changes: 3 additions & 1 deletion src/commands/deleteWorkload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export async function deleteWorkload(workloadNode: KustomizationNode | HelmRelea


if (currentClusterInfo.result.isAzure && workloadType === 'kustomization') {
await azureTools.deleteKustomization(workloadName, currentClusterInfo.result.contextName, currentClusterInfo.result.clusterProvider as AzureClusterProvider);
const fluxConfigName = (workloadNode.resource.spec as any).sourceRef?.name;
const azResourceName = azureTools.getAzName(fluxConfigName, workloadName);
await azureTools.deleteKustomization(fluxConfigName, azResourceName, currentClusterInfo.result.contextName, currentClusterInfo.result.clusterProvider as AzureClusterProvider);
} else {
await fluxTools.delete(workloadType, workloadName, workloadNamespace);
}
Expand Down
43 changes: 0 additions & 43 deletions src/commands/openCreateSourceWebview.ts

This file was deleted.

5 changes: 4 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { statusBar } from './statusBar';
import { Telemetry, TelemetryEventNames } from './telemetry';
import { createTreeViews, clusterTreeViewProvider, sourceTreeViewProvider, workloadTreeViewProvider } from './views/treeViews';
import { shell } from './shell';
import { openConfigureGitOpsPanel } from './panels/configureGitOps';

/** Disable interactive modal dialogs, useful for testing */
export let disableConfirmations = false;
Expand All @@ -24,7 +25,7 @@ export const enum GitOpsExtensionConstants {
/** State that is saved even between editor reloads */
export let globalState: GlobalState;
/** Methods to report telemetry over Application Insights (Exceptions or Custom Events). */
export let telemetry: Telemetry;
export let telemetry: Telemetry | any;

/**
* Called when GitOps extension is activated.
Expand All @@ -49,6 +50,8 @@ export async function activate(context: ExtensionContext) {
// to the cluster
checkIfOpenedFolderGitRepositorySourceExists();

// openConfigureGitOpsPanel(true);

telemetry.send(TelemetryEventNames.Startup);

if (globalState.get(GlobalStateKey.FirstEverActivationStorageKey) === undefined) {
Expand Down
Loading

0 comments on commit b6764f5

Please sign in to comment.