Skip to content

Added supprot for network rx/tx #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: k8s
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 58 additions & 22 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@
[[constraint]]
name = "github.com/kubernetes/client-go"
version = "9.0.0"

[[constraint]]
name = "github.com/kubernetes/metrics"

[[constraint]]
name = "github.com/kubernetes-incubator/metrics-server"
54 changes: 32 additions & 22 deletions connector/collector/kubernetes.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package collector

import (
"k8s.io/metrics/pkg/client/clientset_generated/clientset"

"github.com/bcicen/ctop/config"
"github.com/bcicen/ctop/models"
"k8s.io/client-go/kubernetes"

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

// Kubernetes collector
type Kubernetes struct {
models.Metrics
name string
client *kubernetes.Clientset
client clientset.Interface
clientset *kubernetes.Clientset
running bool
stream chan models.Metrics
done chan bool
Expand All @@ -21,28 +26,33 @@ type Kubernetes struct {

func NewKubernetes(client *kubernetes.Clientset, name string) *Kubernetes {
return &Kubernetes{
Metrics: models.Metrics{},
name: name,
client: client,
scaleCpu: config.GetSwitchVal("scaleCpu"),
Metrics: models.Metrics{},
name: name,
client: clientset.New(client.RESTClient()),
clientset: client,
scaleCpu: config.GetSwitchVal("scaleCpu"),
}
}

func (c *Kubernetes) Start() {
//c.done = make(chan bool)
//c.stream = make(chan models.Metrics)
//stats := make(chan *api.Stats)
func (k *Kubernetes) Start() {
k.done = make(chan bool)
k.stream = make(chan models.Metrics)

//go func() {
// opts := api.StatsOptions{
// ID: c.id,
// Stats: stats,
// Stream: true,
// Done: c.done,
// }
// c.client.Stats(opts)
// c.running = false
//}()
go func() {
k.running = false
for {

cm, err := k.client.Metrics().PodMetricses("akozlenkov").List(metav1.ListOptions{})
if err != nil {
log.Errorf(">>>>>> %s here %s", k.name, err.Error())
continue
}
log.Debugf(">>>> %+v", cm)
//for _, m := range cm.Containers {
// log.Debugf(">>>> %+v", m)
//}
}
}()

//go func() {
// defer close(c.stream)
Expand All @@ -56,8 +66,8 @@ func (c *Kubernetes) Start() {
// log.Infof("collector stopped for container: %s", c.id)
//}()

//c.running = true
//log.Infof("collector started for container: %s", c.id)
k.running = true
log.Infof("collector started for container: %s", k.name)
}

func (c *Kubernetes) Running() bool {
Expand All @@ -69,7 +79,7 @@ func (c *Kubernetes) Stream() chan models.Metrics {
}

func (c *Kubernetes) Logs() LogCollector {
return NewKubernetesLogs(c.name, c.client)
return NewKubernetesLogs(c.name, c.clientset)
}

// Stop collector
Expand Down
92 changes: 57 additions & 35 deletions connector/kubernetes.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package connector

import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/bcicen/ctop/connector/collector"
"github.com/bcicen/ctop/connector/manager"
Expand Down Expand Up @@ -59,42 +62,46 @@ func NewKubernetes() Connector {
}
go k.Loop()
k.refreshAll()
go k.watchEvents()
return k
}

func (k *Kubernetes) watchEvents() {
for {
log.Info("kubernetes event listener starting")
allEvents, err := k.clientset.CoreV1().Events(namespace).List(metav1.ListOptions{})
if err != nil {
log.Error(err.Error())
return
}

for _, e := range allEvents.Items {
if e.Kind != "pod" {
continue
}

actionName := strings.Split(e.Action, ":")[0]

switch actionName {
case "start", "die", "pause", "unpause", "health_status":
log.Debugf("handling docker event: action=%s id=%s", e.Action, e.UID)
k.needsRefresh <- e.Name
case "destroy":
log.Debugf("handling docker event: action=%s id=%s", e.Action, e.UID)
k.delByID(e.Name)
default:
log.Debugf("handling docker event: %v", e)
k.needsRefresh <- e.Name
}
}
time.Sleep(1 * time.Second)
}
}
func (k *Kubernetes) Loop() {
for id := range k.needsRefresh {
c := k.MustGet(id)
k.refresh(c)
}
//log.Debug(">>>>>>1")
//for {
// log.Debug(">>>>>>2")
// pods, err := k.clientset.CoreV1().Pods("").List(metav1.ListOptions{})
// if err != nil {
// panic(err.Error())
// }
// log.Debugf("There are %d pods in the cluster\n", len(pods.Items))

// // Examples for error handling:
// // - Use helper functions like e.g. errors.IsNotFound()
// // - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message
// namespace := "akozlenkov"
// pod := "example-xxxxx"
// _, err = k.clientset.CoreV1().Pods(namespace).Get(pod, metav1.GetOptions{})
// if errors.IsNotFound(err) {
// log.Debugf("Pod %s in namespace %s not found\n", pod, namespace)
// } else if statusError, isStatus := err.(*errors.StatusError); isStatus {
// log.Debugf("Error getting pod %s in namespace %s: %v\n",
// pod, namespace, statusError.ErrStatus.Message)
// } else if err != nil {
// panic(err.Error())
// } else {
// log.Debugf("Found pod %s in namespace %s\n", pod, namespace)
// }

// time.Sleep(10 * time.Second)
//}
}

// Get a single container, creating one anew if not existing
Expand Down Expand Up @@ -123,13 +130,25 @@ func (k *Kubernetes) refresh(c *container.Container) {
return
}
c.SetMeta("name", insp.Name)
c.SetMeta("image", "stub")
c.SetMeta("IPs", "stub")
c.SetMeta("ports", "stub")
c.SetMeta("created", "stub")
c.SetMeta("health", "stub")
c.SetMeta("[ENV-VAR]", "stub")
c.SetState("stub")
if len(insp.Spec.Containers) >= 1 {
c.SetMeta("image", insp.Spec.Containers[0].Image)
c.SetMeta("ports", k8sPort(insp.Spec.Containers[0].Ports))
for _, env := range insp.Spec.Containers[0].Env {
c.SetMeta("[ENV-VAR]", env.Name+"="+env.Value)
}
}
c.SetMeta("IPs", insp.Status.PodIP)
c.SetMeta("created", insp.CreationTimestamp.Format("Mon Jan 2 15:04:05 2006"))
c.SetMeta("health", string(insp.Status.Phase))
c.SetState("running")
}

func k8sPort(ports []v1.ContainerPort) string {
str := []string{}
for _, p := range ports {
str = append(str, fmt.Sprintf("%s:%d -> %d", p.HostIP, p.HostPort, p.ContainerPort))
}
return strings.Join(str, "\n")
}

func (k *Kubernetes) inspect(id string) *v1.Pod {
Expand Down Expand Up @@ -167,9 +186,12 @@ func (k *Kubernetes) refreshAll() {

for _, pod := range allPods.Items {
c := k.MustGet(pod.Name)
c.SetMeta("uid", string(pod.UID))
c.SetMeta("name", pod.Name)
if pod.Initializers != nil && pod.Initializers.Result != nil {
c.SetState(pod.Initializers.Result.Status)
} else {
c.SetState(string(pod.Status.Phase))
}
k.needsRefresh <- c.Id
}
Expand Down
6 changes: 3 additions & 3 deletions cwidgets/compact/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ func (s *Status) SetHealth(val string) {
color := ui.ColorDefault

switch val {
case "healthy":
case "healthy", "Succeeded":
color = ui.ThemeAttr("status.ok")
case "unhealthy":
case "unhealthy", "Failed", "Unknown":
color = ui.ThemeAttr("status.danger")
case "starting":
case "starting", "Pending", "Running":
color = ui.ThemeAttr("status.warn")
}

Expand Down
Loading