Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit 12e578a

Browse files
myan9starpit
authored andcommitted
fix: (plugins/plugin-kubectl): kubectl get pod -w --all-namespaces behaves oddly
Fixes #5169
1 parent 4cc16be commit 12e578a

File tree

1 file changed

+43
-13
lines changed
  • plugins/plugin-kubectl/src/controller/kubectl/watch

1 file changed

+43
-13
lines changed

plugins/plugin-kubectl/src/controller/kubectl/watch/get-watch.ts

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@ import {
3232

3333
import { kindPart } from '../fqn'
3434
import { getKind } from '../explain'
35-
import { formatOf, getLabel, getNamespace, getResourceNamesForArgv, KubeOptions, KubeExecOptions } from '../options'
35+
import { formatOf, isForAllNamespaces, getLabel, getNamespace, getResourceNamesForArgv, KubeOptions, KubeExecOptions } from '../options'
3636

3737
import { getCommandFromArgs } from '../../../lib/util/util'
38-
import { Pair, getNamespaceBreadcrumbs } from '../../../lib/view/formatTable'
38+
import {
39+
Pair,
40+
getNamespaceBreadcrumbs,
41+
preprocessTable as preFormatTables,
42+
formatTable
43+
} from '../../../lib/view/formatTable'
3944

4045
const strings = i18n('plugin-kubectl', 'events')
4146
const debug = Debug('plugin-kubectl/controller/watch/watcher')
@@ -260,6 +265,27 @@ class KubectlWatcher implements Abortable, Watcher {
260265
}, {} as Record<string, Pair[][]>)
261266
}
262267

268+
/**
269+
* a resource cannot be retrieved by name across all namespaces,
270+
* so we need to append the namespace column to match the inital
271+
* all-namespaces table
272+
* see issue: https://github.com/IBM/kui/issues/5169
273+
*
274+
*/
275+
private async allNamespaceOverride(namespace: string, getCommand: string, kind: string) {
276+
const rawData = await this.args.REPL.qexec<string>(`sendtopty ${getCommand.replace(/^k(\s)/, 'kubectl$1')}`)
277+
const preTables = preFormatTables(rawData.split(/^(?=LAST SEEN|NAMESPACE|NAME\s+)/m))
278+
const allNamespacesTable = preTables[0].map((pairs, idx) => {
279+
if (idx === 0) {
280+
return [{ key: 'NAMESPACE', value: 'NAMESPACE' }].concat(pairs)
281+
} else {
282+
return [{ key: 'NAMESPACE', value: namespace }].concat(pairs)
283+
}
284+
})
285+
286+
return formatTable(getCommandFromArgs(this.args), 'get', kind, this.args, allNamespacesTable)
287+
}
288+
263289
/**
264290
* Fetch the user-formatted rows for the so-named resources in the
265291
* given namespace.
@@ -275,17 +301,21 @@ class KubectlWatcher implements Abortable, Watcher {
275301
' '
276302
)} -n ${namespace} ${this.output ? `-o ${this.output}` : ''}`
277303

278-
return this.args.REPL.qexec<Table>(getCommand).catch((err: CodedError) => {
279-
if (err.code !== 404) {
280-
console.error(err)
281-
}
282-
// mark as all offline, if we got a 404 for the bulk get
283-
if (typeof rowNames === 'string') {
284-
this.pusher.offline(rowNames)
285-
} else {
286-
rowNames.forEach(name => this.pusher.offline(name))
287-
}
288-
})
304+
if (isForAllNamespaces(this.args.parsedOptions)) {
305+
return this.allNamespaceOverride(namespace, getCommand, kind)
306+
} else {
307+
return this.args.REPL.qexec<Table>(getCommand).catch((err: CodedError) => {
308+
if (err.code !== 404) {
309+
console.error(err)
310+
}
311+
// mark as all offline, if we got a 404 for the bulk get
312+
if (typeof rowNames === 'string') {
313+
this.pusher.offline(rowNames)
314+
} else {
315+
rowNames.forEach(name => this.pusher.offline(name))
316+
}
317+
})
318+
}
289319
}
290320

291321
/** Get rows as specified by user's -o */

0 commit comments

Comments
 (0)