This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
/
loggregator.go
135 lines (114 loc) · 3.3 KB
/
loggregator.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package podwatcher
import (
"bufio"
"context"
"io"
"strconv"
"strings"
"time"
"code.cloudfoundry.org/eirini-loggregator-bridge/config"
. "code.cloudfoundry.org/eirini-loggregator-bridge/logger"
"code.cloudfoundry.org/go-loggregator/v8"
"code.cloudfoundry.org/go-loggregator/v8/rpc/loggregator_v2"
"k8s.io/client-go/kubernetes"
)
type LoggregatorAppMeta struct {
SourceID, InstanceID string
SourceType, PodName, Namespace, Container, Cluster string // Custom tags
}
type Loggregator struct {
Meta *LoggregatorAppMeta
ConnectionOptions config.LoggregatorOptions
KubeClient *kubernetes.Clientset
LoggregatorClient *loggregator.IngressClient
Context context.Context
}
type LoggregatorLogger struct{}
func (LoggregatorLogger) Printf(message string, args ...interface{}) {
LogDebug(append([]interface{}{message}, args...))
}
func (LoggregatorLogger) Panicf(message string, args ...interface{}) {
panic(message)
}
func NewLoggregator(ctx context.Context, m *LoggregatorAppMeta, kubeClient *kubernetes.Clientset, connectionOptions config.LoggregatorOptions) *Loggregator {
return &Loggregator{Meta: m, KubeClient: kubeClient, ConnectionOptions: connectionOptions, Context: ctx}
}
func (l *Loggregator) Envelope(message []byte) *loggregator_v2.Envelope {
LogDebug("Creating envelope for string: ", string(message))
return &loggregator_v2.Envelope{
Message: &loggregator_v2.Envelope_Log{
Log: &loggregator_v2.Log{
Payload: message,
Type: loggregator_v2.Log_OUT,
},
},
SourceId: l.Meta.SourceID,
InstanceId: l.Meta.InstanceID,
Tags: map[string]string{
"source_type": l.Meta.SourceType,
"pod_name": l.Meta.PodName,
"namespace": l.Meta.Namespace,
"container": l.Meta.Container,
"cluster": l.Meta.Cluster, // ??
},
Timestamp: time.Now().Unix() * 1000000000,
}
}
func (l *Loggregator) SetupLoggregatorClient() error {
tlsConfig, err := loggregator.NewIngressTLSConfig(
l.ConnectionOptions.CAPath,
l.ConnectionOptions.CertPath,
l.ConnectionOptions.KeyPath,
)
if err != nil {
return err
}
logger := LoggregatorLogger{}
loggregatorClient, err := loggregator.NewIngressClient(
tlsConfig,
// Temporary make flushing more frequent to be able to debug
loggregator.WithBatchMaxSize(uint(100)),
loggregator.WithLogger(logger),
loggregator.WithAddr(l.ConnectionOptions.Endpoint),
)
if err != nil {
return err
}
l.LoggregatorClient = loggregatorClient
return nil
}
func (l *Loggregator) Write(b []byte) (int, error) {
l.LoggregatorClient.Emit(l.Envelope(b))
return len(b), nil
}
func (l *Loggregator) Tail(namespace, pod, container string) error {
req := l.KubeClient.CoreV1().RESTClient().Get().
Namespace(namespace).
Name(pod).
Resource("pods").
SubResource("log").
Param("follow", strconv.FormatBool(true)).
Param("container", container).
Param("previous", strconv.FormatBool(false)).
Param("timestamps", strconv.FormatBool(false))
stream, err := req.Stream(l.Context)
if err != nil {
return err
}
defer stream.Close()
reader := bufio.NewReader(stream)
for {
line, err := reader.ReadBytes('\n')
if err == io.EOF {
break
}
if err != nil {
return err
}
_, err = l.Write([]byte(strings.TrimSpace(string(line))))
if err != nil {
return err
}
}
return nil
}