/
read.go
190 lines (172 loc) · 6.38 KB
/
read.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
package functional
import (
"context"
"fmt"
"github.com/openshift/cluster-logging-operator/internal/constants"
"github.com/openshift/cluster-logging-operator/test/helpers/oc"
"strings"
"time"
log "github.com/ViaQ/logerr/v2/log/static"
logging "github.com/openshift/cluster-logging-operator/api/logging/v1"
"github.com/openshift/cluster-logging-operator/internal/url"
"github.com/openshift/cluster-logging-operator/internal/utils"
"github.com/openshift/cluster-logging-operator/test"
"github.com/openshift/cluster-logging-operator/test/helpers/cmd"
"github.com/openshift/cluster-logging-operator/test/helpers/kafka"
"github.com/openshift/cluster-logging-operator/test/helpers/types"
"k8s.io/apimachinery/pkg/util/wait"
)
type Option struct {
Name string
Value string
}
func OptionsInclude(name string, options []Option) (bool, Option) {
for _, o := range options {
if o.Name == name {
return true, o
}
}
return false, Option{}
}
func (f *CollectorFunctionalFramework) ReadApplicationLogsFrom(outputName string) ([]types.ApplicationLog, error) {
raw, err := f.ReadLogsFrom(outputName, applicationLog)
if err != nil {
return nil, err
}
var logs []types.ApplicationLog
if err = types.StrictlyParseLogs(utils.ToJsonLogs(raw), &logs); err != nil {
return nil, err
}
return logs, nil
}
func (f *CollectorFunctionalFramework) ReadRawApplicationLogsFrom(outputName string) ([]string, error) {
return f.ReadLogsFrom(outputName, applicationLog)
}
func (f *CollectorFunctionalFramework) ReadInfrastructureLogsFrom(outputName string) ([]string, error) {
return f.ReadLogsFrom(outputName, logging.InputNameInfrastructure)
}
func (f *CollectorFunctionalFramework) ReadApplicationLogsFromKafka(topic string, brokerlistener string, consumercontainername string) (results []string, err error) {
//inter broker zookeeper connect is plaintext so use plaintext port to check on sent messages from kafka producer ie. fluent-kafka plugin
//till this step it must be ensured that a topic is created and published in kafka-consumer-clo-app-topic container within functional pod
var result string
outputFilename := "/shared/consumed.logs"
cmd := fmt.Sprintf("tail -1 %s", outputFilename)
err = wait.PollUntilContextTimeout(context.TODO(), defaultRetryInterval, maxDuration, true, func(cxt context.Context) (done bool, err error) {
result, err = f.RunCommand(consumercontainername, "/bin/bash", "-c", cmd)
if result != "" && err == nil {
return true, nil
}
log.V(4).Error(err, "Polling logs from kafka")
return false, nil
})
if err == nil {
results = strings.Split(strings.TrimSpace(result), "\n")
}
log.V(4).Info("Returning", "logs", result)
return results, err
}
func (f *CollectorFunctionalFramework) ReadAuditLogsFrom(outputName string) ([]string, error) {
return f.ReadLogsFrom(outputName, auditLog)
}
func (f *CollectorFunctionalFramework) Readk8sAuditLogsFrom(outputName string) ([]string, error) {
return f.ReadLogsFrom(outputName, k8sAuditLog)
}
func (f *CollectorFunctionalFramework) ReadOvnAuditLogsFrom(outputName string) ([]string, error) {
return f.ReadLogsFrom(outputName, ovnAuditLog)
}
func (f *CollectorFunctionalFramework) ReadLogsFrom(outputName, sourceType string) (results []string, err error) {
outputSpecs := f.Forwarder.Spec.OutputMap()
outputType := outputName
var outputSpec *logging.OutputSpec
if output, found := outputSpecs[outputName]; found {
outputType = output.Type
outputSpec = output
}
var readLogs func() ([]string, error)
switch outputType {
case logging.OutputTypeKafka:
readLogs = func() ([]string, error) {
switch sourceType {
case logging.InputNameAudit:
sourceType = kafka.AuditLogsTopic
case logging.InputNameInfrastructure:
sourceType = kafka.InfraLogsTopic
default:
sourceType = kafka.AppLogsTopic
}
container := kafka.ConsumerNameForTopic(sourceType)
return f.ReadApplicationLogsFromKafka(sourceType, "localhost:9092", container)
}
case logging.OutputTypeElasticsearch:
readLogs = func() ([]string, error) {
option := Option{"port", "9200"}
if outputSpec != nil {
if esurl, err := url.Parse(outputSpec.URL); err == nil {
option.Value = esurl.Port()
}
}
return f.GetLogsFromElasticSearch(outputName, sourceType, option)
}
default:
readLogs = func() ([]string, error) {
var result string
outputFiles, ok := outputLogFile[outputType]
if !ok {
return nil, fmt.Errorf(fmt.Sprintf("cant find output of type %s in outputSpec %v", outputName, outputSpecs))
}
file, ok := outputFiles[sourceType]
if !ok {
return nil, fmt.Errorf(fmt.Sprintf("can't find log of type %s", sourceType))
}
result, err = f.ReadFileFrom(outputName, file)
if err == nil {
results = strings.Split(strings.TrimSpace(result), "\n")
}
log.V(4).Info("Returning", "logs", result)
return results, err
}
}
return readLogs()
}
func (f *CollectorFunctionalFramework) ReadFileFrom(outputName, filePath string) (result string, err error) {
return f.ReadFileFromWithRetryInterval(outputName, filePath, defaultRetryInterval)
}
func (f *CollectorFunctionalFramework) ReadFileFromWithRetryInterval(outputName, filePath string, retryInterval time.Duration) (result string, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), retryInterval, f.GetMaxReadDuration(), true, func(cxt context.Context) (done bool, err error) {
result, err = f.RunCommand(strings.ToLower(outputName), "cat", filePath)
if result != "" && err == nil {
return true, nil
}
log.V(4).Error(err, "Polling logs")
return false, nil
})
log.V(3).Info("Returning", "content", result)
return result, err
}
func (f *CollectorFunctionalFramework) ReadNApplicationLogsFrom(n uint64, outputName string) ([]string, error) {
lines := []string{}
ctx, cancel := context.WithTimeout(context.Background(), test.SuccessTimeout())
defer cancel()
reader, err := cmd.TailReaderForContainer(f.Pod, outputName, ApplicationLogFile)
if err != nil {
log.V(3).Error(err, "Error creating tail reader")
return nil, err
}
for {
line, err := reader.ReadLineContext(ctx)
if err != nil {
log.V(3).Error(err, "Error readlinecontext")
return nil, err
}
lines = append(lines, line)
n--
if n == 0 {
break
}
}
return lines, err
}
func (f *CollectorFunctionalFramework) ReadCollectorLogs() (string, error) {
output, err := oc.Literal().From("oc logs -n %s pod/%s -c %s", f.Test.NS.Name, f.Name, constants.CollectorName).Run()
return output, err
}