-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathinfo.go
More file actions
108 lines (92 loc) · 2.86 KB
/
info.go
File metadata and controls
108 lines (92 loc) · 2.86 KB
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
package framework
import (
"fmt"
"runtime/debug"
. "github.com/onsi/ginkgo/v2"
core "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// GetLogs returns the logs for all containers in all pods for a release.
func GetLogs(rm ResourceManager, namespace string, releaseName string) string {
var returnLogs string
pods, err := rm.GetPods(namespace, client.MatchingLabels{
"app.kubernetes.io/instance": releaseName,
})
if err != nil {
return fmt.Sprintf("failed to get pods: %v", err)
}
for _, pod := range pods {
for _, container := range pod.Spec.Containers {
returnLogs += fmt.Sprintf("Logs for container %s:\n", container.Name)
logs, err := rm.GetPodLogs(pod.Namespace, pod.Name, &core.PodLogOptions{
Container: container.Name,
})
if err != nil {
returnLogs += fmt.Sprintf(" failed to get logs: %v\n", err)
continue
}
returnLogs += fmt.Sprintf(" %s\n", logs)
}
}
return returnLogs
}
// GetEvents returns the events for a namespace.
func GetEvents(rm ResourceManager, namespace string) string {
var returnEvents string
events, err := rm.GetEvents(namespace)
if err != nil {
return fmt.Sprintf("failed to get events: %v", err)
}
eventGroups := make(map[string][]core.Event)
for _, event := range events.Items {
eventGroups[event.InvolvedObject.Name] = append(eventGroups[event.InvolvedObject.Name], event)
}
for name, events := range eventGroups {
returnEvents += fmt.Sprintf("Events for %s:\n", name)
for _, event := range events {
returnEvents += fmt.Sprintf(" %s\n", event.Message)
}
returnEvents += "\n"
}
return returnEvents
}
// GetBuildInfo returns the build information.
func GetBuildInfo() (commitHash string, commitTime string, dirtyBuild string) {
commitHash = "unknown"
commitTime = "unknown"
dirtyBuild = "unknown"
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
for _, kv := range info.Settings {
switch kv.Key {
case "vcs.revision":
commitHash = kv.Value
case "vcs.time":
commitTime = kv.Value
case "vcs.modified":
dirtyBuild = kv.Value
}
}
return
}
// AddNginxLogsAndEventsToReport adds nginx logs and events from the namespace to the report if the spec failed.
func AddNginxLogsAndEventsToReport(rm ResourceManager, namespace string) {
if CurrentSpecReport().Failed() {
var returnLogs string
nginxPodNames, _ := GetReadyNginxPodNames(rm.K8sClient, namespace, rm.TimeoutConfig.GetStatusTimeout)
for _, nginxPodName := range nginxPodNames {
returnLogs += fmt.Sprintf("Logs for Nginx Pod %s:\n", nginxPodName)
nginxLogs, _ := rm.GetPodLogs(
namespace,
nginxPodName,
&core.PodLogOptions{Container: "nginx"},
)
returnLogs += fmt.Sprintf(" %s\n", nginxLogs)
}
AddReportEntry("Nginx Logs", returnLogs, ReportEntryVisibilityNever)
events := GetEvents(rm, namespace)
AddReportEntry("Test Events", events, ReportEntryVisibilityNever)
}
}