Skip to content

Commit

Permalink
wait for pod deletion and remove captures for deleted pods
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbom committed Oct 26, 2023
1 parent d04be9b commit a9a2bda
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
29 changes: 27 additions & 2 deletions src/panels/TcpDumpPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,29 @@ spec:
private async _handleDeleteDebugPod(node: string, webview: MessageSink<ToWebViewMsgDef>) {
const command = `delete pod -n ${debugPodNamespace} ${getPodName(node)}`;
const output = await invokeKubectlCommand(this.kubectl, this.kubeConfigFilePath, command);
if (failed(output)) {
webview.postDeleteDebugPodResponse({
node,
succeeded: false,
errorMessage: `Failed to delete ${getPodName(node)}:\n${output.error}`
});
return;
}

const waitResult = await this._waitForPodDeleted(node);
if (failed(waitResult)) {
webview.postStartDebugPodResponse({
node,
succeeded: false,
errorMessage: `Pod ${getPodName(node)} was not deleted:\n${waitResult.error}`
});
return;
}

webview.postDeleteDebugPodResponse({
node,
succeeded: output.succeeded,
errorMessage: failed(output) ? output.error : null
succeeded: true,
errorMessage: null
});
}

Expand Down Expand Up @@ -324,6 +343,12 @@ spec:
return errmap(waitOutput, _ => undefined);
}

private async _waitForPodDeleted(node: string): Promise<Errorable<void>> {
const waitCommand = `wait pod -n ${debugPodNamespace} --for=delete --timeout=300s ${getPodName(node)}`;
const waitOutput = await invokeKubectlCommand(this.kubectl, this.kubeConfigFilePath, waitCommand);
return errmap(waitOutput, _ => undefined);
}

private async _getRunningCaptures(node: string): Promise<Errorable<TcpDumpProcess[]>> {
// List all processes without header columns, including PID, command and args (which contains the command)
const podCommand = "ps -e -o pid= -o comm= -o args=";
Expand Down
20 changes: 14 additions & 6 deletions webview-ui/src/TCPDump/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export const stateUpdater: WebviewStateUpdater<"tcpDump", EventDef, TcpDumpState
}),
vscodeMessageHandler: {
checkNodeStateResponse: (state, args) => ({...state, nodeStates: getNodeStatesFromCheck(state.nodeStates, args)}),
startDebugPodResponse: (state, args) => ({...state, nodeStates: getNodeStatesFromCommand(state.nodeStates, args, NodeStatus.DebugPodRunning, NodeStatus.Unknown)}),
deleteDebugPodResponse: (state, args) => ({...state, nodeStates: getNodeStatesFromCommand(state.nodeStates, args, NodeStatus.Clean, NodeStatus.Unknown)}),
startDebugPodResponse: (state, args) => ({...state, nodeStates: getNodeStatesFromPodCreation(state.nodeStates, args)}),
deleteDebugPodResponse: (state, args) => ({...state, nodeStates: getNodeStatesFromPodDeletion(state.nodeStates, args)}),
startCaptureResponse: (state, args) => ({...state, nodeStates: getNodeStatesFromCaptureStartResult(state.nodeStates, args)}),
stopCaptureResponse: (state, args) => ({...state, nodeStates: getNodeStatesFromCaptureStopResult(state.nodeStates, args)}),
downloadCaptureFileResponse: (state, args) => ({...state, nodeStates: getNodeStatesFromDownloadResult(state.nodeStates, args)})
Expand Down Expand Up @@ -100,14 +100,22 @@ function getNodeStatesFromCheck(nodeStates: NodeStates, result: NodeCheckResult)
return {...nodeStates, [result.node]: nodeState};
}

function getNodeStatesFromCommand(nodeStates: NodeStates, result: NodeCommandResult, statusIfSucceeded: NodeStatus, statusIfFailed: NodeStatus): NodeStates {
const status = result.succeeded ? statusIfSucceeded : statusIfFailed;
function getNodeStatesFromStatus(nodeStates: NodeStates, node: NodeName, newStatus: NodeStatus): NodeStates {
return updateNodeState(nodeStates, node, state => ({...state, status: newStatus}));
}

function getNodeStatesFromPodCreation(nodeStates: NodeStates, result: NodeCommandResult): NodeStates {
const errorMessage = result.succeeded ? null : result.errorMessage;
const status = result.succeeded ? NodeStatus.DebugPodRunning : NodeStatus.Unknown;
return updateNodeState(nodeStates, result.node, state => ({...state, status, errorMessage}));
}

function getNodeStatesFromStatus(nodeStates: NodeStates, node: NodeName, newStatus: NodeStatus): NodeStates {
return updateNodeState(nodeStates, node, state => ({...state, status: newStatus}));
function getNodeStatesFromPodDeletion(nodeStates: NodeStates, result: NodeCommandResult): NodeStates {
const errorMessage = result.succeeded ? null : result.errorMessage;
const status = result.succeeded ? NodeStatus.Clean : NodeStatus.Unknown;
const currentCaptureName = null;
const completedCaptures: NodeCapture[] = [];
return updateNodeState(nodeStates, result.node, state => ({...state, status, errorMessage, currentCaptureName, completedCaptures}));
}

function getNodeStatesFromCaptureStarting(nodeStates: NodeStates, node: NodeName, capture: CaptureName): NodeStates {
Expand Down

0 comments on commit a9a2bda

Please sign in to comment.