Skip to content

Commit

Permalink
fix(plugins/plugin-kubectl): oc get projects does not allow user sele…
Browse files Browse the repository at this point in the history
…ction

Fixes #4805
  • Loading branch information
starpit committed Jun 8, 2020
1 parent 985ed25 commit 20aeacf
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
39 changes: 39 additions & 0 deletions plugins/plugin-kubectl/oc/src/controller/oc/get/projects.ts
@@ -0,0 +1,39 @@
/*
* Copyright 2020 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Arguments, Registrar } from '@kui-shell/core'
import { defaultFlags, KubeOptions, commandPrefix, doGet, getNamespacesTransformer } from '@kui-shell/plugin-kubectl'

/** Actuate a project switch by using `oc project set` */
function doSwitchViaOc(ns: string, args: Arguments<KubeOptions>) {
return args.REPL.pexec(`oc project ${args.REPL.encodeComponent(ns)}`)
}

// we use the fetcher from 'kubectl get', and the viewTransformer from 'kubectl get namespaces'
export default function registerOcProjectGet(registrar: Registrar) {
const viewTransformer = getNamespacesTransformer.bind(undefined, doSwitchViaOc)

registrar.listen(
`/${commandPrefix}/oc/get/project`,
doGet('oc'),
Object.assign({}, defaultFlags, { viewTransformer })
)
registrar.listen(
`/${commandPrefix}/oc/get/projects`,
doGet('oc'),
Object.assign({}, defaultFlags, { viewTransformer })
)
}
2 changes: 2 additions & 0 deletions plugins/plugin-kubectl/oc/src/plugin.ts
Expand Up @@ -17,11 +17,13 @@
import { Registrar } from '@kui-shell/core'

import raw from './controller/raw'
import getProjects from './controller/oc/get/projects'
import delegates from './controller/kubectl/delegates'
import catchall from './controller/kubectl/catchall'

export default (registrar: Registrar) => {
delegates(registrar)
getProjects(registrar)
raw(registrar)
catchall(registrar)
}
22 changes: 17 additions & 5 deletions plugins/plugin-kubectl/src/controller/kubectl/get-namespaces.ts
Expand Up @@ -129,8 +129,20 @@ export function t2rt({ name, attributes }: Row): RadioTableRow {
}
}

/** Function type that will actuate a namespace switch */
type SwitchFn = (ns: string, args: Arguments<KubeOptions>) => void

/** SwitchFn impl that uses `kubectl config set-context` */
const doSwitchViaKubectl: SwitchFn = (ns: string, args: Arguments<KubeOptions>) => {
return args.REPL.pexec(`kubectl config set-context --current --namespace=${ns}`)
}

/** Format as RadioTable */
async function asRadioTable(args: Arguments<KubeOptions>, { header, body }: Table): Promise<RadioTable> {
async function asRadioTable(
doSwitch: SwitchFn,
args: Arguments<KubeOptions>,
{ header, body }: Table
): Promise<RadioTable> {
const {
metadata: { namespace: currentNamespace }
} = await getCurrentContext(args.tab)
Expand All @@ -148,7 +160,7 @@ async function asRadioTable(args: Arguments<KubeOptions>, { header, body }: Tabl
Object.assign(rtRow, {
onSelect: () => {
const ns = radioTableCellToString(rtRow.cells[rtRow.nameIdx])
args.REPL.pexec(`kubectl config set-context --current --namespace=${ns}`)
doSwitch(ns, args)
}
})
)
Expand All @@ -171,10 +183,10 @@ async function asRadioTable(args: Arguments<KubeOptions>, { header, body }: Tabl
}

/** Table -> RadioTable view transformer */
function viewTransformer(args: Arguments<KubeOptions>, response: KResponse) {
export function viewTransformer(doSwitch: SwitchFn, args: Arguments<KubeOptions>, response: KResponse) {
if (isTable(response)) {
if (isTableRequest(args) && !isWatchRequest(args)) {
return asRadioTable(args, response)
return asRadioTable(doSwitch, args, response)
} else {
return response
}
Expand All @@ -190,7 +202,7 @@ function viewTransformer(args: Arguments<KubeOptions>, response: KResponse) {
* a RadioTable.
*
*/
const rtFlags = Object.assign({}, flags, { viewTransformer })
const rtFlags = Object.assign({}, flags, { viewTransformer: viewTransformer.bind(undefined, doSwitchViaKubectl) })

export default (commandTree: Registrar) => {
commandTree.listen(`/${commandPrefix}/kubectl/get/namespaces`, doGet('kubectl'), rtFlags)
Expand Down
3 changes: 2 additions & 1 deletion plugins/plugin-kubectl/src/index.ts
Expand Up @@ -91,7 +91,7 @@ export { fqnOf, fqn } from './controller/kubectl/fqn'
*
*/
export { register as registerEdit } from './controller/kubectl/edit'
export { getter } from './controller/kubectl/get'
export { doGet, getter } from './controller/kubectl/get'
export { doRun } from './controller/kubectl/run'
export { doCreate } from './controller/kubectl/create'
export { doDelete } from './controller/kubectl/delete'
Expand All @@ -100,6 +100,7 @@ export { register as registerConfig } from './controller/kubectl/config'
export { registerApplySubcommands } from './controller/kubectl/apply-subcommands'

export { viewTransformer as getTransformer } from './controller/kubectl/get'
export { viewTransformer as getNamespacesTransformer } from './controller/kubectl/get-namespaces'

/** A channel that covers *possible* changes to kubectl config */
export { onKubectlConfigChangeEvents, offKubectlConfigChangeEvents } from './controller/kubectl/config'
Expand Down

0 comments on commit 20aeacf

Please sign in to comment.