Skip to content

Commit

Permalink
Merge pull request #471 from weaveworks/cleaning-up-debug-logs
Browse files Browse the repository at this point in the history
Cleaning up debug logs
  • Loading branch information
Kingdon Barrett authored Sep 7, 2023
2 parents 1902db2 + 109759b commit 4d6c5fb
Show file tree
Hide file tree
Showing 43 changed files with 352 additions and 306 deletions.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
},
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
"typescript.tsc.autoDetect": "off",
"debug.node.autoAttach": "on"
"debug.node.autoAttach": "on",
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true
},
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-gitops-tools",
"displayName": "GitOps Tools for Flux",
"description": "GitOps automation tools for continuous delivery of Kubernetes and Cloud Native applications",
"version": "0.25.1-edge.5",
"version": "0.25.1-edge.6",
"author": "Kingdon Barrett <kingdon@weave.works>",
"contributors": [
"Kingdon Barrett <kingdon@weave.works>",
Expand Down
2 changes: 1 addition & 1 deletion src/cli/flux/fluxTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class FluxTools {
errorData += `Command '${cmd}' timed out`;
}
// + (treeShellResult.code === null ? 'Command timed out' : '';
window.showErrorMessage(`Failed to get resources created by the kustomization ${name}. ERROR: ${errorData}`);
window.showWarningMessage(`Failed to get resources created by the kustomization ${name}. ERROR: ${errorData}`);
return;
}

Expand Down
47 changes: 24 additions & 23 deletions src/cli/kubernetes/apiResources.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
import { redrawhResourcesTreeViews, refreshResourcesTreeViews } from 'commands/refreshTreeViews';
import { currentContextData } from 'data/contextData';
import { setVSCodeContext, telemetry } from 'extension';
import { TelemetryError } from 'types/telemetryEventNames';
import { invokeKubectlCommand } from './kubernetesToolsKubectl';
import { Kind } from 'types/kubernetes/kubernetesTypes';
import { createK8sClients } from 'k8s/client';
import { ContextId } from 'types/extensionIds';
import { refreshAllTreeViews, refreshResourcesTreeViews } from 'commands/refreshTreeViews';
import { restartKubeProxy } from './kubectlProxy';
import { Kind } from 'types/kubernetes/kubernetesTypes';
import { TelemetryError } from 'types/telemetryEventNames';
import { clusterDataProvider } from 'ui/treeviews/treeViews';
import { restartKubeProxy } from './kubectlProxy';
import { invokeKubectlCommand } from './kubernetesToolsKubectl';

export enum ApiState {
Loading,
Loaded,
ClusterUnreachable,
}

