forked from virtual-kubelet/systemk
/
log.go
188 lines (166 loc) · 4.81 KB
/
log.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package provider
import (
"io"
"net/http"
"net/url"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
nodeapi "github.com/virtual-kubelet/virtual-kubelet/node/api"
)
func (p *p) GetContainerLogsHandler(w http.ResponseWriter, r *http.Request) {
handleError(func(w http.ResponseWriter, req *http.Request) error {
vars := mux.Vars(r)
if len(vars) != 3 {
return errdefs.NotFound("not found")
}
namespace := vars["namespace"]
pod := vars["pod"]
container := vars["container"]
query := r.URL.Query()
opts, err := parseLogOptions(query)
if err != nil {
return err
}
r.Header.Set("Transfer-Encoding", "chunked")
logsReader, cancel, err := journalReader(namespace, pod, container, opts)
if err != nil {
return errors.Wrap(err, "failed to get systemd journal logs reader")
}
defer logsReader.Close()
defer cancel()
// ResponseWriter must be flushed after each write.
if _, ok := w.(writeFlusher); !ok {
log.Warn("HTTP response writer does not support flushes")
}
fw := flushOnWrite(w)
if !opts.Follow {
io.Copy(fw, logsReader)
return nil
}
// If in follow mode, follow until interrupted.
untilTime := make(chan time.Time, 1)
errChan := make(chan error, 1)
go func(w io.Writer, errChan chan error) {
err := journalFollow(untilTime, logsReader, w)
if err != nil && err != ErrExpired {
err = errors.Wrap(err, "failed to follow systemd journal logs")
}
errChan <- err
}(fw, errChan)
// Stop following logs if request context is completed.
select {
case err := <-errChan:
return err
case <-r.Context().Done():
close(untilTime)
}
return nil
})(w, r)
}
func (p *p) notFound(w http.ResponseWriter, _ *http.Request) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}
func parseLogOptions(q url.Values) (opts nodeapi.ContainerLogOpts, err error) {
if tailLines := q.Get("tailLines"); tailLines != "" {
opts.Tail, err = strconv.Atoi(tailLines)
if err != nil {
return opts, errdefs.AsInvalidInput(errors.Wrap(err, "could not parse \"tailLines\""))
}
if opts.Tail < 0 {
return opts, errdefs.InvalidInputf("\"tailLines\" is %d", opts.Tail)
}
}
if follow := q.Get("follow"); follow != "" {
opts.Follow, err = strconv.ParseBool(follow)
if err != nil {
return opts, errdefs.AsInvalidInput(errors.Wrap(err, "could not parse \"follow\""))
}
}
if limitBytes := q.Get("limitBytes"); limitBytes != "" {
opts.LimitBytes, err = strconv.Atoi(limitBytes)
if err != nil {
return opts, errdefs.AsInvalidInput(errors.Wrap(err, "could not parse \"limitBytes\""))
}
if opts.LimitBytes < 1 {
return opts, errdefs.InvalidInputf("\"limitBytes\" is %d", opts.LimitBytes)
}
}
if previous := q.Get("previous"); previous != "" {
opts.Previous, err = strconv.ParseBool(previous)
if err != nil {
return opts, errdefs.AsInvalidInput(errors.Wrap(err, "could not parse \"previous\""))
}
}
if sinceSeconds := q.Get("sinceSeconds"); sinceSeconds != "" {
opts.SinceSeconds, err = strconv.Atoi(sinceSeconds)
if err != nil {
return opts, errdefs.AsInvalidInput(errors.Wrap(err, "could not parse \"sinceSeconds\""))
}
if opts.SinceSeconds < 1 {
return opts, errdefs.InvalidInputf("\"sinceSeconds\" is %d", opts.SinceSeconds)
}
}
if sinceTime := q.Get("sinceTime"); sinceTime != "" {
opts.SinceTime, err = time.Parse(time.RFC3339, sinceTime)
if err != nil {
return opts, errdefs.AsInvalidInput(errors.Wrap(err, "could not parse \"sinceTime\""))
}
if opts.SinceSeconds > 0 {
return opts, errdefs.InvalidInput("both \"sinceSeconds\" and \"sinceTime\" are set")
}
}
if timestamps := q.Get("timestamps"); timestamps != "" {
opts.Timestamps, err = strconv.ParseBool(timestamps)
if err != nil {
return opts, errdefs.AsInvalidInput(errors.Wrap(err, "could not parse \"timestamps\""))
}
}
return opts, nil
}
type handlerFunc func(http.ResponseWriter, *http.Request) error
func handleError(f handlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
err := f(w, req)
if err == nil {
return
}
code := httpStatusCode(err)
w.WriteHeader(code)
io.WriteString(w, err.Error())
}
}
func flushOnWrite(w io.Writer) io.Writer {
if fw, ok := w.(writeFlusher); ok {
return &flushWriter{fw}
}
return w
}
type flushWriter struct {
w writeFlusher
}
type writeFlusher interface {
Flush()
Write([]byte) (int, error)
}
func (fw *flushWriter) Write(p []byte) (int, error) {
n, err := fw.w.Write(p)
if n > 0 {
fw.w.Flush()
}
return n, err
}
func httpStatusCode(err error) int {
switch {
case err == nil:
return http.StatusOK
case errdefs.IsNotFound(err):
return http.StatusNotFound
case errdefs.IsInvalidInput(err):
return http.StatusBadRequest
default:
return http.StatusInternalServerError
}
}