From 20aeacf32afbf74dc8efae0f251c20662e723e0b Mon Sep 17 00:00:00 2001 From: Nick Mitchell Date: Mon, 8 Jun 2020 11:00:17 -0400 Subject: [PATCH] fix(plugins/plugin-kubectl): oc get projects does not allow user selection Fixes #4805 --- .../oc/src/controller/oc/get/projects.ts | 39 +++++++++++++++++++ plugins/plugin-kubectl/oc/src/plugin.ts | 2 + .../src/controller/kubectl/get-namespaces.ts | 22 ++++++++--- plugins/plugin-kubectl/src/index.ts | 3 +- 4 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 plugins/plugin-kubectl/oc/src/controller/oc/get/projects.ts diff --git a/plugins/plugin-kubectl/oc/src/controller/oc/get/projects.ts b/plugins/plugin-kubectl/oc/src/controller/oc/get/projects.ts new file mode 100644 index 00000000000..665be5d861f --- /dev/null +++ b/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) { + 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 }) + ) +} diff --git a/plugins/plugin-kubectl/oc/src/plugin.ts b/plugins/plugin-kubectl/oc/src/plugin.ts index bc16be603c9..03c43c00604 100644 --- a/plugins/plugin-kubectl/oc/src/plugin.ts +++ b/plugins/plugin-kubectl/oc/src/plugin.ts @@ -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) } diff --git a/plugins/plugin-kubectl/src/controller/kubectl/get-namespaces.ts b/plugins/plugin-kubectl/src/controller/kubectl/get-namespaces.ts index 8bfcf1327b6..35c34930bae 100644 --- a/plugins/plugin-kubectl/src/controller/kubectl/get-namespaces.ts +++ b/plugins/plugin-kubectl/src/controller/kubectl/get-namespaces.ts @@ -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) => void + +/** SwitchFn impl that uses `kubectl config set-context` */ +const doSwitchViaKubectl: SwitchFn = (ns: string, args: Arguments) => { + return args.REPL.pexec(`kubectl config set-context --current --namespace=${ns}`) +} + /** Format as RadioTable */ -async function asRadioTable(args: Arguments, { header, body }: Table): Promise { +async function asRadioTable( + doSwitch: SwitchFn, + args: Arguments, + { header, body }: Table +): Promise { const { metadata: { namespace: currentNamespace } } = await getCurrentContext(args.tab) @@ -148,7 +160,7 @@ async function asRadioTable(args: Arguments, { 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) } }) ) @@ -171,10 +183,10 @@ async function asRadioTable(args: Arguments, { header, body }: Tabl } /** Table -> RadioTable view transformer */ -function viewTransformer(args: Arguments, response: KResponse) { +export function viewTransformer(doSwitch: SwitchFn, args: Arguments, response: KResponse) { if (isTable(response)) { if (isTableRequest(args) && !isWatchRequest(args)) { - return asRadioTable(args, response) + return asRadioTable(doSwitch, args, response) } else { return response } @@ -190,7 +202,7 @@ function viewTransformer(args: Arguments, 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) diff --git a/plugins/plugin-kubectl/src/index.ts b/plugins/plugin-kubectl/src/index.ts index a87207b4c25..b17e33365ce 100644 --- a/plugins/plugin-kubectl/src/index.ts +++ b/plugins/plugin-kubectl/src/index.ts @@ -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' @@ -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'