From 011478f62f4df13d4f1f447568026c18eeefa3a9 Mon Sep 17 00:00:00 2001 From: Nick Mitchell Date: Tue, 29 Jun 2021 16:13:26 -0400 Subject: [PATCH] fix(plugins/plugin-kubectl): in browser clients, switching kui tabs can yield stale kube responses part of #7737 --- .../src/controller/kubectl/config.ts | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/plugins/plugin-kubectl/src/controller/kubectl/config.ts b/plugins/plugin-kubectl/src/controller/kubectl/config.ts index 48fa1d26b63..e88f4c54274 100644 --- a/plugins/plugin-kubectl/src/controller/kubectl/config.ts +++ b/plugins/plugin-kubectl/src/controller/kubectl/config.ts @@ -61,9 +61,7 @@ export function offKubectlConfigChangeEvents(handler: Handler) { * have changed. * */ -async function doConfig(args: Arguments) { - const response = await doExecWithPty(args) - +function emitChangeEventIfNeeded(args: Arguments) { const idx = args.argvNoOptions.indexOf('config') const verb = args.argvNoOptions[idx + 1] const change = @@ -80,13 +78,30 @@ async function doConfig(args: Arguments) { verb === 'use-context' ? args.argvNoOptions[idx + 2] : undefined ) } +} + +/** Kui proxy-side handler; we just pass it through to the PTY, and then emit a config change event */ +async function doConfig(args: Arguments) { + args.command = args.command.replace(/_config/, 'config') + args.argvNoOptions[1] = 'config' + args.argv[1] = 'config' + const response = await doExecWithPty(args) + emitChangeEventIfNeeded(args) + return response +} +/** Kui client-side handler; we pass it through to the proxy-side handler, but also emit a config change event */ +async function doConfigClient(cmd: string, verb: string, args: Arguments) { + const command = args.command.replace(/config/, '_config') + const response = await args.REPL.qexec(command) + emitChangeEventIfNeeded(args) return response } export function register(registrar: Registrar, cmd: string) { mutators.forEach(verb => { - registrar.listen(`/${cmd}/config/${verb}`, doConfig, flags) + registrar.listen(`/${cmd}/_config/${verb}`, doConfig, Object.assign({}, flags, { requiresLocal: true })) + registrar.listen(`/${cmd}/config/${verb}`, doConfigClient.bind(undefined, cmd, verb), flags) }) }