Skip to content

Commit

Permalink
Add K8S CLI and pods list. TODO Goroutine / Memory / CPU. #10
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienBreux committed Aug 14, 2017
1 parent f12141d commit 3431587
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 6 deletions.
98 changes: 98 additions & 0 deletions kubernetes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"time"

"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func getClientSet() *kubernetes.Clientset {
// Use the current context in kubeconfig
c, err := clientcmd.BuildConfigFromFlags("", *getKubeConfig())
if err != nil {
panic(err.Error())
}

// Create the client set
cs, err := kubernetes.NewForConfig(c)
if err != nil {
panic(err.Error())
}

return cs
}

func getKubeConfig() *string {
var kc *string
if h := getHomeDir(); h != "" {
kc = flag.String("kubeconfig", filepath.Join(h, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kc = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()

return kc
}

func getHomeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}

func getPods() (*v1.PodList, error) {
cs := getClientSet()
return cs.CoreV1().Pods(NAMESPACE).List(metav1.ListOptions{})
}

func columnHelperRestarts(cs []v1.ContainerStatus) string {
r := 0
for _, c := range cs {
r = r + int(c.RestartCount)
}
return strconv.Itoa(r)
}

func columnHelperAge(t metav1.Time) string {
delta := time.Now().Sub(t.Time)

if delta.Hours() > 1 {
if delta.Hours() > 24 {
d := float64(delta.Hours() / 24)
return fmt.Sprintf("%.0fd", d)
} else {
return fmt.Sprintf("%.0fh", delta.Hours())
}
} else if delta.Minutes() > 1 {
return fmt.Sprintf("%.0fm", delta.Minutes())
} else if delta.Seconds() > 1 {
return fmt.Sprintf("%.0fs", delta.Seconds())
}

return "?"
}

func columnHelperStatus(status v1.PodStatus) string {
return fmt.Sprintf("%s", status.Phase)
}

func columnHelperReady(cs []v1.ContainerStatus) string {
cr := 0
for _, c := range cs {
if c.Ready {
cr = cr + 1
}
}
return fmt.Sprintf("%d/%d", cr, len(cs))
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/jroimartin/gocui"
)

var NAMESPACE string = "default"

func main() {
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
Expand Down
38 changes: 32 additions & 6 deletions views.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,51 @@ func viewPods(g *gocui.Gui, lMaxX int, lMaxY int) error {
// Set as current view
g.SetCurrentView(v.Name())

// Content
// Add column
// Content: Add column
//viewPodsAddLine(v, lMaxX, "NAME", "CPU", "MEMORY", "READY", "STATUS", "RESTARTS", "AGE") // TODO CPU + Memory
viewPodsAddLine(v, lMaxX, "NAME", "READY", "STATUS", "RESTARTS", "AGE")
fmt.Fprintln(v, strings.Repeat("─", lMaxX))

// Content: Add lines
// TODO Use goroutine
pods, err := getPods()
if err != nil {
panic(err.Error())
}
if len(pods.Items) > 0 {
debug(g, fmt.Sprintf("There are %d pods in the cluster", len(pods.Items)))
for _, pod := range pods.Items {
n := pod.GetName()
//c := "?" // TODO CPU + Memory
//m := "?" // TODO CPU + Memory
s := columnHelperStatus(pod.Status)
r := columnHelperRestarts(pod.Status.ContainerStatuses)
a := columnHelperAge(pod.CreationTimestamp)
cr := columnHelperReady(pod.Status.ContainerStatuses)
viewPodsAddLine(v, lMaxX, n, cr, s, r, a)
//viewPodsAddLine(v, lMaxX, n, c, m, cr, s, r, a) // TODO CPU + Memory
}
} else {
debug(g, "Pods not found.")
}
}

return nil
}

// Add line to view pods
//func viewPodsAddLine(v *gocui.View, maxX int, name, cpu, memory, ready, status, restarts, age string) { // TODO CPU + Memory
func viewPodsAddLine(v *gocui.View, maxX int, name, ready, status, restarts, age string) {
wN := maxX - 40 - 2
wN := maxX - 34 // 54 // TODO CPU + Memory
if wN < 45 {
wN = 45
}
line := pad.Right(name, wN, " ") +
pad.Right(ready, 12, " ") +
pad.Right(status, 12, " ") +
pad.Right(restarts, 12, " ") +
//pad.Right(cpu, 10, " ") + // TODO CPU + Memory
//pad.Right(memory, 10, " ") + // TODO CPU + Memory
pad.Right(ready, 10, " ") +
pad.Right(status, 10, " ") +
pad.Right(restarts, 10, " ") +
pad.Right(age, 4, " ")
fmt.Fprintln(v, line)
}

0 comments on commit 3431587

Please sign in to comment.