-
Notifications
You must be signed in to change notification settings - Fork 596
/
logexporter.go
191 lines (162 loc) · 5.49 KB
/
logexporter.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
189
190
191
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package lib
import (
"bytes"
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
pkgtest "knative.dev/pkg/test"
"knative.dev/pkg/test/helpers"
"knative.dev/pkg/test/logstream/v2"
"knative.dev/pkg/test/prow"
)
func (c *Client) ExportLogs(dir string) error {
return exportLogs(c.Kube, c.Namespace, dir, c.T.Logf)
}
func exportLogs(kubeClient kubernetes.Interface, namespace, dir string, logFunc func(format string, args ...interface{})) error {
// Create a directory for the namespace.
logPath := filepath.Join(dir, namespace)
if err := helpers.CreateDir(logPath); err != nil {
return fmt.Errorf("error creating directory %q: %w", namespace, err)
}
pods, err := kubeClient.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
return fmt.Errorf("error listing pods in namespace %q: %w", namespace, err)
}
var errs []error
for _, pod := range pods.Items {
for _, ct := range pod.Spec.Containers {
fn := filepath.Join(logPath, fmt.Sprintf("%s-%s.log", pod.Name, ct.Name))
logFunc("Exporting logs in pod %q container %q to %q", pod.Name, ct.Name, fn)
f, err := os.Create(fn)
if err != nil {
errs = append(errs, fmt.Errorf("error creating file %q: %w", fn, err))
}
log, err := pkgtest.PodLogs(context.Background(), kubeClient, pod.Name, ct.Name, pod.Namespace)
if err != nil {
errs = append(errs, fmt.Errorf("error getting logs for pod %q container %q: %w", pod.Name, ct.Name, err))
}
_, err = f.Write(log)
if err != nil {
errs = append(errs, fmt.Errorf("error writing logs into file %q: %w", fn, err))
}
f.Close()
}
}
return helpers.CombineErrors(errs)
}
func ExportLogs(systemLogsDir, systemNamespace string) {
// If the test is run by CI, export the pod logs in the namespace to the artifacts directory,
// which will then be uploaded to GCS after the test job finishes.
if prow.IsCI() {
config, err := pkgtest.Flags.GetRESTConfig()
if err != nil {
log.Printf("Failed to create REST config: %v\n", err)
return
}
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
log.Printf("Failed to create kube client: %v\n", err)
return
}
dir := filepath.Join(prow.GetLocalArtifactsDir(), systemLogsDir)
if err := exportLogs(kubeClient, systemNamespace, dir, log.Printf); err != nil {
log.Printf("Error in exporting logs: %v", err)
}
}
}
// ExportLogStreamOnError starts a log stream from the given namespace
// for pods specified via podPrefixes. The log strema is stopped and
// logs exported upon calling the returned Canceler.
func ExportLogStreamOnError(t *testing.T, logDir, namespace string, podPrefixes ...string) logstream.Canceler {
config, err := pkgtest.Flags.GetRESTConfig()
if err != nil {
t.Fatalf("Failed to create REST config: %v\n", err)
}
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
t.Fatalf("Failed to create kube client: %v\n", err)
}
buf := threadSafeBuffer{}
callback := func(s string, params ...interface{}) {
buf.Write([]byte(fmt.Sprintf(s, params...) + "\n"))
}
sysStream := logstream.New(context.Background(), kubeClient,
logstream.WithNamespaces(namespace),
logstream.WithLineFiltering(false),
logstream.WithPodPrefixes(podPrefixes...))
canceler, err := sysStream.StartStream("unfiltered-logs", callback)
if err != nil {
t.Fatal("Unable to stream logs from namespace", err)
}
return func() {
canceler()
if !t.Failed() {
return
}
// Maps pod name prefix to its logs.
logs := make(map[string]string)
if len(podPrefixes) == 0 {
logs["all-in-one"] = buf.String()
} else {
// Divide logs by podPrefix so that logs for each pod are stored in a separate file.
for _, podPrefix := range podPrefixes {
for _, line := range strings.Split(buf.String(), "\n") {
if strings.Contains(line, podPrefix) {
logs[podPrefix] = logs[podPrefix] + line + "\n"
}
}
}
}
dir := filepath.Join(prow.GetLocalArtifactsDir(), logDir)
if err := helpers.CreateDir(dir); err != nil {
t.Errorf("Error creating directory %q: %v", dir, err)
}
var errs []error
for podPrefix, log := range logs {
path := filepath.Join(dir, fmt.Sprintf("%s.log", podPrefix))
f, err := os.Create(path)
if err != nil {
errs = append(errs, fmt.Errorf("error creating file %q: %w", path, err))
continue
}
_, err = f.Write([]byte(log))
if err != nil {
errs = append(errs, fmt.Errorf("error writing logs into file %q: %w", path, err))
}
_ = f.Close()
}
if len(errs) > 0 {
t.Errorf("Failed to write logs: %v", helpers.CombineErrors(errs))
}
}
}
// threadSafeBuffer avoids race conditions on bytes.Buffer.
// See: https://stackoverflow.com/a/36226525/844449
type threadSafeBuffer struct {
bytes.Buffer
sync.Mutex
}
func (b *threadSafeBuffer) Write(p []byte) (n int, err error) {
b.Mutex.Lock()
defer b.Mutex.Unlock()
return b.Buffer.Write(p)
}