2019-12-17 15:11:26 +03:00
|
|
|
// Package kubernetes is a logger implementing (github.com/micro/go-micro/debug/log).Log
|
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2019-12-21 02:16:05 +03:00
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2019-12-17 20:24:01 +03:00
|
|
|
|
2019-12-17 15:11:26 +03:00
|
|
|
"github.com/micro/go-micro/debug/log"
|
2019-12-21 02:16:05 +03:00
|
|
|
"github.com/micro/go-micro/util/kubernetes/client"
|
2019-12-17 15:11:26 +03:00
|
|
|
)
|
|
|
|
|
2019-12-21 02:16:05 +03:00
|
|
|
type klog struct {
|
|
|
|
client client.Kubernetes
|
2019-12-17 15:11:26 +03:00
|
|
|
|
2019-12-21 02:16:05 +03:00
|
|
|
log.Options
|
|
|
|
}
|
|
|
|
|
2019-12-21 02:34:08 +03:00
|
|
|
func (k *klog) podLogStream(podName string, stream *kubeStream) {
|
|
|
|
p := make(map[string]string)
|
|
|
|
p["follow"] = "true"
|
|
|
|
|
|
|
|
body, err := k.client.Logs(podName, client.AdditionalParams(p))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s := bufio.NewScanner(body)
|
|
|
|
defer body.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stream.stop:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
if s.Scan() {
|
|
|
|
record := k.parse(s.Text())
|
|
|
|
stream.stream <- record
|
|
|
|
} else {
|
|
|
|
// TODO: is there a blocking call
|
|
|
|
// rather than a sleep loop?
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *klog) getMatchingPods() ([]string, error) {
|
|
|
|
r := &client.Resource{
|
|
|
|
Kind: "pod",
|
|
|
|
Value: new(client.PodList),
|
|
|
|
}
|
|
|
|
|
|
|
|
l := make(map[string]string)
|
|
|
|
|
|
|
|
l["name"] = client.Format(k.Options.Name)
|
|
|
|
// TODO: specify micro:service
|
|
|
|
// l["micro"] = "service"
|
|
|
|
|
|
|
|
if err := k.client.Get(r, l); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var matches []string
|
|
|
|
|
|
|
|
for _, p := range r.Value.(*client.PodList).Items {
|
|
|
|
// find labels that match the name
|
|
|
|
if p.Metadata.Labels["name"] == client.Format(k.Options.Name) {
|
|
|
|
matches = append(matches, p.Metadata.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *klog) parse(line string) log.Record {
|
|
|
|
record := log.Record{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(line), &record); err != nil {
|
|
|
|
record.Timestamp = time.Now().UTC()
|
|
|
|
record.Message = line
|
|
|
|
record.Metadata = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
record.Metadata["service"] = k.Options.Name
|
|
|
|
|
|
|
|
return record
|
|
|
|
}
|
|
|
|
|
2019-12-21 02:16:05 +03:00
|
|
|
func (k *klog) Read(options ...log.ReadOption) ([]log.Record, error) {
|
|
|
|
opts := &log.ReadOptions{}
|
|
|
|
for _, o := range options {
|
|
|
|
o(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
logsToGet, err := k.getMatchingPods()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-21 02:34:08 +03:00
|
|
|
|
|
|
|
var records []log.Record
|
2019-12-21 02:16:05 +03:00
|
|
|
|
|
|
|
for _, l := range logsToGet {
|
|
|
|
logParams := make(map[string]string)
|
2019-12-21 02:34:08 +03:00
|
|
|
|
2019-12-21 02:16:05 +03:00
|
|
|
if !opts.Since.Equal(time.Time{}) {
|
|
|
|
logParams["sinceSeconds"] = strconv.Itoa(int(time.Since(opts.Since).Seconds()))
|
|
|
|
}
|
2019-12-21 02:34:08 +03:00
|
|
|
|
2019-12-21 02:16:05 +03:00
|
|
|
if opts.Count != 0 {
|
|
|
|
logParams["tailLines"] = strconv.Itoa(opts.Count)
|
|
|
|
}
|
2019-12-21 02:34:08 +03:00
|
|
|
|
2019-12-21 02:16:05 +03:00
|
|
|
if opts.Stream == true {
|
|
|
|
logParams["follow"] = "true"
|
|
|
|
}
|
2019-12-21 02:34:08 +03:00
|
|
|
|
2019-12-21 02:16:05 +03:00
|
|
|
logs, err := k.client.Logs(l, client.AdditionalParams(logParams))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer logs.Close()
|
2019-12-21 02:34:08 +03:00
|
|
|
|
2019-12-21 02:16:05 +03:00
|
|
|
s := bufio.NewScanner(logs)
|
2019-12-21 02:34:08 +03:00
|
|
|
|
2019-12-21 02:16:05 +03:00
|
|
|
for s.Scan() {
|
|
|
|
record := k.parse(s.Text())
|
|
|
|
record.Metadata["pod"] = l
|
|
|
|
records = append(records, record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-21 02:34:08 +03:00
|
|
|
// sort the records
|
|
|
|
sort.Slice(records, func(i, j int) bool { return records[i].Timestamp.Before(records[j].Timestamp) })
|
|
|
|
|
2019-12-21 02:16:05 +03:00
|
|
|
return records, nil
|
2019-12-17 20:24:01 +03:00
|
|
|
}
|
2019-12-17 15:11:26 +03:00
|
|
|
|
2019-12-17 20:24:01 +03:00
|
|
|
func (k *klog) Write(l log.Record) error {
|
|
|
|
return write(l)
|
2019-12-17 19:09:51 +03:00
|
|
|
}
|
2019-12-17 15:11:26 +03:00
|
|
|
|
2019-12-17 20:24:01 +03:00
|
|
|
func (k *klog) Stream() (log.Stream, error) {
|
2019-12-21 02:34:08 +03:00
|
|
|
// find the matching pods
|
2019-12-21 02:16:05 +03:00
|
|
|
pods, err := k.getMatchingPods()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-21 02:34:08 +03:00
|
|
|
|
|
|
|
stream := &kubeStream{
|
|
|
|
stream: make(chan log.Record),
|
|
|
|
stop: make(chan bool),
|
2019-12-21 02:16:05 +03:00
|
|
|
}
|
2019-12-21 02:34:08 +03:00
|
|
|
|
|
|
|
// stream from the individual pods
|
2019-12-21 02:16:05 +03:00
|
|
|
for _, pod := range pods {
|
2019-12-21 02:34:08 +03:00
|
|
|
go k.podLogStream(pod, stream)
|
2019-12-21 02:16:05 +03:00
|
|
|
}
|
|
|
|
|
2019-12-21 02:34:08 +03:00
|
|
|
return stream, nil
|
2019-12-17 15:11:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a configured Kubernetes logger
|
2019-12-21 02:16:05 +03:00
|
|
|
func New(opts ...log.Option) log.Log {
|
|
|
|
klog := &klog{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&klog.Options)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(os.Getenv("KUBERNETES_SERVICE_HOST")) > 0 {
|
|
|
|
klog.client = client.NewClientInCluster()
|
|
|
|
} else {
|
|
|
|
klog.client = client.NewLocalDevClient()
|
|
|
|
}
|
|
|
|
return klog
|
|
|
|
}
|