Fixing micro logs being follow by default against k8s (#1866)

This commit is contained in:
Janos Dobronszki 2020-07-23 09:50:38 +02:00 committed by GitHub
parent a3a7434f2c
commit fbdf1f2c1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 17 deletions

View File

@ -102,7 +102,6 @@ func (k *klog) Read(options ...log.ReadOption) ([]log.Record, error) {
for _, o := range options { for _, o := range options {
o(opts) o(opts)
} }
pods, err := k.getMatchingPods() pods, err := k.getMatchingPods()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -350,25 +350,27 @@ func (k *kubernetes) Init(opts ...runtime.Option) error {
func (k *kubernetes) Logs(s *runtime.Service, options ...runtime.LogsOption) (runtime.LogStream, error) { func (k *kubernetes) Logs(s *runtime.Service, options ...runtime.LogsOption) (runtime.LogStream, error) {
klo := newLog(k.client, s.Name, options...) klo := newLog(k.client, s.Name, options...)
stream, err := klo.Stream()
if err != nil { if !klo.options.Stream {
return nil, err
}
// If requested, also read existing records and stream those too
if klo.options.Count > 0 {
go func() {
records, err := klo.Read() records, err := klo.Read()
if err != nil { if err != nil {
log.Errorf("Failed to get logs for service '%v' from k8s: %v", s.Name, err) log.Errorf("Failed to get logs for service '%v' from k8s: %v", s.Name, err)
return return nil, err
} }
// @todo: this might actually not run before podLogStream starts kstream := &kubeStream{
// and might cause out of order log retrieval at the receiving end. stream: make(chan runtime.LogRecord),
// A better approach would probably to suppor this inside the `klog.Stream` method. stop: make(chan bool),
}
go func() {
for _, record := range records { for _, record := range records {
stream.Chan() <- record kstream.Chan() <- record
} }
}() }()
return kstream, nil
}
stream, err := klo.Stream()
if err != nil {
return nil, err
} }
return stream, nil return stream, nil
} }
@ -398,6 +400,7 @@ func (k *kubeStream) Stop() error {
return nil return nil
default: default:
close(k.stop) close(k.stop)
close(k.stream)
} }
return nil return nil
} }