Adhere to new interfaces

This commit is contained in:
Jake Sanders 2019-12-17 17:24:01 +00:00
parent c2b307e5bb
commit 81e7edd666
3 changed files with 41 additions and 16 deletions

View File

@ -2,21 +2,23 @@
package kubernetes package kubernetes
import ( import (
"errors"
"github.com/micro/go-micro/debug/log" "github.com/micro/go-micro/debug/log"
) )
type klog struct{} type klog struct{}
func (k *klog) Read(...log.ReadOption) []log.Record { return nil } func (k *klog) Read(...log.ReadOption) ([]log.Record, error) {
return nil, errors.New("not implemented")
func (k *klog) Write(l log.Record) {
write(l)
} }
func (k *klog) Stream() (<-chan log.Record, chan bool) { func (k *klog) Write(l log.Record) error {
c, s := make(chan log.Record), make(chan bool) return write(l)
go close(c) }
return c, s
func (k *klog) Stream() (log.Stream, error) {
return &klogStreamer{}, nil
} }
// New returns a configured Kubernetes logger // New returns a configured Kubernetes logger

View File

@ -39,14 +39,18 @@ func TestKubernetes(t *testing.T) {
} }
assert.Equal(t, write, read, "Write was not equal") assert.Equal(t, write, read, "Write was not equal")
assert.Nil(t, k.Read(), "Read should be unimplemented") _, err = k.Read()
assert.Error(t, err, "Read should be unimplemented")
stream, stop := k.Stream() stream, err := k.Stream()
if err != nil {
t.Error(err)
}
records := []log.Record{} records := []log.Record{}
for s := range stream { go stream.Stop()
for s := range stream.Chan() {
records = append(records, s) records = append(records, s)
} }
close(stop) assert.Equal(t, 0, len(records), "Stream should return nothing")
assert.Equal(t, 0, len(records), "Stream should be unimplemented")
} }

View File

@ -8,8 +8,27 @@ import (
"os" "os"
) )
func write(l log.Record) { func write(l log.Record) error {
if m, err := json.Marshal(l); err == nil { m, err := json.Marshal(l)
fmt.Fprintf(os.Stderr, "%s", m) if err == nil {
_, err := fmt.Fprintf(os.Stderr, "%s", m)
return err
} }
return err
}
type klogStreamer struct {
streamChan chan log.Record
}
func (k *klogStreamer) Chan() <-chan log.Record {
if k.streamChan == nil {
k.streamChan = make(chan log.Record)
}
return k.streamChan
}
func (k *klogStreamer) Stop() error {
close(k.streamChan)
return nil
} }