2019-12-17 16:09:51 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-04-02 13:16:35 +02:00
|
|
|
"sync"
|
2019-12-20 23:34:08 +00:00
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/debug/log"
|
2019-12-17 16:09:51 +00:00
|
|
|
)
|
|
|
|
|
2019-12-17 17:24:01 +00: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 16:09:51 +00:00
|
|
|
}
|
2019-12-17 17:24:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-20 23:34:08 +00:00
|
|
|
type kubeStream struct {
|
2019-12-17 18:34:21 +00:00
|
|
|
// the k8s log stream
|
2019-12-20 23:34:08 +00:00
|
|
|
stream chan log.Record
|
2020-04-02 13:16:35 +02:00
|
|
|
sync.Mutex
|
2019-12-17 18:34:21 +00:00
|
|
|
// the stop chan
|
|
|
|
stop chan bool
|
2019-12-17 17:24:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 23:34:08 +00:00
|
|
|
func (k *kubeStream) Chan() <-chan log.Record {
|
|
|
|
return k.stream
|
2019-12-17 17:24:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 23:34:08 +00:00
|
|
|
func (k *kubeStream) Stop() error {
|
2020-04-02 13:16:35 +02:00
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
2019-12-17 18:34:21 +00:00
|
|
|
select {
|
|
|
|
case <-k.stop:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
close(k.stop)
|
2019-12-20 23:34:08 +00:00
|
|
|
close(k.stream)
|
2019-12-17 18:34:21 +00:00
|
|
|
}
|
2019-12-17 17:24:01 +00:00
|
|
|
return nil
|
2019-12-17 16:09:51 +00:00
|
|
|
}
|