Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugins/plugin-kubectl): kubectl create ns failures do not display as errors in Kui #7756

Merged
merged 1 commit into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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