1
1
package clusterconfig
2
2
3
3
import (
4
- "bufio"
5
- "context"
6
- "fmt"
7
- "strings"
8
-
9
- corev1 "k8s.io/api/core/v1"
10
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
4
"k8s.io/client-go/kubernetes"
12
- corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
13
- restclient "k8s.io/client-go/rest"
14
- "k8s.io/klog/v2"
15
5
16
6
"github.com/openshift/insights-operator/pkg/record"
17
7
)
@@ -23,7 +13,7 @@ import (
23
13
// The Kubernetes API https://github.com/kubernetes/client-go/blob/master/kubernetes/typed/core/v1/pod_expansion.go#L48
24
14
// Response see https://docs.openshift.com/container-platform/4.6/rest_api/workloads_apis/pod-core-v1.html#apiv1namespacesnamespacepodsnamelog
25
15
//
26
- // Location in archive: config/pod/{namespace-name} /logs/{pod-name}/errors.log
16
+ // Location in archive: config/pod/openshift-apiserver-operator /logs/{pod-name}/errors.log
27
17
func GatherOpenShiftAPIServerOperatorLogs (g * Gatherer ) ([]record.Record , []error ) {
28
18
messagesToSearch := []string {
29
19
"the server has received too many requests and has asked us" ,
@@ -35,87 +25,21 @@ func GatherOpenShiftAPIServerOperatorLogs(g *Gatherer) ([]record.Record, []error
35
25
return nil , []error {err }
36
26
}
37
27
38
- client := gatherKubeClient .CoreV1 ()
39
-
40
- records , err := gatherOpenShiftAPIServerOperatorLastDayLogs (g .ctx , client , messagesToSearch )
41
- if err != nil {
42
- return nil , []error {err }
43
- }
44
-
45
- return records , nil
46
- }
28
+ coreClient := gatherKubeClient .CoreV1 ()
47
29
48
- func gatherOpenShiftAPIServerOperatorLastDayLogs (
49
- ctx context.Context , coreClient corev1client.CoreV1Interface , messagesToSearch []string ,
50
- ) ([]record.Record , error ) {
51
- const namespace = "openshift-apiserver-operator"
52
- var (
53
- sinceSeconds int64 = 86400 // last day
54
- limitBytes int64 = 1024 * 64 // maximum 64 kb of logs
30
+ records , err := gatherLogsFromPodsInNamespace (
31
+ g .ctx ,
32
+ coreClient ,
33
+ "openshift-apiserver-operator" ,
34
+ messagesToSearch ,
35
+ 86400 , // last day
36
+ 1024 * 64 , // maximum 64 kb of logs
37
+ "errors" ,
38
+ "app=openshift-apiserver-operator" ,
55
39
)
56
-
57
- pods , err := coreClient .Pods (namespace ).List (ctx , metav1.ListOptions {})
58
40
if err != nil {
59
- return nil , err
60
- }
61
-
62
- var records []record.Record
63
-
64
- for _ , pod := range pods .Items {
65
- request := coreClient .Pods (namespace ).GetLogs (pod .Name , & corev1.PodLogOptions {
66
- SinceSeconds : & sinceSeconds ,
67
- LimitBytes : & limitBytes ,
68
- })
69
-
70
- logs , err := filterLogs (ctx , request , messagesToSearch )
71
- if err != nil {
72
- return nil , err
73
- }
74
-
75
- if len (strings .TrimSpace (logs )) != 0 {
76
- records = append (records , record.Record {
77
- Name : fmt .Sprintf ("config/pod/%s/logs/%s/errors.log" , pod .Namespace , pod .Name ),
78
- Item : Raw {logs },
79
- })
80
- }
81
- }
82
-
83
- if len (pods .Items ) == 0 {
84
- klog .Infof ("no pods in %v namespace were found" , namespace )
41
+ return nil , []error {err }
85
42
}
86
43
87
44
return records , nil
88
45
}
89
-
90
- func filterLogs (ctx context.Context , request * restclient.Request , messagesToSearch []string ) (string , error ) {
91
- stream , err := request .Stream (ctx )
92
- if err != nil {
93
- return "" , err
94
- }
95
-
96
- defer func () {
97
- err := stream .Close ()
98
- if err != nil {
99
- klog .Errorf ("error during closing a stream: %v" , err )
100
- }
101
- }()
102
-
103
- scanner := bufio .NewScanner (stream )
104
-
105
- var result string
106
-
107
- for scanner .Scan () {
108
- line := scanner .Text ()
109
- for _ , messageToSearch := range messagesToSearch {
110
- if strings .Contains (strings .ToLower (line ), strings .ToLower (messageToSearch )) {
111
- result += line + "\n "
112
- }
113
- }
114
- }
115
-
116
- if err := scanner .Err (); err != nil {
117
- return "" , err
118
- }
119
-
120
- return result , nil
121
- }
0 commit comments