type KindApiParams = {
export type KindApiParams = {
plural: string; // configmaps, deployments, gitrepositories, ...
group: string; // '', apps, source.toolkit.fluxcd.io, ...
version: string; // v1, v1beta2, ...
};

export let apiState: ApiState = ApiState.Loading;

/*
* Current cluster supported kubernetes resource kinds.
*/
let apiResources: Map<Kind, KindApiParams> | undefined;

/**
* Return all available kubernetes resource kinds
*/
export function getAvailableResourcePlurals(): string[] | undefined {
const context = currentContextData();
const plurals: string[] = [];
if(apiResources) {
apiResources.forEach((value, key) => {
if(context.apiResources) {
context.apiResources.forEach((value, key) => {
plurals.push(value.plural);
});

Expand All @@ -43,32 +39,38 @@ export function getAvailableResourcePlurals(): string[] | undefined {


export function getAPIParams(kind: Kind): KindApiParams | undefined {
if(apiResources) {
return apiResources.get(kind);
const context = currentContextData();

if(context.apiResources) {
return context.apiResources.get(kind);
}
}


export async function loadAvailableResourceKinds() {
apiResources = undefined;
apiState = ApiState.Loading;
const context = currentContextData();
context.apiResources = undefined;
context.apiState = ApiState.Loading;
// will set their content to Loading API...
redrawhResourcesTreeViews();

const kindsShellResult = await invokeKubectlCommand('api-resources --verbs=list -o wide');
if (kindsShellResult?.code !== 0) {
telemetry.sendError(TelemetryError.FAILED_TO_GET_AVAILABLE_RESOURCE_KINDS);
console.warn(`Failed to get resource kinds: ${kindsShellResult?.stderr}`);
apiState = ApiState.ClusterUnreachable;
context.apiState = ApiState.ClusterUnreachable;
setVSCodeContext(ContextId.ClusterUnreachable, true);
clusterDataProvider.updateCurrentContextChildNodes();
refreshResourcesTreeViews();
redrawhResourcesTreeViews();
return;
}

const lines = kindsShellResult.stdout
.split('\n')
.filter(line => line.length).slice(1);

apiResources = new Map<Kind, KindApiParams>();
context.apiResources = new Map<Kind, KindApiParams>();

lines.map(line => {
let cols = line.split(/\s+/);
Expand All @@ -86,16 +88,15 @@ export async function loadAvailableResourceKinds() {
version = group;
group = '';
}
apiResources?.set(kind, { plural, group, version });
context.apiResources?.set(kind, { plural, group, version });
});

console.log('apiResources loaded');

apiState = ApiState.Loaded;
context.apiState = ApiState.Loaded;
setVSCodeContext(ContextId.ClusterUnreachable, false);
clusterDataProvider.updateCurrentContextChildNodes();

createK8sClients();
await restartKubeProxy();

// give proxy init callbacks time to fire
Expand Down
11 changes: 3 additions & 8 deletions src/cli/kubernetes/kubectlGet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import safesh from 'shell-escape-tag';

import { setVSCodeContext, telemetry } from 'extension';
import { telemetry } from 'extension';
import { k8sList } from 'k8s/list';
import { Bucket } from 'types/flux/bucket';
import { GitOpsTemplate } from 'types/flux/gitOpsTemplate';
Expand All @@ -12,10 +12,9 @@ import { OCIRepository } from 'types/flux/ociRepository';
import { Deployment, Kind, KubernetesObject, Pod, qualifyToolkitKind } from 'types/kubernetes/kubernetesTypes';
import { TelemetryError } from 'types/telemetryEventNames';
import { parseJson, parseJsonItems } from 'utils/jsonUtils';
import { invokeKubectlCommand } from './kubernetesToolsKubectl';
import { getAvailableResourcePlurals } from './apiResources';
import { window } from 'vscode';
import { ContextId } from 'types/extensionIds';
import { getAvailableResourcePlurals } from './apiResources';
import { invokeKubectlCommand } from './kubernetesToolsKubectl';
/**
* RegExp for the Error that should not be sent in telemetry.
* Server doesn't have a resource type = when GitOps not enabled
Expand All @@ -41,11 +40,8 @@ export async function getResource(name: string, namespace: string, kind: string)
}

export async function getResourcesAllNamespaces<T extends KubernetesObject>(kind: Kind, telemetryError: TelemetryError): Promise<T[]> {
const t1 = Date.now();

const list = await k8sList(kind);
if(list !== undefined) {
console.log(`k8sList ${kind}`, list.length, ' ∆', Date.now() - t1);
return list as T[];
}

Expand All @@ -61,7 +57,6 @@ export async function getResourcesAllNamespaces<T extends KubernetesObject>(kind
}

const items = parseJsonItems(shellResult.stdout);
console.log(`kubectl get ${kind}`, items.length, ' ∆', Date.now() - t1);
return items as T[];
}

Expand Down
17 changes: 4 additions & 13 deletions src/cli/kubernetes/kubectlProxy.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { KubeConfig } from '@kubernetes/client-node';
import { ChildProcess } from 'child_process';
import * as shell from 'cli/shell/exec';
import { destroyK8sClients } from 'k8s/client';
import { createK8sClients, destroyK8sClients } from 'k8s/client';
import { createProxyConfig } from 'k8s/createKubeProxyConfig';

// let isConnecting = false;
export let proxyProc: ChildProcess | undefined;
export let kubeProxyConfig: KubeConfig | undefined;

// tries to keep alive the `kubectl proxy` process
// if process dies or errors out it will be stopped
// and restarted by the 1000ms interval
// and restarted by the 2000ms interval
// if context changes proxy is stopped and restarted immediately
export function kubeProxyKeepAlive() {
// keep alive
Expand All @@ -28,35 +27,31 @@ async function startKubeProxy() {
}

proxyProc = shell.execProc('kubectl proxy -p 0');
console.log(`~proxy started ${proxyProc.pid}`);

procListen(proxyProc);
}

function procListen(p: ChildProcess) {
p.on('exit', async code => {
console.log('~proxy exit', p.pid, code);
if(proxyProc?.pid === p.pid) {
stopKubeProxy();
}
});

p.on('error', err => {
console.log('~proxy error', p.pid, err);
p.kill();
});

p.stdout?.on('data', (data: string) => {
console.log(`~proxy ${p.pid} STDOUT: ${data}`);
if(data.includes('Starting to serve on')) {
const port = parseInt(data.split(':')[1].trim());
kubeProxyConfig = createProxyConfig(port);
console.log('~kubeproxy config ready');
createK8sClients();
}
});

p.stderr?.on('data', (data: string) => {
console.log(`~proxy ${p.pid} STDERR: ${data}`);
console.warn(`kubectl proxy ${p.pid} STDERR: ${data}`);
p.kill();
});
}
Expand All @@ -65,17 +60,13 @@ function procListen(p: ChildProcess) {
export async function stopKubeProxy() {
if(proxyProc) {
if(!proxyProc.killed) {
console.log(`~proxy.kill() ${proxyProc.pid}`);
proxyProc.kill();
}
proxyProc = undefined;
kubeProxyConfig = undefined;

destroyK8sClients();
// isConnecting = false;
console.log('~stopped kube proxy');
}

}

export async function restartKubeProxy() {
Expand Down
11 changes: 3 additions & 8 deletions src/cli/kubernetes/kubernetesConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import { window } from 'vscode';
import * as k8s from '@kubernetes/client-node';
import { ActionOnInvalid } from '@kubernetes/client-node/dist/config_types';
import { shellCodeError } from 'cli/shell/exec';
import { refreshAllTreeViews } from 'commands/refreshTreeViews';
import { setVSCodeContext, telemetry } from 'extension';
import { ContextId } from 'types/extensionIds';
import { TelemetryError } from 'types/telemetryEventNames';
import { refreshClustersTreeView } from 'ui/treeviews/treeViews';
import { reloadClustersTreeView } from 'ui/treeviews/treeViews';
import { kcContextsListChanged, kcCurrentContextChanged, kcTextChanged } from 'utils/kubeConfigCompare';
import { loadAvailableResourceKinds as loadApiResources } from './apiResources';
import { restartKubeProxy } from './kubectlProxy';
import { loadKubeConfigPath } from './kubernetesConfigWatcher';
import { invokeKubectlCommand } from './kubernetesToolsKubectl';

Expand All @@ -31,7 +29,6 @@ export const kubeConfig: k8s.KubeConfig = new k8s.KubeConfig();

// reload the kubeconfig via kubernetes-tools. fire events if things have changed
export async function syncKubeConfig(forceReloadResourceKinds = false) {
console.log('syncKubeConfig');

kubeConfigState = KubeConfigState.Loading;
const configShellResult = await invokeKubectlCommand('config view');
Expand Down Expand Up @@ -65,23 +62,21 @@ async function kubeconfigChanged(newKubeConfig: k8s.KubeConfig, forceReloadResou
// load the changed kubeconfig globally so that the following code use the new config
kubeConfig.loadFromString(newKubeConfig.exportConfig(), {onInvalidEntry: ActionOnInvalid.FILTER});

console.log(kubeConfig.currentContext);
if(!currentContextExists()) {
kubeConfigState = KubeConfigState.NoContextSelected;
}


if (contextChanged) {
console.log('currentContext changed', kubeConfig.getCurrentContext());
setVSCodeContext(ContextId.CurrentClusterGitOpsNotEnabled, false);
setVSCodeContext(ContextId.ClusterUnreachable, false);
}

if (contextChanged || forceReloadResourceKinds) {
refreshClustersTreeView();
reloadClustersTreeView();
loadApiResources();
} else if (contextsListChanged) {
refreshClustersTreeView();
reloadClustersTreeView();
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/cli/kubernetes/kubernetesToolsKubectl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ export async function invokeKubectlCommand(command: string, printOutput = true):

let kubectlShellResult;
const commandWithArgs = `kubectl ${command} --request-timeout ${getRequestTimeout()}`;
const t1 = Date.now();
kubectlShellResult = await shell.exec(commandWithArgs);
const t2 = Date.now();
console.log(`exec ${command} ∆`, t2 - t1);


if(printOutput) {
output.send(`> kubectl ${command}`, {
Expand Down
1 change: 0 additions & 1 deletion src/commands/createFromTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as vscode from 'vscode';
import { GitOpsTemplateNode } from 'ui/treeviews/nodes/gitOpsTemplateNode';
import { openCreateFromTemplatePanel } from 'ui/webviews/createFromTemplate/openWebview';

Expand Down
8 changes: 4 additions & 4 deletions src/commands/deleteSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { window } from 'vscode';

import { AzureClusterProvider, azureTools } from 'cli/azure/azureTools';
import { fluxTools } from 'cli/flux/fluxTools';
import { kubeConfig } from 'cli/kubernetes/kubernetesConfig';
import { telemetry } from 'extension';
import { failed } from 'types/errorable';
import { FluxSource } from 'types/fluxCliTypes';
Expand All @@ -11,8 +12,7 @@ import { BucketNode } from 'ui/treeviews/nodes/source/bucketNode';
import { GitRepositoryNode } from 'ui/treeviews/nodes/source/gitRepositoryNode';
import { HelmRepositoryNode } from 'ui/treeviews/nodes/source/helmRepositoryNode';
import { OCIRepositoryNode } from 'ui/treeviews/nodes/source/ociRepositoryNode';
import { getCurrentClusterInfo, refreshSourcesTreeView, refreshWorkloadsTreeView } from 'ui/treeviews/treeViews';
import { kubeConfig } from 'cli/kubernetes/kubernetesConfig';
import { getCurrentClusterInfo, reloadSourcesTreeView, reloadWorkloadsTreeView } from 'ui/treeviews/treeViews';

/**
* Delete a source
Expand Down Expand Up @@ -54,10 +54,10 @@ export async function deleteSource(sourceNode: GitRepositoryNode | OCIRepository

if (currentClusterInfo.result.isAzure) {
await azureTools.deleteSource(sourceName, contextName, currentClusterInfo.result.clusterProvider as AzureClusterProvider);
refreshWorkloadsTreeView();
reloadWorkloadsTreeView();
} else {
await fluxTools.delete(sourceType, sourceName, sourceNamespace);
}

refreshSourcesTreeView();
reloadSourcesTreeView();
}
6 changes: 3 additions & 3 deletions src/commands/deleteWorkload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { window } from 'vscode';

import { AzureClusterProvider, azureTools } from 'cli/azure/azureTools';
import { fluxTools } from 'cli/flux/fluxTools';
import { kubeConfig } from 'cli/kubernetes/kubernetesConfig';
import { telemetry } from 'extension';
import { failed } from 'types/errorable';
import { FluxWorkload } from 'types/fluxCliTypes';
import { Kind } from 'types/kubernetes/kubernetesTypes';
import { TelemetryEvent } from 'types/telemetryEventNames';
import { HelmReleaseNode } from 'ui/treeviews/nodes/workload/helmReleaseNode';
import { KustomizationNode } from 'ui/treeviews/nodes/workload/kustomizationNode';
import { getCurrentClusterInfo, refreshWorkloadsTreeView } from 'ui/treeviews/treeViews';
import { kubeConfig } from 'cli/kubernetes/kubernetesConfig';
import { getCurrentClusterInfo, reloadWorkloadsTreeView } from 'ui/treeviews/treeViews';


/**
Expand Down Expand Up @@ -73,5 +73,5 @@ export async function deleteWorkload(workloadNode: KustomizationNode | HelmRelea
await fluxTools.delete(workloadType, workloadName, workloadNamespace);
}

refreshWorkloadsTreeView();
reloadWorkloadsTreeView();
}
Loading

0 comments on commit 4d6c5fb

Please sign in to comment.