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

Commit 1e3d60f

Browse files
committed
feat(plugins/plugin-kubectl): CLI-based kubectl polling does not allow for flexible overrides of the command prefix
Fixes #7042
1 parent f424b5e commit 1e3d60f

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

plugins/plugin-kubectl/src/controller/kubectl/options.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Arguments, ExecOptions, ParsedOptions } from '@kui-shell/core'
1818

1919
import { FinalState } from '../../lib/model/states'
2020
import { getCurrentDefaultNamespace } from './contexts'
21+
import { getCommandFromArgs } from '../../lib/util/util'
2122

2223
type EntityFormat = 'yaml' | 'json'
2324
type TableFormat = 'wide' | string // want: 'custom-columns-file=' | 'custom-columns='
@@ -345,6 +346,22 @@ export function withKubeconfigFrom(args: Pick<Arguments<KubeOptions>, 'parsedOpt
345346
}
346347
}
347348

349+
function execOptionsHasPrefix(data: Arguments['execOptions']['data']): data is { kubectlPrefix: string } {
350+
return !Buffer.isBuffer(data) && typeof data === 'object' && typeof data.kubectlPrefix === 'string'
351+
}
352+
353+
/** As with `withKubeconfigFrom`, and also copy over the leading command (e.g. `kubectl`) */
354+
export function withKubeconfigAndCommandFrom(
355+
args: Pick<Arguments<KubeOptions>, 'argvNoOptions' | 'execOptions' | 'parsedOptions'>,
356+
cmdline: string
357+
): string {
358+
if (execOptionsHasPrefix(args.execOptions.data)) {
359+
return withKubeconfigFrom(args, `${args.execOptions.data.kubectlPrefix} ${cmdline}`)
360+
} else {
361+
return withKubeconfigFrom(args, `${getCommandFromArgs(args)} ${cmdline}`)
362+
}
363+
}
364+
348365
/** Apply --dry-run? */
349366
export function isDryRun(args: Arguments<KubeOptions>): boolean {
350367
const opt = args.parsedOptions['dry-run']

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
getNamespace,
4141
getResourceNamesForArgv,
4242
withKubeconfigFrom,
43+
withKubeconfigAndCommandFrom,
4344
KubeOptions,
4445
KubeExecOptions
4546
} from '../options'
@@ -337,9 +338,9 @@ class KubectlWatcher implements Abortable, Watcher {
337338
namespace: string,
338339
rowNames: string[]
339340
): Promise<Table | void> {
340-
const getCommand = withKubeconfigFrom(
341+
const getCommand = withKubeconfigAndCommandFrom(
341342
this.args,
342-
`${getCommandFromArgs(this.args)} get ${kindPart(apiVersion, kind)} ${rowNames.join(' ')} -n ${namespace} ${
343+
`get ${kindPart(apiVersion, kind)} ${rowNames.join(' ')} -n ${namespace} ${
343344
this.output ? `-o ${this.output}` : ''
344345
}`
345346
)

plugins/plugin-kubectl/src/lib/util/util.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
1817
import { Arguments } from '@kui-shell/core'
1918

2019
import commandPrefix from '../../controller/command-prefix'
@@ -105,16 +104,16 @@ export class StatusError extends Error {}
105104
export class TryLaterError extends StatusError {}
106105

107106
export class NotFoundError extends StatusError {
108-
code: any // eslint-disable-line @typescript-eslint/no-explicit-any
107+
public code: any // eslint-disable-line @typescript-eslint/no-explicit-any
109108

110109
// eslint-disable-next-line @typescript-eslint/no-explicit-any
111-
constructor(message: string, code: any = 404) {
110+
public constructor(message: string, code: any = 404) {
112111
super(message)
113112
this.code = code
114113
}
115114
}
116115

117-
export const getCommandFromArgs = (args: { argvNoOptions: string[] }) => {
116+
export const getCommandFromArgs = (args: Pick<Arguments, 'argvNoOptions'>): string => {
118117
const cmd = args.argvNoOptions[0] === commandPrefix ? args.argvNoOptions[1] : args.argvNoOptions[0]
119118
return cmd === 'k' ? 'kubectl' : cmd
120119
}

0 commit comments

Comments
 (0)