-
Notifications
You must be signed in to change notification settings - Fork 90
/
0_gatherer.go
292 lines (249 loc) · 10.2 KB
/
0_gatherer.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package clusterconfig
import (
"context"
"fmt"
"reflect"
"runtime"
"sort"
"strings"
"time"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"github.com/openshift/insights-operator/pkg/anonymization"
"github.com/openshift/insights-operator/pkg/record"
"github.com/openshift/insights-operator/pkg/recorder"
"github.com/openshift/insights-operator/pkg/utils"
)
// gatherMetadata contains general information about collected data
type gatherMetadata struct {
// info about gathering functions
StatusReports []gathererStatusReport `json:"status_reports"`
MemoryAlloc uint64 `json:"memory_alloc_bytes"`
Uptime float64 `json:"uptime_seconds"`
// shows if obfuscation(hiding IPs and cluster domain) is enabled
IsGlobalObfuscationEnabled bool `json:"is_global_obfuscation_enabled"`
}
// gathererStatusReport contains general information about specific gatherer function
type gathererStatusReport struct {
Name string `json:"name"`
Duration time.Duration `json:"duration_in_ms"`
RecordsCount int `json:"records_count"`
Errors []string `json:"errors"`
}
// Gatherer is a driving instance invoking collection of data
type Gatherer struct {
ctx context.Context
gatherKubeConfig *rest.Config
gatherProtoKubeConfig *rest.Config
metricsGatherKubeConfig *rest.Config
anonymizer *anonymization.Anonymizer
startTime time.Time
}
type gatherResult struct {
records []record.Record
errors []error
}
type gatherFunction func(g *Gatherer, c chan<- gatherResult)
type gathering struct {
function gatherFunction
canFail bool
}
func important(function gatherFunction) gathering {
return gathering{function, false}
}
func failable(function gatherFunction) gathering {
return gathering{function, true}
}
const gatherAll = "ALL"
var gatherFunctions = map[string]gathering{
"pdbs": important(GatherPodDisruptionBudgets),
"metrics": failable(GatherMostRecentMetrics),
"operators": important(GatherClusterOperators),
"operators_pods_and_events": important(GatherClusterOperatorPodsAndEvents),
"container_images": important(GatherContainerImages),
"workload_info": failable(GatherWorkloadInfo),
"nodes": important(GatherNodes),
"config_maps": failable(GatherConfigMaps),
"version": important(GatherClusterVersion),
"infrastructures": important(GatherClusterInfrastructure),
"networks": important(GatherClusterNetwork),
"authentication": important(GatherClusterAuthentication),
"image_registries": important(GatherClusterImageRegistry),
"image_pruners": important(GatherClusterImagePruner),
"feature_gates": important(GatherClusterFeatureGates),
"oauths": important(GatherClusterOAuth),
"ingress": important(GatherClusterIngress),
"proxies": important(GatherClusterProxy),
"certificate_signing_requests": important(GatherCertificateSigningRequests),
"crds": important(GatherCRD),
"host_subnets": important(GatherHostSubnet),
"machine_sets": important(GatherMachineSet),
"install_plans": important(GatherInstallPlans),
"service_accounts": important(GatherServiceAccounts),
"machine_config_pools": important(GatherMachineConfigPool),
"container_runtime_configs": important(GatherContainerRuntimeConfig),
"netnamespaces": important(GatherNetNamespace),
"openshift_apiserver_operator_logs": failable(GatherOpenShiftAPIServerOperatorLogs),
"openshift_sdn_logs": failable(GatherOpenshiftSDNLogs),
"openshift_sdn_controller_logs": failable(GatherOpenshiftSDNControllerLogs),
"openshift_authentication_logs": failable(GatherOpenshiftAuthenticationLogs),
"sap_config": failable(GatherSAPConfig),
"sap_license_management_logs": failable(GatherSAPVsystemIptablesLogs),
"sap_pods": failable(GatherSAPPods),
"sap_datahubs": failable(GatherSAPDatahubs),
"olm_operators": failable(GatherOLMOperators),
"pod_network_connectivity_checks": failable(GatherPNCC),
}
// New creates new Gatherer
func New(
gatherKubeConfig, gatherProtoKubeConfig, metricsGatherKubeConfig *rest.Config, anonymizer *anonymization.Anonymizer,
) *Gatherer {
return &Gatherer{
gatherKubeConfig: gatherKubeConfig,
gatherProtoKubeConfig: gatherProtoKubeConfig,
metricsGatherKubeConfig: metricsGatherKubeConfig,
anonymizer: anonymizer,
startTime: time.Now(),
}
}
// GatherInfo from reflection
type GatherInfo struct {
name string
result gatherResult
function gatherFunction
canFail bool
rvString string
}
// NewGatherInfo that holds reflection information
func NewGatherInfo(gather string, rv reflect.Value) *GatherInfo {
gatherFunc := gatherFunctions[gather].function
return &GatherInfo{
name: runtime.FuncForPC(reflect.ValueOf(gatherFunc).Pointer()).Name(),
result: rv.Interface().(gatherResult),
function: gatherFunc,
canFail: gatherFunctions[gather].canFail,
rvString: rv.String(),
}
}
// Gather is hosting and calling all the recording functions
func (g *Gatherer) Gather(ctx context.Context, gatherList []string, recorder recorder.Interface) error {
g.ctx = ctx
var errors []string
var gatherReport gatherMetadata
if len(gatherList) == 0 {
errors = append(errors, "no gather functions are specified to run")
}
if utils.StringInSlice(gatherAll, gatherList) {
gatherList = fullGatherList()
}
// Starts the gathers in Go routines
cases, starts, err := g.startGathering(gatherList, &errors)
if err != nil {
return err
}
// Gets the info from the Go routines
for range gatherList {
chosen, value, _ := reflect.Select(cases)
// The chosen channel has been closed, so zero out the channel to disable the case
cases[chosen].Chan = reflect.ValueOf(nil)
gather := gatherList[chosen]
gi := NewGatherInfo(gather, value)
statusReport, errorsReport := createStatusReport(gi, recorder, starts[chosen])
if len(errorsReport) > 0 {
errors = append(errors, errorsReport...)
}
gatherReport.StatusReports = append(gatherReport.StatusReports, statusReport)
}
// if obfuscation is enabled, we want to know it from the archive
gatherReport.IsGlobalObfuscationEnabled = g.anonymizer != nil
// fill in performance related data to the report
var m runtime.MemStats
runtime.ReadMemStats(&m)
gatherReport.MemoryAlloc = m.HeapAlloc
gatherReport.Uptime = time.Since(g.startTime).Truncate(time.Millisecond).Seconds()
// records the report
if err := recordGatherReport(recorder, gatherReport); err != nil {
errors = append(errors, fmt.Sprintf("unable to record io status reports: %v", err))
}
if len(errors) > 0 {
return sumErrors(errors)
}
return nil
}
func createStatusReport(gather *GatherInfo, recorder recorder.Interface, starts time.Time) (gathererStatusReport, []string) {
var errors []string
elapsed := time.Since(starts).Truncate(time.Millisecond)
klog.V(4).Infof("Gather %s took %s to process %d records", gather.name, elapsed, len(gather.result.records))
shortName := strings.Replace(gather.name, "github.com/openshift/insights-operator/pkg/gather/", "", 1)
report := gathererStatusReport{shortName, time.Duration(elapsed.Milliseconds()), len(gather.result.records), extractErrors(gather.result.errors)}
if gather.canFail {
for _, err := range gather.result.errors {
klog.V(5).Infof("Couldn't gather %s' received following error: %s\n", gather.name, err.Error())
}
} else {
errors = extractErrors(gather.result.errors)
}
errors = append(errors, recordStatusReport(recorder, gather.result.records)...)
klog.V(5).Infof("Read from %s's channel and received %s\n", gather.name, gather.rvString)
return report, errors
}
func recordStatusReport(recorder recorder.Interface, records []record.Record) []string {
var errors []string
for _, r := range records {
if err := recorder.Record(r); err != nil {
errors = append(errors, fmt.Sprintf("unable to record %s: %v", r.Name, err))
continue
}
}
return errors
}
// Runs each gather functions in a goroutine.
// Every gather function is given its own channel to send back the results.
// 1. return value: `cases` list, used for dynamically reading from the channels.
// 2. return value: `starts` list, contains that start time of each gather function.
func (g *Gatherer) startGathering(gatherList []string, errors *[]string) ([]reflect.SelectCase, []time.Time, error) {
var cases []reflect.SelectCase
var starts []time.Time
// Starts the gathers in Go routines
for _, gatherID := range gatherList {
gather, ok := gatherFunctions[gatherID]
gFn := gather.function
if !ok {
*errors = append(*errors, fmt.Sprintf("unknown gatherId in config: %s", gatherID))
continue
}
channel := make(chan gatherResult)
cases = append(cases, reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(channel)})
gatherName := runtime.FuncForPC(reflect.ValueOf(gFn).Pointer()).Name()
klog.V(5).Infof("Gathering %s", gatherName)
starts = append(starts, time.Now())
go gFn(g, channel)
if err := g.ctx.Err(); err != nil {
return nil, nil, err
}
}
return cases, starts, nil
}
func recordGatherReport(recorder recorder.Interface, metadata gatherMetadata) error {
r := record.Record{Name: "insights-operator/gathers", Item: record.JSONMarshaller{Object: metadata}}
return recorder.Record(r)
}
func extractErrors(errors []error) []string {
var errStrings []string
for _, err := range errors {
errStrings = append(errStrings, err.Error())
}
return errStrings
}
func sumErrors(errors []string) error {
sort.Strings(errors)
errors = utils.UniqueStrings(errors)
return fmt.Errorf("%s", strings.Join(errors, ", "))
}
func fullGatherList() []string {
gatherList := make([]string, 0, len(gatherFunctions))
for k := range gatherFunctions {
gatherList = append(gatherList, k)
}
return gatherList
}