Skip to content

Commit

Permalink
fix(plugins/plugin-kubectl): kubectl create ns failures do not displa…
Browse files Browse the repository at this point in the history
…y as errors in Kui
  • Loading branch information
myan9 committed Jul 1, 2021
1 parent 6472295 commit 9c199e0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 4 additions & 2 deletions plugins/plugin-kubectl/src/controller/client/direct/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

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

import { fetchFile } from '../../../lib/util/fetch-file'
import { Explained, getKindAndVersion } from '../../kubectl/explain'
Expand Down Expand Up @@ -98,7 +98,9 @@ export default async function createDirect(
if (ok.length === 0) {
// all errors? then tell the user about them (no need to re-invoke the CLI)
if (errors.length > 0 && errors.every(is404or409)) {
return errors.map(_ => _.message).join('\n')
const error: CodedError = new Error(errors.map(_ => _.message).join('\n'))
error.code = errors[0].code
throw error
}
// otherwise: intentional fall-through, returning void; let
// kubectl CLI handle the errors for now
Expand Down
4 changes: 2 additions & 2 deletions plugins/plugin-kubectl/src/controller/kubectl/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

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

import defaultFlags from './flags'
import { isDryRun, isEntityFormat, KubeOptions, formatOf } from './options'
Expand Down Expand Up @@ -52,7 +52,7 @@ export const doCreate = (verb: 'create' | 'apply', command = 'kubectl') => async
debug('createDirect falling through to CLI impl')
}
} catch (err) {
if (err.code === 404) {
if (is404or409(err)) {
throw err
} else {
console.error('Error in direct create. Falling back to CLI create.', err.code, err)
Expand Down
6 changes: 6 additions & 0 deletions plugins/plugin-kubectl/src/test/k8s2/get-pod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ commands.forEach(command => {
allocateNS(this, ns)

/** error handling starts */
it('should error out when creating an existing namespace', () => {
return CLI.command(`${command} create ns ${ns}`, this.app)
.then(ReplExpect.error(409))
.catch(Common.oops(this, true))
})

it('should command not found when kubectl is not specified', () => {
return CLI.command('get pods', this.app)
.then(ReplExpect.error(127))
Expand Down

0 comments on commit 9c199e0

Please sign in to comment.