forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
79 lines (69 loc) · 1.72 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package utils
import (
"io"
"sync"
"github.com/rancher/kontainer-engine/logstream"
"github.com/rancher/norman/condition"
"github.com/rancher/types/apis/management.cattle.io/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func NewClusterLoggingLogger(obj *v3.ClusterLogging, cl v3.ClusterLoggingInterface, cond condition.Cond) (logstream.LoggerStream, io.Closer) {
logger := logstream.NewLogStream()
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for event := range logger.Stream() {
if cond.GetMessage(obj) != event.Message {
if event.Error {
cond.False(obj)
}
cond.Message(obj, event.Message)
if newObj, err := cl.Update(obj); err == nil {
obj = newObj
} else {
newObj, err = cl.Get(obj.Name, metav1.GetOptions{})
if err == nil {
obj = newObj
}
}
}
}
}()
return logger, closerFunc(func() error {
logger.Close()
wg.Wait()
return nil
})
}
func NewProjectLoggingLogger(obj *v3.ProjectLogging, pl v3.ProjectLoggingInterface, cond condition.Cond) (logstream.LoggerStream, io.Closer) {
logger := logstream.NewLogStream()
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for event := range logger.Stream() {
if cond.GetMessage(obj) != event.Message {
if event.Error {
cond.False(obj)
}
cond.Message(obj, event.Message)
if newObj, err := pl.Update(obj); err == nil {
obj = newObj
} else {
newObj, err = pl.Controller().Lister().Get(obj.Namespace, obj.Name)
if err == nil {
obj = newObj
}
}
}
}
}()
return logger, closerFunc(func() error {
logger.Close()
wg.Wait()
return nil
})
}
type closerFunc func() error
func (f closerFunc) Close() error { return f() }