Skip to content

Commit

Permalink
Fixes and performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
itowlson committed Sep 10, 2018
1 parent c686e0a commit 87a4fe2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ export async function activate(context): Promise<extensionapi.ExtensionAPI> {
registerTelemetry(context)
];

telemetry.invalidateClusterType(undefined, kubectl);

await azureclusterprovider.init(clusterProviderRegistry, { shell: shell, fs: fs });
await minikubeclusterprovider.init(clusterProviderRegistry, { shell: shell, minikube: minikube });
// On save, refresh the Helm YAML preview.
Expand Down Expand Up @@ -1707,8 +1709,8 @@ async function useContextKubernetes(explorerNode: explorer.KubernetesObject) {
const targetContext = contextObj.contextName;
const shellResult = await kubectl.invokeAsync(`config use-context ${targetContext}`);
if (shellResult.code === 0) {
refreshExplorer();
telemetry.invalidateClusterType(targetContext);
refreshExplorer();
} else {
vscode.window.showErrorMessage(`Failed to set '${targetContext}' as current cluster: ${shellResult.stderr}`);
}
Expand Down
47 changes: 33 additions & 14 deletions src/telemetry-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,60 @@ export function telemetrise(command: string, kubectl: Kubectl, callback: (...arg
}

export enum ClusterType {
Unknown = 0,
Azure,
Minikube,
Other
}

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

export function invalidateClusterType(newContext: string): void {
export function invalidateClusterType(newContext: string, kubectl?: Kubectl): void {
latestContextName = newContext || null;
cachedClusterType = null;
cachedClusterType = ClusterType.Unknown;
if (kubectl) {
setImmediate(() => {
try {
loadCachedClusterType(kubectl);
} catch {
// swallow it
}
});
}
}

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;
}
if (cachedClusterType === ClusterType.Unknown) {
await loadCachedClusterType(kubectl);
}

switch (cachedClusterType) {
case ClusterType.Azure:
return 'azure';
case ClusterType.Minikube:
return 'minikube';
case ClusterType.Other:
return 'other';
default:
return 'indeterminate';
}
}

async function loadCachedClusterType(kubectl: Kubectl) {
if (latestContextName && knownClusters[latestContextName]) {
cachedClusterType = knownClusters[latestContextName];
}
else {
cachedClusterType = await inferCurrentClusterType(kubectl, latestContextName);
if (latestContextName) {
knownClusters[latestContextName] = cachedClusterType;
}
}
}

async function inferCurrentClusterType(kubectl: Kubectl, contextNameHint: string | null): Promise<ClusterType | null> {
async function inferCurrentClusterType(kubectl: Kubectl, contextNameHint: string | null): Promise<ClusterType> {
if (!latestContextName) {
const ctxsr = await kubectl.invokeAsync('config current-context');
if (ctxsr.code === 0) {
Expand All @@ -59,7 +78,7 @@ async function inferCurrentClusterType(kubectl: Kubectl, contextNameHint: string

const cisr = await kubectl.invokeAsync('cluster-info');
if (cisr.code !== 0) {
return null;
return ClusterType.Unknown;
}
const masterInfos = cisr.stdout.split('\n')
.filter((s) => s.indexOf('master is running at') >= 0);
Expand All @@ -69,7 +88,7 @@ async function inferCurrentClusterType(kubectl: Kubectl, contextNameHint: string
}

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

Expand Down

0 comments on commit 87a4fe2

Please sign in to comment.