/
reporter.go
151 lines (121 loc) · 4.12 KB
/
reporter.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
/*
Copyright 2015 The Kubernetes 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 e2e
import (
"context"
"fmt"
"io"
"os"
"strconv"
"time"
"github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/config"
"github.com/onsi/ginkgo/reporters"
"github.com/onsi/ginkgo/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
clientset "k8s.io/client-go/kubernetes"
"github.com/bitpoke/mysql-operator/test/e2e/framework"
)
type podLogReporter struct {
namespace string
logPath string
logFile *os.File
out io.Writer
}
// NewLogsPodReporter writes the logs for all pods in the specified namespace.
// if path is specified then the logs are written to that path, else logs are
// written to GinkgoWriter
func NewLogsPodReporter(ns, path string) reporters.Reporter {
return &podLogReporter{
namespace: ns,
logPath: path,
out: ginkgo.GinkgoWriter,
}
}
// called when suite starts
func (r *podLogReporter) SpecSuiteWillBegin(config config.GinkgoConfigType, s *types.SuiteSummary) {
if r.logPath != "" {
var err error
r.logFile, err = os.OpenFile(r.logPath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
fmt.Printf("Failed to open file: %s with error: %s\n", r.logPath, err)
return
}
r.out = r.logFile
}
}
// called before BeforeSuite before starting tests
func (r *podLogReporter) BeforeSuiteDidRun(setupSummary *types.SetupSummary) {}
// called before every test
func (r *podLogReporter) SpecWillRun(specSummary *types.SpecSummary) {}
// called after every test
func (r *podLogReporter) SpecDidComplete(specSummary *types.SpecSummary) {
// don't output logs if test didn't failed
if specSummary.State <= types.SpecStatePassed {
return
}
// get the kubernetes client
kubeCfg, err := framework.LoadConfig()
if err != nil {
fmt.Println("Failed to get kubeconfig!")
return
}
client, err := clientset.NewForConfig(kubeCfg)
if err != nil {
fmt.Println("Failed to create k8s client!")
return
}
fmt.Fprintf(r.out, "## Start test: %v\n", specSummary.ComponentTexts)
LogPodsWithLabels(client, r.namespace, map[string]string{}, specSummary.RunTime, r.out)
fmt.Fprintf(r.out, "## END test\n")
}
// called before AfterSuite runs
func (r *podLogReporter) AfterSuiteDidRun(setupSummary *types.SetupSummary) {}
// caleed at the end
func (r *podLogReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) {
if r.logFile != nil {
r.logFile.Close()
}
}
func LogPodsWithLabels(c clientset.Interface, ns string, match map[string]string, since time.Duration, out io.Writer) {
podList, err := c.CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: labels.SelectorFromSet(match).String()})
if err != nil {
fmt.Fprintf(out, "error listing pods: %s", err)
return
}
for _, pod := range podList.Items {
for _, container := range pod.Spec.Containers {
fmt.Fprintf(out, "\n\n===============\nSTART LOGS for %s (%s):\n", pod.Name, container.Name)
runLogs(c, ns, pod.Name, container.Name, false, since, out)
fmt.Fprintf(out, "\n\n===============\nSTOP LOGS for %s (%s):\n", pod.Name, container.Name)
}
}
}
func runLogs(client clientset.Interface, namespace, name, container string, previous bool, sinceStart time.Duration, out io.Writer) error {
req := client.CoreV1().RESTClient().Get().
Namespace(namespace).
Name(name).
Resource("pods").
SubResource("log").
Param("container", container).
Param("previous", strconv.FormatBool(previous)).
Param("since", strconv.FormatInt(int64(sinceStart.Round(time.Second).Seconds()), 10))
readCloser, err := req.Stream(context.TODO())
if err != nil {
return err
}
defer readCloser.Close()
_, err = io.Copy(out, readCloser)
return err
}