micro/debug/log/kubernetes/stream.go

45 lines
618 B
Go
Raw Normal View History

package kubernetes
import (
"encoding/json"
"fmt"
"os"
"sync"
2019-12-21 02:34:08 +03:00
"github.com/micro/go-micro/v3/debug/log"
)
2019-12-17 20:24:01 +03:00
func write(l log.Record) error {
m, err := json.Marshal(l)
if err == nil {
_, err := fmt.Fprintf(os.Stderr, "%s", m)
return err
}
2019-12-17 20:24:01 +03:00
return err
}
2019-12-21 02:34:08 +03:00
type kubeStream struct {
2019-12-17 21:34:21 +03:00
// the k8s log stream
2019-12-21 02:34:08 +03:00
stream chan log.Record
sync.Mutex
2019-12-17 21:34:21 +03:00
// the stop chan
stop chan bool
2019-12-17 20:24:01 +03:00
}
2019-12-21 02:34:08 +03:00
func (k *kubeStream) Chan() <-chan log.Record {
return k.stream
2019-12-17 20:24:01 +03:00
}
2019-12-21 02:34:08 +03:00
func (k *kubeStream) Stop() error {
k.Lock()
defer k.Unlock()
2019-12-17 21:34:21 +03:00
select {
case <-k.stop:
return nil
default:
close(k.stop)
2019-12-21 02:34:08 +03:00
close(k.stream)
2019-12-17 21:34:21 +03:00
}
2019-12-17 20:24:01 +03:00
return nil
}