Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit ed0374f

Browse files
committed
fix(plugins/plugin-kubectl): kubectl logs -l with no matches yields internal error
Fixes #4811
1 parent d4cf08c commit ed0374f

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

plugins/plugin-kubectl/i18n/logs_en_US.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"Message": "Message",
88
"Details": "Details",
99
"No log data": "No log data",
10+
"No matching pods": "No matching pods",
1011
"Logs are live streaming. Showing all containers.": "Logs are live **streaming**. Showing **all containers**.",
1112
"Log streaming is paused. Showing all containers.": "Log streaming is **paused**. Showing **all containers**.",
1213
"Log streaming stopped. Showing all containers.": "Log streaming **stopped**. Showing **all containers**.",

plugins/plugin-kubectl/logs/src/controller/kubectl/logs.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Arguments, CodedError, ExecType, MultiModalResponse, Registrar, flatten } from '@kui-shell/core'
17+
import { Arguments, CodedError, ExecType, MultiModalResponse, Registrar, flatten, i18n } from '@kui-shell/core'
1818
import {
1919
isUsage,
2020
doHelp,
@@ -28,6 +28,7 @@ import {
2828
getContainer,
2929
getNamespace,
3030
KubeItems,
31+
isKubeItems,
3132
isKubeItemsOfKind,
3233
KubeResource,
3334
Pod,
@@ -36,6 +37,8 @@ import {
3637

3738
import commandPrefix from '../command-prefix'
3839

40+
const strings = i18n('plugin-kubectl', 'logs')
41+
3942
interface LogOptions extends KubeOptions {
4043
f: string
4144
follow: string
@@ -142,6 +145,11 @@ function viewTransformer(defaultMode: string) {
142145
return async (args: Arguments<KubeOptions>, response: KubeResource | KubeItems<Pod>) => {
143146
if (isKubeItemsOfKind(response, isPod)) {
144147
return transformMulti(defaultMode, args, response)
148+
} else if (isKubeItems(response)) {
149+
// otherwise, we have an empty list of items
150+
const error: CodedError = new Error(strings('No matching pods'))
151+
error.code = 404
152+
throw error
145153
}
146154

147155
if (isPod(response)) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2020 IBM Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { Common, CLI, ReplExpect } from '@kui-shell/test'
18+
import { createNS, allocateNS, deleteNS } from '@kui-shell/plugin-kubectl/tests/lib/k8s/utils'
19+
20+
describe(`kubectl logs via label ${process.env.MOCHA_RUN_TARGET || ''}`, function(this: Common.ISuite) {
21+
before(Common.before(this))
22+
after(Common.after(this))
23+
24+
const ns: string = createNS()
25+
26+
allocateNS(this, ns)
27+
28+
it('should fail with 404 when querying with bogus label', () =>
29+
CLI.command(`kubectl logs -lxxxxxxxxx=yyyyyyyyyyy -n ${ns}`, this.app)
30+
.then(ReplExpect.error(404))
31+
.catch(Common.oops(this, true)))
32+
33+
deleteNS(this, ns)
34+
})

0 commit comments

Comments
 (0)