/
gather_containers_logs.go
144 lines (130 loc) · 3.99 KB
/
gather_containers_logs.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
package conditional
import (
"context"
"fmt"
"k8s.io/client-go/kubernetes"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/klog/v2"
"github.com/openshift/insights-operator/pkg/gatherers"
"github.com/openshift/insights-operator/pkg/gatherers/common"
"github.com/openshift/insights-operator/pkg/record"
)
// BuildGatherContainersLogs Collects either current or previous containers logs for pods firing one of the
// alerts from the conditions fetched from insights conditions service.
//
// ### API Reference
// None
//
// ### Sample data
// - docs/insights-archive-sample/conditional/namespaces/openshift-cluster-samples-operator/pods/cluster-samples-operator-8ffb9b45f-49mjr/containers/cluster-samples-operator-watch/logs/last-100-lines.log
//
// ### Location in archive
// - `conditional/namespaces/{namespace}/pods/{pod}/containers/{container}/{logs|logs-previous}/last-{tail-length}-lines.log`
//
// ### Config ID
// `conditional/containers_logs`
//
// ### Released version
// - 4.10.0
//
// ### Backported versions
// None
//
// ### Changes
// None
func (g *Gatherer) BuildGatherContainersLogs(paramsInterface interface{}) (gatherers.GatheringClosure, error) { // nolint: dupl
params, ok := paramsInterface.(GatherContainersLogsParams)
if !ok {
return gatherers.GatheringClosure{}, fmt.Errorf(
"unexpected type in paramsInterface, expected %T, got %T",
GatherContainersLogsParams{},
paramsInterface)
}
return gatherers.GatheringClosure{
Run: func(ctx context.Context) ([]record.Record, []error) {
kubeClient, err := kubernetes.NewForConfig(g.gatherProtoKubeConfig)
if err != nil {
return nil, []error{err}
}
coreClient := kubeClient.CoreV1()
return g.gatherContainersLogs(ctx, params, coreClient)
},
}, nil
}
func (g *Gatherer) gatherContainersLogs(
ctx context.Context,
params GatherContainersLogsParams,
coreClient corev1client.CoreV1Interface,
) ([]record.Record, []error) {
alertInstances, ok := g.firingAlerts[params.AlertName]
if !ok {
err := fmt.Errorf("conditional gather triggered, but specified alert %q is not firing", params.AlertName)
return nil, []error{err}
}
const logMissingAlert = "%s at alertName: %s"
var errs []error
var records []record.Record
for _, alertLabels := range alertInstances {
podNamespace, err := getAlertPodNamespace(alertLabels)
if err != nil {
klog.Warningf(logMissingAlert, err.Error(), params.AlertName)
errs = append(errs, err)
continue
}
podName, err := getAlertPodName(alertLabels)
if err != nil {
klog.Warningf(logMissingAlert, err.Error(), params.AlertName)
errs = append(errs, err)
continue
}
var podContainer string
if len(params.Container) > 0 {
podContainer = params.Container
} else {
podContainer, err = getAlertPodContainer(alertLabels)
if err != nil {
klog.Warningf(logMissingAlert, err.Error(), params.AlertName)
errs = append(errs, err)
}
}
logContainersFilter := common.LogContainersFilter{
Namespace: podNamespace,
FieldSelector: fmt.Sprintf("metadata.name=%s", podName),
}
// The container label may not be present for all alerts (e.g., KubePodNotReady).
if len(podContainer) > 0 {
logContainersFilter.ContainerNameRegexFilter = fmt.Sprintf("^%s$", podContainer)
}
logRecords, err := common.CollectLogsFromContainers(
ctx,
coreClient,
logContainersFilter,
common.LogMessagesFilter{
TailLines: params.TailLines,
Previous: params.Previous,
},
func(namespace, podName, containerName string) string {
logDirName := "logs"
if params.Previous {
logDirName = "logs-previous"
}
return fmt.Sprintf(
"%s/namespaces/%s/pods/%s/containers/%s/%s/last-%d-lines.log",
g.GetName(),
namespace,
podName,
containerName,
logDirName,
params.TailLines,
)
},
)
if err != nil {
newErr := fmt.Errorf("unable to get container logs: %v", err)
klog.Warning(newErr.Error())
errs = append(errs, newErr)
}
records = append(records, logRecords...)
}
return records, errs
}