Update the util/kubernetes client to retrieve logs

This commit is contained in:
Jake Sanders
2019-12-17 16:09:51 +00:00
parent 0415ead504
commit 53ca742c66
7 changed files with 85 additions and 9 deletions

View File

@@ -9,7 +9,9 @@ type klog struct{}
func (k *klog) Read(...log.ReadOption) []log.Record { return nil }
func (k *klog) Write(log.Record) {}
func (k *klog) Write(l log.Record) {
write(l)
}
func (k *klog) Stream(stop chan bool) <-chan log.Record {
c := make(chan log.Record)

View File

@@ -2,6 +2,7 @@ package kubernetes
import (
"bytes"
"encoding/json"
"io"
"os"
"testing"
@@ -13,6 +14,7 @@ import (
func TestKubernetes(t *testing.T) {
k := New()
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
@@ -20,17 +22,22 @@ func TestKubernetes(t *testing.T) {
s := os.Stderr
os.Stderr = w
meta := make(map[string]string)
meta["foo"] = "bar"
k.Write(log.Record{
write := log.Record{
Timestamp: time.Unix(0, 0),
Value: "Test log entry",
Metadata: meta,
})
}
meta["foo"] = "bar"
k.Write(write)
b := &bytes.Buffer{}
w.Close()
io.Copy(b, r)
os.Stderr = s
assert.Equal(t, "Test log entry", b.String(), "Write was not equal")
var read log.Record
if err := json.Unmarshal(b.Bytes(), &read); err != nil {
t.Fatalf("json.Unmarshal failed: %s", err.Error())
}
assert.Equal(t, write, read, "Write was not equal")
assert.Nil(t, k.Read(), "Read should be unimplemented")

View File

@@ -0,0 +1,15 @@
package kubernetes
import "github.com/micro/go-micro/debug/log"
import (
"encoding/json"
"fmt"
"os"
)
func write(l log.Record) {
if m, err := json.Marshal(l); err == nil {
fmt.Fprintf(os.Stderr, "%s", m)
}
}