Skip to content

Commit

Permalink
Transfer commit: include Azure and Minikube target in command telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
itowlson committed Sep 6, 2018
1 parent fb7a5f9 commit c686e0a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ export async function activate(context): Promise<extensionapi.ExtensionAPI> {
export const deactivate = () => { };

function registerCommand(command: string, callback: (...args: any[]) => any): vscode.Disposable {
const wrappedCallback = telemetry.telemetrise(command, callback);
const wrappedCallback = telemetry.telemetrise(command, kubectl, callback);
return vscode.commands.registerCommand(command, wrappedCallback);
}

Expand Down Expand Up @@ -1708,6 +1708,7 @@ async function useContextKubernetes(explorerNode: explorer.KubernetesObject) {
const shellResult = await kubectl.invokeAsync(`config use-context ${targetContext}`);
if (shellResult.code === 0) {
refreshExplorer();
telemetry.invalidateClusterType(targetContext);
} else {
vscode.window.showErrorMessage(`Failed to set '${targetContext}' as current cluster: ${shellResult.stderr}`);
}
Expand Down
84 changes: 81 additions & 3 deletions src/telemetry-helper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,86 @@
import { reporter } from './telemetry';
import { Kubectl } from './kubectl';

export function telemetrise(command: string, callback: (...args: any[]) => any): (...args: any[]) => any {
return (a) => {
reporter.sendTelemetryEvent("command", { command: command });
export function telemetrise(command: string, kubectl: Kubectl, callback: (...args: any[]) => any): (...args: any[]) => any {
return async (a) => {
reporter.sendTelemetryEvent("command", { command: command, clusterType: await clusterType(kubectl) });
return callback(a);
};
}

export enum ClusterType {
Azure,
Minikube,
Other
}

let latestContextName: string | null;
let cachedClusterType: ClusterType | null = null;
const knownClusters: any = {};

export function invalidateClusterType(newContext: string): void {
latestContextName = newContext || null;
cachedClusterType = null;
}

async function clusterType(kubectl: Kubectl): Promise<string> {
if (latestContextName && knownClusters[latestContextName]) {
cachedClusterType = knownClusters[latestContextName];
}
if (!cachedClusterType) {
cachedClusterType = await inferCurrentClusterType(kubectl, latestContextName);
if (latestContextName) {
knownClusters[latestContextName] = cachedClusterType;
}
}
switch (cachedClusterType) {
case ClusterType.Azure:
return 'azure';
case ClusterType.Minikube:
return 'minikube';
case ClusterType.Other:
return 'other';
}
}

async function inferCurrentClusterType(kubectl: Kubectl, contextNameHint: string | null): Promise<ClusterType | null> {
if (!latestContextName) {
const ctxsr = await kubectl.invokeAsync('config current-context');
if (ctxsr.code === 0) {
latestContextName = ctxsr.stdout.trim();
} else {
return ClusterType.Other; // something is terribly wrong; we don't want to retry
}
}

if (latestContextName === 'minikube') {
return ClusterType.Minikube;
}

const cisr = await kubectl.invokeAsync('cluster-info');
if (cisr.code !== 0) {
return null;
}
const masterInfos = cisr.stdout.split('\n')
.filter((s) => s.indexOf('master is running at') >= 0);

if (masterInfos.length === 0) {
return ClusterType.Other; // something is terribly wrong; we don't want to retry
}

const masterInfo = masterInfos[0];
if (masterInfo.indexOf('azure.com') >= 0) {
return ClusterType.Azure;
}

if (latestContextName) {
const gcsr = await kubectl.invokeAsync(`config get-contexts ${latestContextName}`);
if (gcsr.code === 0) {
if (gcsr.stdout.indexOf('minikube') >= 0) {
return ClusterType.Minikube; // It's pretty heuristic, so don't spend time parsing the table
}
}
}

return ClusterType.Other;
}

0 comments on commit c686e0a

Please sign in to comment.