mirror of
https://github.com/bcicen/ctop.git
synced 2025-12-06 15:16:41 +08:00
A draft of connector to kubernetse
This commit is contained in:
committed by
Bradley Cicenas
parent
665e8fdd06
commit
187adf0540
125
connector/collector/kubernetes.go
Normal file
125
connector/collector/kubernetes.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"github.com/bcicen/ctop/config"
|
||||
"github.com/bcicen/ctop/models"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
// Kubernetes collector
|
||||
type Kubernetes struct {
|
||||
models.Metrics
|
||||
name string
|
||||
client *kubernetes.Clientset
|
||||
running bool
|
||||
stream chan models.Metrics
|
||||
done chan bool
|
||||
lastCpu float64
|
||||
lastSysCpu float64
|
||||
scaleCpu bool
|
||||
}
|
||||
|
||||
func NewKubernetes(client *kubernetes.Clientset, name string) *Kubernetes {
|
||||
return &Kubernetes{
|
||||
Metrics: models.Metrics{},
|
||||
name: name,
|
||||
client: 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)
|
||||
|
||||
//go func() {
|
||||
// opts := api.StatsOptions{
|
||||
// ID: c.id,
|
||||
// Stats: stats,
|
||||
// Stream: true,
|
||||
// Done: c.done,
|
||||
// }
|
||||
// c.client.Stats(opts)
|
||||
// c.running = false
|
||||
//}()
|
||||
|
||||
//go func() {
|
||||
// defer close(c.stream)
|
||||
// for s := range stats {
|
||||
// c.ReadCPU(s)
|
||||
// c.ReadMem(s)
|
||||
// c.ReadNet(s)
|
||||
// c.ReadIO(s)
|
||||
// c.stream <- c.Metrics
|
||||
// }
|
||||
// log.Infof("collector stopped for container: %s", c.id)
|
||||
//}()
|
||||
|
||||
//c.running = true
|
||||
//log.Infof("collector started for container: %s", c.id)
|
||||
}
|
||||
|
||||
func (c *Kubernetes) Running() bool {
|
||||
return c.running
|
||||
}
|
||||
|
||||
func (c *Kubernetes) Stream() chan models.Metrics {
|
||||
return c.stream
|
||||
}
|
||||
|
||||
func (c *Kubernetes) Logs() LogCollector {
|
||||
return NewKubernetesLogs(c.name, c.client)
|
||||
}
|
||||
|
||||
// Stop collector
|
||||
func (c *Kubernetes) Stop() {
|
||||
c.done <- true
|
||||
}
|
||||
|
||||
//
|
||||
//func (c *Kubernetes) ReadCPU(stats *api.Stats) {
|
||||
// ncpus := float64(len(stats.CPUStats.CPUUsage.PercpuUsage))
|
||||
// total := float64(stats.CPUStats.CPUUsage.TotalUsage)
|
||||
// system := float64(stats.CPUStats.SystemCPUUsage)
|
||||
//
|
||||
// cpudiff := total - c.lastCpu
|
||||
// syscpudiff := system - c.lastSysCpu
|
||||
//
|
||||
// if c.scaleCpu {
|
||||
// c.CPUUtil = round((cpudiff / syscpudiff * 100))
|
||||
// } else {
|
||||
// c.CPUUtil = round((cpudiff / syscpudiff * 100) * ncpus)
|
||||
// }
|
||||
// c.lastCpu = total
|
||||
// c.lastSysCpu = system
|
||||
// c.Pids = int(stats.PidsStats.Current)
|
||||
//}
|
||||
|
||||
//func (c *Kubernetes) ReadMem(stats *api.Stats) {
|
||||
// c.MemUsage = int64(stats.MemoryStats.Usage - stats.MemoryStats.Stats.Cache)
|
||||
// c.MemLimit = int64(stats.MemoryStats.Limit)
|
||||
// c.MemPercent = percent(float64(c.MemUsage), float64(c.MemLimit))
|
||||
//}
|
||||
|
||||
//func (c *Kubernetes) ReadNet(stats *api.Stats) {
|
||||
// var rx, tx int64
|
||||
// for _, network := range stats.Networks {
|
||||
// rx += int64(network.RxBytes)
|
||||
// tx += int64(network.TxBytes)
|
||||
// }
|
||||
// c.NetRx, c.NetTx = rx, tx
|
||||
//}
|
||||
//
|
||||
//func (c *Kubernetes) ReadIO(stats *api.Stats) {
|
||||
// var read, write int64
|
||||
// for _, blk := range stats.BlkioStats.IOServiceBytesRecursive {
|
||||
// if blk.Op == "Read" {
|
||||
// read = int64(blk.Value)
|
||||
// }
|
||||
// if blk.Op == "Write" {
|
||||
// write = int64(blk.Value)
|
||||
// }
|
||||
// }
|
||||
// c.IOBytesRead, c.IOBytesWrite = read, write
|
||||
//}
|
||||
78
connector/collector/kubernetes_logs.go
Normal file
78
connector/collector/kubernetes_logs.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/bcicen/ctop/models"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
type KubernetesLogs struct {
|
||||
id string
|
||||
client *kubernetes.Clientset
|
||||
done chan bool
|
||||
}
|
||||
|
||||
func NewKubernetesLogs(id string, client *kubernetes.Clientset) *KubernetesLogs {
|
||||
return &KubernetesLogs{
|
||||
id: id,
|
||||
client: client,
|
||||
done: make(chan bool),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *KubernetesLogs) Stream() chan models.Log {
|
||||
//r, w := io.Pipe()
|
||||
logCh := make(chan models.Log)
|
||||
//ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
//opts := api.LogsOptions{
|
||||
// Context: ctx,
|
||||
// Container: l.id,
|
||||
// OutputStream: w,
|
||||
// ErrorStream: w,
|
||||
// Stdout: true,
|
||||
// Stderr: true,
|
||||
// Tail: "10",
|
||||
// Follow: true,
|
||||
// Timestamps: true,
|
||||
//}
|
||||
|
||||
//// read io pipe into channel
|
||||
//go func() {
|
||||
// scanner := bufio.NewScanner(r)
|
||||
// for scanner.Scan() {
|
||||
// parts := strings.Split(scanner.Text(), " ")
|
||||
// ts := l.parseTime(parts[0])
|
||||
// logCh <- models.Log{Timestamp: ts, Message: strings.Join(parts[1:], " ")}
|
||||
// }
|
||||
//}()
|
||||
|
||||
//// connect to container log stream
|
||||
//go func() {
|
||||
// err := l.client.Logs(opts)
|
||||
// if err != nil {
|
||||
// log.Errorf("error reading container logs: %s", err)
|
||||
// }
|
||||
// log.Infof("log reader stopped for container: %s", l.id)
|
||||
//}()
|
||||
|
||||
//go func() {
|
||||
// <-l.done
|
||||
// cancel()
|
||||
//}()
|
||||
|
||||
log.Infof("log reader started for container: %s", l.id)
|
||||
return logCh
|
||||
}
|
||||
|
||||
func (l *KubernetesLogs) Stop() { l.done <- true }
|
||||
|
||||
func (l *KubernetesLogs) parseTime(s string) time.Time {
|
||||
ts, err := time.Parse("2006-01-02T15:04:05.000000000Z", s)
|
||||
if err != nil {
|
||||
log.Errorf("failed to parse container log: %s", err)
|
||||
ts = time.Now()
|
||||
}
|
||||
return ts
|
||||
}
|
||||
Reference in New Issue
Block a user