Skip to content

Commit

Permalink
fix(plugins/plugin-kubectl): improved handling of token timeout in tr…
Browse files Browse the repository at this point in the history
…ay menu
  • Loading branch information
starpit committed Sep 5, 2022
1 parent 57a3cab commit c2cdabf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions plugins/plugin-kubectl/src/tray/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { app } from 'electron'

import bug from '@kui-shell/client/icons/png/bugTemplate.png'
import grid from '@kui-shell/client/icons/png/gridTemplate.png'
import error from '@kui-shell/client/icons/png/errorTemplate.png'
import powerOff from '@kui-shell/client/icons/png/powerOffTemplate.png'

// these our are tray menu icons; the electron api specifies that if
Expand Down Expand Up @@ -48,6 +49,7 @@ function iconFor(filepath: string) {

export const bugIcon = iconFor(bug)
export const gridIcon = iconFor(grid)
export const errorIcon = iconFor(error)
export const powerOffIcon = iconFor(powerOff)
export const trayIcon = iconFor(tray)
export const trayIcon2x = iconFor(tray2x)
Expand Down
32 changes: 24 additions & 8 deletions plugins/plugin-kubectl/src/tray/menus/namespaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
* limitations under the License.
*/

import { MenuItemConstructorOptions } from 'electron'
import Debug from 'debug'
import { CreateWindowFunction } from '@kui-shell/core'
import { MenuItemConstructorOptions } from 'electron'

import Loading from '../loading'
import { get, set } from './current'
import { errorIcon } from '../../icons'
import { onRefresh } from '../../events'
import UpdateFunction from '../../update'

Expand Down Expand Up @@ -59,6 +61,10 @@ class NamespaceWatcher {
this.scan()
}

private unauthorized() {
this.namespaces = [{ label: 'Your token has probably timed out', enabled: false, icon: errorIcon }]
}

private async scan() {
try {
const currentNamespaceP = get().catch(() => '')
Expand All @@ -68,26 +74,36 @@ class NamespaceWatcher {
'kubectl',
['get', 'ns', '--no-headers', '--output=custom-columns=NAME:.metadata.name,STATUS:.status.phase', '--watch'],
{
stdio: ['inherit', 'pipe', 'inherit']
stdio: ['inherit', 'pipe', 'pipe']
}
)

child.on('exit', async code => {
if (code === 1 && this.namespaces.length === 0) {
this.namespaces = [{ label: 'Your token has probably timed out', enabled: false }]
if (
code === 1 &&
(this.namespaces.length === 0 || (this.namespaces.length === 1 && this.namespaces[0] === Loading))
) {
this.unauthorized()
}
await new Promise(resolve => setTimeout(resolve, 2000))
this.scan()
})

child.on('error', err => {
if (!/ENOENT/.test(err.message)) {
// ENOENT if kubectl is not found
console.error(err.message)
if (/Unauthorized/.test(err.message)) {
this.unauthorized()
} else {
if (!/ENOENT/.test(err.message)) {
// ENOENT if kubectl is not found
console.error(err.message)
}
this.namespaces = [{ label: '<none>', enabled: false }]
}
this.namespaces = [{ label: '<none>', enabled: false }]
})

const debug = Debug('plugin-kubectl/tray/menus/namespaces')
child.stderr.on('data', data => debug(data.toString()))

// partial line from last batch
let leftover = ''

Expand Down

0 comments on commit c2cdabf

Please sign in to comment.