astefanutti / kubebox

⎈❏ Terminal and Web console for Kubernetes
http://astefanutti.github.io/kubebox
MIT License
2.15k stars 142 forks source link

Add fields to pods table to match kubectl get pod -owide #44

Open 593769290 opened 5 years ago

593769290 commented 5 years ago

Add some fields to pods_table, as result, it looks like kubectl get pod -owide. 'NAME', 'STATUS', 'READY', 'RESTARTS', 'AGE', 'IP', 'NODE'.

593769290 commented 5 years ago

some code to make it work:

lib/ui/dashboard.js

    function updatePodsTable(pods) {
      const selected = pods_table.selected;
      pods_table.setData(pods.items.reduce((data, pod) => {
        const uid = pod.metadata.uid;
        data.push([
          uid === pod_selected ? `{blue-fg}${pod.metadata.name}{/blue-fg}` : pod.metadata.name,
          // TODO: add a visual hint depending on the status
          k8s.podStatus(pod),
          k8s.podReadyCount(pod),
          k8s.podRestartsCount(pod),
          // FIXME: negative duration is displayed when pod starts as clocks may not be synced
          util.formatDuration(moment.duration(moment().diff(moment(pod.status.startTime)))),
          pod.status.podIP,
          pod.status.hostIP
        ]);
        return data;
      }, [['NAME', 'STATUS', 'READY', 'RESTARTS', 'AGE', 'IP', 'NODE']]));
      pods_table.select(selected);
    }

lib/kubernetes.js

module.exports.podReadyCount = function (pod) {
  let readycount = 0;
  let allcount = 0;
    if (pod.status.initContainerStatuses){
        (pod.status.initContainerStatuses || []).forEach(container => {
          allcount++;
          const state = container.state;
          if (state.terminated && state.terminated.exitCode === 0) {
              readycount++;
          }
        });
    }else{
        (pod.status.containerStatuses || []).forEach(container => {
          allcount++;
          const state = container.state;
          if (container.ready && state.running) {
              readycount++;
          }
        });
    }

    return ''+readycount+'/'+allcount;
}

module.exports.podRestartsCount = function (pod) {
  let recount = 0;
  (pod.status.containerStatuses || []).forEach(container => {
    if (container.restartCount){
        recount+= container.restartCount;
    }
  });
  return ''+recount;
}
astefanutti commented 5 years ago

Thanks for the suggestion. This is something we plan to do.

Actually, I was thinking the output of kubectl get pod -owide could be used directly. As I researched a way to solve the issue when the client clock is not in sync with the server clock, I found it is the only way to retrieve the correct pod ages.

593769290 commented 5 years ago

to solve the problem that client clock is not in sync with server clock.

think about change 'AGE' field to ‘STARTTIME’, or just add a field ‘STARTTIME’.

astefanutti commented 5 years ago

Indeed, replacing the age by the start time would be a good idea in case there is no way to retrieve the age server-side.