Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Add pods logs. Close #13
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienBreux committed Aug 15, 2017
1 parent 0c6b9ae commit 931e5a1
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 43 deletions.
72 changes: 39 additions & 33 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,63 +31,69 @@ func actionGlobalToggleDebug(g *gocui.Gui, v *gocui.View) error {
// View pods: Up
func actionViewPodsUp(g *gocui.Gui, v *gocui.View) error {
moveViewCursorUp(g, v, 2)
line, err := getViewLine(g, v)
POD = getPodNameFromLine(line)
debug(g, " - Select up in pods view: "+POD)
return err
debug(g, "Select up in pods view")
return nil
}

// View pods: Down
func actionViewPodsDown(g *gocui.Gui, v *gocui.View) error {
moveViewCursorDown(g, v, false)
line, err := getViewLine(g, v)
POD = getPodNameFromLine(line)
debug(g, " - Select down in pods view: "+POD)
return err
debug(g, "Select down in pods view")
return nil
}

// View pods: Delete
func actionViewPodsDelete(g *gocui.Gui, v *gocui.View) error {
debug(g, "Delete pod: "+POD)
err := deletePod(POD)
p, err := getSelectedPod(g)
if err != nil {
return err
}

go viewPodsRefreshList(g)
if err := deletePod(p); err != nil {
return err
}

return err
}
debug(g, "Delete pod: "+p)

// Show views logs
func showViewLogs(g *gocui.Gui, v *gocui.View) error {
vn := "logs"

debug(g, "Action: Show view logs")
g.SetViewOnTop(vn)
g.SetViewOnTop(vn + "-containers")
g.SetCurrentView(vn)

// TODO Enable logs
switch LOG_MOD {
case "pod":
v, err := g.View(vn)
if err != nil {
return err
}
getPodLogs(POD, v)
}
go viewPodsRefreshList(g)

return nil
}

// View pods: Logs
func actionViewPodsLogs(g *gocui.Gui, v *gocui.View) error {
LOG_MOD = "pod"
err := showViewLogs(g, v)
err := showViewPodsLogs(g)

return err
}

// View pod logs: Up
func actionViewPodsLogsUp(g *gocui.Gui, v *gocui.View) error {
vLc, err := g.View("logs-containers")
if err != nil {
return err
}
moveViewCursorUp(g, vLc, 0)
refreshPodsLogs(g)
debug(g, "Select up in logs view")
return nil
}

// View pod logs: Down
func actionViewPodsLogsDown(g *gocui.Gui, v *gocui.View) error {
vLc, err := g.View("logs-containers")
if err != nil {
return err
}
moveViewCursorDown(g, vLc, false)
refreshPodsLogs(g)
debug(g, "Select down in logs view")
return nil
}

// View logs: Hide
func actionViewLogsHide(g *gocui.Gui, v *gocui.View) error {
func actionViewPodsLogsHide(g *gocui.Gui, v *gocui.View) error {
g.SetViewOnBottom("logs")
g.SetViewOnBottom("logs-containers")
g.SetCurrentView("pods")
Expand Down
28 changes: 23 additions & 5 deletions kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,46 @@ func getPods() (*v1.PodList, error) {
return cs.CoreV1().Pods(NAMESPACE).List(metav1.ListOptions{})
}

// Get the pod containers
func getPodContainers(p string) []string {
var pc []string
cs := getClientSet()

pod, _ := cs.CoreV1().Pods(NAMESPACE).Get(p, metav1.GetOptions{})
for _, c := range pod.Spec.Containers {
pc = append(pc, c.Name)
}

return pc
}

// Delete pod
func deletePod(p string) error {
cs := getClientSet()

return cs.CoreV1().Pods(NAMESPACE).Delete(p, &metav1.DeleteOptions{})
}

// Get pod logs
func getPodLogs(p string, out io.Writer) error {
// Get pod container logs
func getPodContainerLogs(p string, c string, o io.Writer) error {
tl := int64(50)
cs := getClientSet()

opts := &v1.PodLogOptions{}
opts := &v1.PodLogOptions{
Container: c,
TailLines: &tl,
}

req := cs.CoreV1().Pods(NAMESPACE).GetLogs(p, opts)

readCloser, err := req.Stream()
if err != nil {
return err
}
defer readCloser.Close()

_, err = io.Copy(out, readCloser)
_, err = io.Copy(o, readCloser)

readCloser.Close()

return err
}
Expand Down
84 changes: 82 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
var DEBUG_DISPLAYED bool = false
var LOG_MOD string = "pod"
var NAMESPACE string = "default"
var POD string = ""

// Configure globale keys
var keys []Key = []Key{
Expand All @@ -22,7 +21,9 @@ var keys []Key = []Key{
Key{"pods", gocui.KeyArrowDown, actionViewPodsDown},
Key{"pods", 'd', actionViewPodsDelete},
Key{"pods", 'l', actionViewPodsLogs},
Key{"logs", 'l', actionViewLogsHide},
Key{"logs", 'l', actionViewPodsLogsHide},
Key{"logs", gocui.KeyArrowUp, actionViewPodsLogsUp},
Key{"logs", gocui.KeyArrowDown, actionViewPodsLogsDown},
}

// Main or not main, that's the question^^
Expand Down Expand Up @@ -159,3 +160,82 @@ func getPodNameFromLine(line string) string {

return line[0:i]
}

// Get selected pod
func getSelectedPod(g *gocui.Gui) (string, error) {
v, err := g.View("pods")
if err != nil {
return "", err
}
l, err := getViewLine(g, v)
if err != nil {
return "", err
}
p := getPodNameFromLine(l)

return p, nil
}

// Show views logs
func showViewPodsLogs(g *gocui.Gui) error {
vn := "logs"

switch LOG_MOD {
case "pod":
// Get current selected pod
p, err := getSelectedPod(g)
if err != nil {
return err
}

// Display pod containers
vLc, err := g.View(vn + "-containers")
if err != nil {
return err
}
vLc.Clear()
for _, c := range getPodContainers(p) {
fmt.Fprintln(vLc, c)
}
vLc.SetCursor(0, 0)

// Display logs
refreshPodsLogs(g)
}

debug(g, "Action: Show view logs")
g.SetViewOnTop(vn)
g.SetViewOnTop(vn + "-containers")
g.SetCurrentView(vn)

return nil
}

// Refresh pods logs
func refreshPodsLogs(g *gocui.Gui) error {
vn := "logs"

// Get current selected pod
p, err := getSelectedPod(g)
if err != nil {
return err
}

vLc, err := g.View(vn + "-containers")
if err != nil {
return err
}

c, err := getViewLine(g, vLc)
if err != nil {
return err
}

vL, err := g.View(vn)
if err != nil {
return err
}
getPodContainerLogs(p, c, vL)

return nil
}
12 changes: 9 additions & 3 deletions views.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,20 @@ func viewLogs(g *gocui.Gui, lMaxX int, lMaxY int) error {
}

// Containers view
if v, err := g.SetView("logs-containers", 2, 2, lMaxX-4, lMaxY-2); err != nil {
//w := int(lMaxX / 4)
minX := int(lMaxX/5) * 4
minY := 2
maxX := lMaxX - 4
maxY := int(lMaxY / 5)
if v, err := g.SetView("logs-containers", minX, minY, maxX, maxY); err != nil {
if err != gocui.ErrUnknownView {
return err
}

// Settings
v.Frame = false
v.BgColor = gocui.ColorRed
v.Frame = true
v.BgColor = gocui.ColorBlack
v.Highlight = true
}

return nil
Expand Down

0 comments on commit 931e5a1

Please sign in to comment.