Skip to content

Commit

Permalink
Better error handling in pods table widget
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Apr 29, 2021
1 parent fba84d3 commit 0616035
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions lib/ui/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ class Dashboard extends EventEmitter {
},
});

let pods_table_text;
function pods_table_message(text, options = {}) {
if (pods_table_text) pods_table_text.destroy();
pods_table_text = blessed.text(Object.assign({
parent : pods_table,
tags : true,
top : 'center',
left : 'center',
content : text,
}, options));
}

const resources = blessed.box({
label : 'Resources',
parent : screen,
Expand Down Expand Up @@ -461,12 +473,10 @@ class Dashboard extends EventEmitter {
.catch(error => {
// cancelled
if (!error) return;

// let's skip expected errors if the pod is being deleted
if (k8s.isPodTerminating(getPodByUid(pod.metadata.uid)) && (error.name === 'Kubebox' || error.response)) {
return;
}

if (error.name === 'Kubebox') {
graphs.forEach(g => g.message(error.message));
} else if (error.response) {
Expand All @@ -482,6 +492,7 @@ class Dashboard extends EventEmitter {
} else {
console.error(error.stack);
}
screen.render();
});
}

Expand Down Expand Up @@ -671,13 +682,18 @@ class Dashboard extends EventEmitter {
this.reset = function (page) {
// cancel current running tasks and open requests
cancellations.run('dashboard');
// reset state
current_namespace = null;
pod_selected = null;
container_selected = null;
pods_list = {};
// reset dashboard widgets
pods_table.setLabel('Pods');
pods_table.setData([]);
if (pods_table_text) {
pods_table_text.destroy();
pods_table_text = null;
}
// reset focus
if (pods_table.detached) {
page.focus = pods_table;
Expand All @@ -696,39 +712,49 @@ class Dashboard extends EventEmitter {
current_namespace = namespace;
let listPodsError;
// FIXME: should be cancellable
const promise = until(client.pods(current_namespace).get())
const promise = until(client.pods(namespace).get())
.do(pods_table, pods_table.setLabel)
.spin(s => `${s} Pods {grey-fg}[${current_namespace}]{/grey-fg}`)
.done(_ => `Pods {grey-fg}[${current_namespace}]{/grey-fg}`)
.spin(s => `${s} Pods {grey-fg}[${namespace}]{/grey-fg}`)
.done(_ => `Pods {grey-fg}[${namespace}]{/grey-fg}`)
.then(response => {
pods_list = JSON.parse(response.body.toString('utf8'));
pods_list.items = pods_list.items || [];
updatePodsTable();
}).catch(error => {
listPodsError = error;
pods_table_message(`{red-bg}Error: ${error.message}{/red-bg}`);
screen.render();
return Promise.reject(error);
});

promise
.then(() => {
console.debug(`Watching for pods changes in namespace ${current_namespace} ...`);
console.debug(`Watching for pods changes in namespace ${namespace} ...`);
screen.render();
const id = setInterval(refreshPodAges, 1000);
cancellations.add('dashboard.refreshPodAges', () => clearInterval(id));
const { promise, cancellation } = client.pods(current_namespace)
const { promise, cancellation } = client.pods(namespace)
.watch(pods_list.metadata.resourceVersion)
.get({ generator: watchPodChanges });
.get({
generator: function* () {
yield* watchPodChanges(namespace);
}
});
cancellations.add('dashboard', cancellation);
return promise;
})
.catch(error => {
if (!listPodsError) console.error(error.stack);
if (!listPodsError) {
pods_table_message(`{red-bg}Error: ${error.message}{/red-bg}`);
console.error(error.stack);
screen.render();
}
});

return promise;
}

function* watchPodChanges() {
function* watchPodChanges(namespace) {
const index = object => pods_list.items.findIndex(pod => pod.metadata.uid === object.metadata.uid);
let change;
try {
Expand Down Expand Up @@ -785,7 +811,7 @@ class Dashboard extends EventEmitter {
}
// retry the pods list watch request
cancellations.run('dashboard.refreshPodAges');
dashboard.run(current_namespace).catch(error => console.error(error.stack));
dashboard.run(namespace).catch(error => console.error(error.stack));
}

function refreshPodAges() {
Expand Down

0 comments on commit 0616035

Please sign in to comment.