Skip to content

Commit cef3813

Browse files
author
Ricardo Lüders
committed
refactor: change gaher_log helper to return array of errors
1 parent 01a3bb4 commit cef3813

8 files changed

+19
-47
lines changed

pkg/gatherers/clusterconfig/openshift_apiserver_operator_logs.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,11 @@ func (g *Gatherer) GatherOpenShiftAPIServerOperatorLogs(ctx context.Context) ([]
4141

4242
coreClient := gatherKubeClient.CoreV1()
4343

44-
records, err := common.CollectLogsFromContainers(
44+
return common.CollectLogsFromContainers(
4545
ctx,
4646
coreClient,
4747
containersFilter,
4848
messagesFilter,
4949
nil,
5050
)
51-
if err != nil {
52-
return nil, []error{err}
53-
}
54-
55-
return records, nil
5651
}

pkg/gatherers/clusterconfig/openshift_authentication_logs.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,11 @@ func (g *Gatherer) GatherOpenshiftAuthenticationLogs(ctx context.Context) ([]rec
4141

4242
coreClient := gatherKubeClient.CoreV1()
4343

44-
records, err := common.CollectLogsFromContainers(
44+
return common.CollectLogsFromContainers(
4545
ctx,
4646
coreClient,
4747
containersFilter,
4848
messagesFilter,
4949
nil,
5050
)
51-
if err != nil {
52-
return nil, []error{err}
53-
}
54-
55-
return records, nil
5651
}

pkg/gatherers/clusterconfig/openshift_sdn_controller_logs.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,11 @@ func (g *Gatherer) GatherOpenshiftSDNControllerLogs(ctx context.Context) ([]reco
5555

5656
coreClient := gatherKubeClient.CoreV1()
5757

58-
records, err := common.CollectLogsFromContainers(
58+
return common.CollectLogsFromContainers(
5959
ctx,
6060
coreClient,
6161
containersFilter,
6262
messagesFilter,
6363
nil,
6464
)
65-
if err != nil {
66-
return nil, []error{err}
67-
}
68-
69-
return records, nil
7065
}

pkg/gatherers/clusterconfig/openshift_sdn_logs.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,11 @@ func (g *Gatherer) GatherOpenshiftSDNLogs(ctx context.Context) ([]record.Record,
4949

5050
coreClient := gatherKubeClient.CoreV1()
5151

52-
records, err := common.CollectLogsFromContainers(
52+
return common.CollectLogsFromContainers(
5353
ctx,
5454
coreClient,
5555
containersFilter,
5656
messagesFilter,
5757
nil,
5858
)
59-
if err != nil {
60-
return nil, []error{err}
61-
}
62-
63-
return records, nil
6459
}

pkg/gatherers/clusterconfig/sap_vsystem_iptables_logs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func gatherSAPLicenseManagementLogs(
9393
nil,
9494
)
9595
if err != nil {
96-
errs = append(errs, err)
96+
errs = append(errs, err...)
9797
} else {
9898
records = append(records, namespaceRecords...)
9999
}

pkg/gatherers/common/gather_logs.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func CollectLogsFromContainers( //nolint:gocyclo
6060
containersFilter LogContainersFilter,
6161
messagesFilter LogMessagesFilter,
6262
buildLogFileName func(namespace string, podName string, containerName string) string,
63-
) ([]record.Record, error) {
63+
) ([]record.Record, []error) {
6464
if buildLogFileName == nil {
6565
buildLogFileName = func(namespace string, podName string, containerName string) string {
6666
return fmt.Sprintf("config/pod/%s/logs/%s/errors.log", namespace, podName)
@@ -72,9 +72,10 @@ func CollectLogsFromContainers( //nolint:gocyclo
7272
FieldSelector: containersFilter.FieldSelector,
7373
})
7474
if err != nil {
75-
return nil, err
75+
return nil, []error{err}
7676
}
7777

78+
var errs []error
7879
var records []record.Record
7980

8081
for i := range pods.Items {
@@ -92,7 +93,7 @@ func CollectLogsFromContainers( //nolint:gocyclo
9293
if len(containersFilter.ContainerNameRegexFilter) > 0 {
9394
match, err := regexp.MatchString(containersFilter.ContainerNameRegexFilter, containerName)
9495
if err != nil {
95-
return nil, err
96+
return nil, []error{err}
9697
}
9798
if !match {
9899
continue
@@ -115,8 +116,8 @@ func CollectLogsFromContainers( //nolint:gocyclo
115116
}
116117

117118
if containersFilter.MaxNamespaceContainers > 0 && len(records) >= containersFilter.MaxNamespaceContainers {
118-
klog.Infof("Max containers per namespace reached (max: %d). Skipping %s for %s.",
119-
containersFilter.MaxNamespaceContainers, containerName, containersFilter.Namespace)
119+
errs = append(errs, fmt.Errorf("skipping %s for %s. Max containers per namespace reached (max: %d)",
120+
containerName, containersFilter.Namespace, containersFilter.MaxNamespaceContainers))
120121
continue
121122
}
122123

@@ -131,7 +132,7 @@ func CollectLogsFromContainers( //nolint:gocyclo
131132

132133
logs, err := filterLogs(ctx, request, messagesFilter.MessagesToSearch, messagesFilter.IsRegexSearch)
133134
if err != nil {
134-
return nil, err
135+
return nil, []error{err}
135136
}
136137

137138
if len(strings.TrimSpace(logs)) != 0 {
@@ -147,7 +148,7 @@ func CollectLogsFromContainers( //nolint:gocyclo
147148
klog.Infof("no pods in %v namespace were found", containersFilter.Namespace)
148149
}
149150

150-
return records, nil
151+
return records, errs
151152
}
152153

153154
func filterLogs(

pkg/gatherers/common/gather_logs_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func testGatherLogs(t *testing.T, regexSearch bool, stringToSearch string, shoul
4545
t.Fatal(err)
4646
}
4747

48-
records, err := CollectLogsFromContainers(
48+
records, errs := CollectLogsFromContainers(
4949
ctx,
5050
coreClient,
5151
LogContainersFilter{
@@ -61,7 +61,7 @@ func testGatherLogs(t *testing.T, regexSearch bool, stringToSearch string, shoul
6161
},
6262
nil,
6363
)
64-
if err != nil {
64+
if len(errs) > 0 {
6565
t.Fatal(err)
6666
}
6767

pkg/gatherers/conditional/gather_logs_of_namespace.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,23 @@ func (g *Gatherer) BuildGatherLogsOfNamespace(paramsInterface interface{}) (gath
3535

3636
return gatherers.GatheringClosure{
3737
Run: func(ctx context.Context) ([]record.Record, []error) {
38-
records, err := g.gatherLogsOfNamespace(ctx, params.Namespace, params.TailLines, params.MaxContainers)
39-
if err != nil {
40-
return records, []error{err}
41-
}
42-
return records, nil
38+
return g.gatherLogsOfNamespace(ctx, params.Namespace, params.TailLines, params.MaxContainers)
4339
},
4440
CanFail: canConditionalGathererFail,
4541
}, nil
4642
}
4743

48-
func (g *Gatherer) gatherLogsOfNamespace(ctx context.Context, namespace string, tailLines int64, maxContainers int) ([]record.Record, error) {
44+
func (g *Gatherer) gatherLogsOfNamespace(ctx context.Context, namespace string, tailLines int64, maxContainers int) ([]record.Record, []error) {
4945
kubeClient, err := kubernetes.NewForConfig(g.gatherProtoKubeConfig)
5046
if err != nil {
51-
return nil, err
47+
return nil, []error{err}
5248
}
5349

5450
coreClient := kubeClient.CoreV1()
5551

5652
fileName := fmt.Sprintf("last-%v-lines.log", tailLines)
5753

58-
records, err := common.CollectLogsFromContainers(
54+
return common.CollectLogsFromContainers(
5955
ctx,
6056
coreClient,
6157
common.LogContainersFilter{
@@ -72,9 +68,4 @@ func (g *Gatherer) gatherLogsOfNamespace(ctx context.Context, namespace string,
7268
)
7369
},
7470
)
75-
if err != nil {
76-
return nil, err
77-
}
78-
79-
return records, nil
8071
}

0 commit comments

Comments
 (0)