Skip to content

Commit

Permalink
fix: avoid using kui-custom command for switching kubectl namespace
Browse files Browse the repository at this point in the history
Fixes #4806
  • Loading branch information
starpit committed Jun 6, 2020
1 parent 3fa608d commit 872029d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 34 deletions.
34 changes: 1 addition & 33 deletions plugins/plugin-kubectl/src/controller/kubectl/get-namespaces.ts
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import Debug from 'debug'
import {
Arguments,
Registrar,
Expand All @@ -35,8 +34,6 @@ import { doGet, doGetAsMMR, getFlags as flags } from './get'
import { isKubeResource } from '../../lib/model/resource'
import { KubeOptions, isTableRequest, isWatchRequest } from './options'

const debug = Debug('plugin-kubectl/controller/kubectl/get/namespace')

/**
* Summarize the resources in the namespace indicated by the last
* positional argument into a table, where resources are histogrammed
Expand Down Expand Up @@ -89,33 +86,6 @@ function doSummarizeNamespace(args: Arguments<KubeOptions>): Promise<Table> {
return doSummarizeNamedNamespace(args.tab, ns)
}

/**
* Switch to the namespace indicated by the last positional argument,
* then summarize the resources in that namespace in a table.
*
*/
async function doSwitchNamespace(args: Arguments<KubeOptions>): Promise<true | Table> {
// switch to this namespace
const ns = args.argvNoOptions[args.argvNoOptions.length - 1]

// this does the actual switch
await args.REPL.qexec(`kubectl config set-context --current --namespace=${ns}`)

let summarizeNamespaceOnSwitch = false
try {
summarizeNamespaceOnSwitch = require('@kui-shell/client/config.d/kubectl.json').summarizeNamespaceOnSwitch
} catch (err) {
debug('using default value for summarizeNamespaceOnSwitch', err)
}

if (!summarizeNamespaceOnSwitch) {
// client config told us not to summarize namespace on switch
return true
}

return doSummarizeNamespace(args)
}

/**
* @return the currently active namespace in the currently selected context
*
Expand Down Expand Up @@ -178,7 +148,7 @@ async function asRadioTable(args: Arguments<KubeOptions>, { header, body }: Tabl
Object.assign(rtRow, {
onSelect: () => {
const ns = radioTableCellToString(rtRow.cells[rtRow.nameIdx])
args.REPL.pexec(`namespace switch ns ${ns}`)
args.REPL.pexec(`kubectl config set-context --current --namespace=${ns}`)
}
})
)
Expand Down Expand Up @@ -232,6 +202,4 @@ export default (commandTree: Registrar) => {

commandTree.listen(`/${commandPrefix}/namespace/current`, doGetCurrentNamespace, flags)
commandTree.listen(`/${commandPrefix}/namespace/summarize`, doSummarizeNamespace, flags)
commandTree.listen(`/${commandPrefix}/namespace/switch`, doSwitchNamespace, flags)
commandTree.listen(`/${commandPrefix}/ns/switch`, doSwitchNamespace, flags)
}
2 changes: 1 addition & 1 deletion plugins/plugin-kubectl/src/test/k8s1/namespace.ts
Expand Up @@ -62,7 +62,7 @@ describe(`kubectl namespace ${process.env.MOCHA_RUN_TARGET || ''}`, function(thi
/** switch to default context via command */
const switchToDefault = () => {
it('should switch back to default via command', () => {
return CLI.command(`namespace switch default`, this.app)
return CLI.command(`kubectl config set-context --current --namespace=default`, this.app)
.then(ReplExpect.okWithAny)
.catch(Common.oops(this, true))
})
Expand Down

0 comments on commit 872029d

Please sign in to comment.