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

Commit efd0db0

Browse files
myan9k8s-ci-robot
authored andcommitted
fix(plugins/plugin-kubectl): kubectl create ns failures do not display as errors in Kui
1 parent 6472295 commit efd0db0

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

plugins/plugin-kubectl/src/controller/client/direct/create.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import Debug from 'debug'
18-
import { Arguments, Table, isTable, is404or409 } from '@kui-shell/core'
18+
import { Arguments, CodedError, Table, isTable, is404or409 } from '@kui-shell/core'
1919

2020
import { fetchFile } from '../../../lib/util/fetch-file'
2121
import { Explained, getKindAndVersion } from '../../kubectl/explain'
@@ -98,7 +98,9 @@ export default async function createDirect(
9898
if (ok.length === 0) {
9999
// all errors? then tell the user about them (no need to re-invoke the CLI)
100100
if (errors.length > 0 && errors.every(is404or409)) {
101-
return errors.map(_ => _.message).join('\n')
101+
const error: CodedError = new Error(errors.map(_ => _.message).join('\n'))
102+
error.code = errors[0].code
103+
throw error
102104
}
103105
// otherwise: intentional fall-through, returning void; let
104106
// kubectl CLI handle the errors for now

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import Debug from 'debug'
18-
import { Registrar, Arguments } from '@kui-shell/core'
18+
import { Registrar, Arguments, is404or409 } from '@kui-shell/core'
1919

2020
import defaultFlags from './flags'
2121
import { isDryRun, isEntityFormat, KubeOptions, formatOf } from './options'
@@ -52,7 +52,7 @@ export const doCreate = (verb: 'create' | 'apply', command = 'kubectl') => async
5252
debug('createDirect falling through to CLI impl')
5353
}
5454
} catch (err) {
55-
if (err.code === 404) {
55+
if (is404or409(err)) {
5656
throw err
5757
} else {
5858
console.error('Error in direct create. Falling back to CLI create.', err.code, err)

plugins/plugin-kubectl/src/test/k8s2/get-pod.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ commands.forEach(command => {
6767
allocateNS(this, ns)
6868

6969
/** error handling starts */
70+
it('should error out when creating an existing namespace', () => {
71+
return CLI.command(`${command} create ns ${ns}`, this.app)
72+
.then(ReplExpect.error(409))
73+
.catch(Common.oops(this, true))
74+
})
75+
7076
it('should command not found when kubectl is not specified', () => {
7177
return CLI.command('get pods', this.app)
7278
.then(ReplExpect.error(127))

0 commit comments

Comments
 (0)