@@ -107,19 +107,16 @@ func (d *GatherJob) Gather(ctx context.Context, kubeConfig, protoKubeConfig *res
107
107
return gather .RecordArchiveMetadata (mapToArray (allFunctionReports ), rec , anonymizer )
108
108
}
109
109
110
- // GatherAndUpload runs a single gather and stores the generated archive, uploads it
111
- // and waits for the corresponding Insights analysis report.
112
- // 1. Creates the necessary configs/clients
113
- // 2. Creates the configobserver
114
- // 3. Initiates the recorder
115
- // 4. Executes a Gather
116
- // 5. Flushes the results
117
- // 6. Get the latest archive
118
- // 7. Uploads the archive
119
- // 8. Waits for the corresponding Insights analysis download
110
+ // GatherAndUpload runs a single gather and stores the generated archive, uploads it.
111
+ // 1. Prepare the necessary kube configs
112
+ // 2. Get the corresponding "datagathers.insights.openshift.io" resource
113
+ // 3. Create all the gatherers
114
+ // 4. Run data gathering
115
+ // 5. Recodrd the data into the Insights archive
116
+ // 6. Get the latest archive and upload it
117
+ // 7. Updates the status of the corresponding "datagathers.insights.openshift.io" resource continuously
120
118
func (d * GatherJob ) GatherAndUpload (kubeConfig , protoKubeConfig * rest.Config ) error { // nolint: funlen, gocyclo
121
- klog .Infof ("Starting insights-operator %s" , version .Get ().String ())
122
- // these are operator clients
119
+ klog .Info ("Starting data gathering" )
123
120
kubeClient , err := kubernetes .NewForConfig (protoKubeConfig )
124
121
if err != nil {
125
122
return err
@@ -143,11 +140,8 @@ func (d *GatherJob) GatherAndUpload(kubeConfig, protoKubeConfig *rest.Config) er
143
140
klog .Error ("failed to get coresponding DataGather custom resource: %v" , err )
144
141
return err
145
142
}
146
- updatedCR := dataGatherCR .DeepCopy ()
147
- updatedCR .Status .State = insightsv1alpha1 .Running
148
- updatedCR .Status .StartTime = metav1 .Now ()
149
143
150
- dataGatherCR , err = insightClient . DataGathers (). UpdateStatus ( ctx , updatedCR , metav1. UpdateOptions {} )
144
+ dataGatherCR , err = updateDataGatherStatus ( ctx , * insightClient , dataGatherCR . DeepCopy (), insightsv1alpha1 . Pending , nil )
151
145
if err != nil {
152
146
klog .Error ("failed to update coresponding DataGather custom resource: %v" , err )
153
147
return err
@@ -186,6 +180,11 @@ func (d *GatherJob) GatherAndUpload(kubeConfig, protoKubeConfig *rest.Config) er
186
180
)
187
181
uploader := insightsuploader .New (nil , insightsClient , configObserver , nil , nil , 0 )
188
182
183
+ dataGatherCR , err = updateDataGatherStatus (ctx , * insightClient , dataGatherCR , insightsv1alpha1 .Running , nil )
184
+ if err != nil {
185
+ klog .Error ("failed to update coresponding DataGather custom resource: %v" , err )
186
+ return err
187
+ }
189
188
allFunctionReports := make (map [string ]gather.GathererFunctionReport )
190
189
for _ , gatherer := range gatherers {
191
190
functionReports , err := gather .CollectAndRecordGatherer (ctx , gatherer , rec , dataGatherCR .Spec .Gatherers ) // nolint: govet
@@ -197,50 +196,50 @@ func (d *GatherJob) GatherAndUpload(kubeConfig, protoKubeConfig *rest.Config) er
197
196
allFunctionReports [functionReports [i ].FuncName ] = functionReports [i ]
198
197
}
199
198
}
200
- err = gather .RecordArchiveMetadata (mapToArray (allFunctionReports ), rec , anonymizer )
201
- if err != nil {
202
- klog .Error (err )
203
- return err
204
- }
205
- err = rec .Flush ()
206
- if err != nil {
207
- klog .Error (err )
208
- return err
199
+
200
+ for k := range allFunctionReports {
201
+ fr := allFunctionReports [k ]
202
+ // duration = 0 means the gatherer didn't run
203
+ if fr .Duration == 0 {
204
+ continue
205
+ }
206
+
207
+ gs := status .CreateDataGatherGathererStatus (& fr )
208
+ dataGatherCR .Status .Gatherers = append (dataGatherCR .Status .Gatherers , gs )
209
209
}
210
- lastArchive , err := recdriver .LastArchive ()
210
+
211
+ // record data
212
+ conditions := []metav1.Condition {}
213
+ lastArchive , err := record (mapToArray (allFunctionReports ), rec , recdriver , anonymizer )
211
214
if err != nil {
212
- klog .Error (err )
215
+ conditions = append (conditions , status .DataRecordedCondition (metav1 .ConditionFalse , "RecordingFailed" ,
216
+ fmt .Sprintf ("Failed to record data: %v" , err )))
217
+ _ , recErr := updateDataGatherStatus (ctx , * insightClient , dataGatherCR , insightsv1alpha1 .Failed , conditions )
218
+ if recErr != nil {
219
+ klog .Error ("data recording failed and the update of DataGaher resource status failed as well: %v" , recErr )
220
+ }
213
221
return err
214
222
}
223
+ conditions = append (conditions , status .DataRecordedCondition (metav1 .ConditionTrue , "AsExpected" , "" ))
224
+
225
+ // upload data
215
226
insightsRequestID , err := uploader .Upload (ctx , lastArchive )
216
227
if err != nil {
217
228
klog .Error (err )
229
+ conditions = append (conditions , status .DataUploadedCondition (metav1 .ConditionFalse , "UploadFailed" ,
230
+ fmt .Sprintf ("Failed to upload data: %v" , err )))
231
+ _ , updateErr := updateDataGatherStatus (ctx , * insightClient , dataGatherCR , insightsv1alpha1 .Failed , conditions )
232
+ if updateErr != nil {
233
+ klog .Error ("data upload failed and the update of DataGaher resource status failed as well: %v" , updateErr )
234
+ }
218
235
return err
219
236
}
220
237
klog .Infof ("Insights archive successfully uploaded with InsightsRequestID: %s" , insightsRequestID )
221
238
222
- dataGatherCR .Status .FinishTime = metav1 .Now ()
223
- dataGatherCR .Status .State = insightsv1alpha1 .Completed
224
239
dataGatherCR .Status .InsightsRequestID = insightsRequestID
225
- dataGatherCR .Status .Conditions = []metav1.Condition {
226
- {
227
- Type : "DataUploaded" ,
228
- Status : metav1 .ConditionTrue ,
229
- Reason : "AsExpected" ,
230
- LastTransitionTime : metav1 .Now (),
231
- },
232
- }
233
- for k := range allFunctionReports {
234
- fr := allFunctionReports [k ]
235
- // duration = 0 means the gatherer didn't run
236
- if fr .Duration == 0 {
237
- continue
238
- }
240
+ conditions = append (conditions , status .DataUploadedCondition (metav1 .ConditionTrue , "AsExpected" , "" ))
239
241
240
- gs := status .CreateDataGatherGathererStatus (& fr )
241
- dataGatherCR .Status .Gatherers = append (dataGatherCR .Status .Gatherers , gs )
242
- }
243
- _ , err = insightClient .DataGathers ().UpdateStatus (ctx , dataGatherCR , metav1.UpdateOptions {})
242
+ _ , err = updateDataGatherStatus (ctx , * insightClient , dataGatherCR , insightsv1alpha1 .Completed , conditions )
244
243
if err != nil {
245
244
klog .Error (err )
246
245
return err
@@ -256,3 +255,41 @@ func mapToArray(m map[string]gather.GathererFunctionReport) []gather.GathererFun
256
255
}
257
256
return a
258
257
}
258
+
259
+ // record is a helper function recording the archive metadata as well as data.
260
+ // Returns last known Insights archive and an error when recording failed.
261
+ func record (functionReports []gather.GathererFunctionReport ,
262
+ rec * recorder.Recorder , recdriver * diskrecorder.DiskRecorder , anonymizer * anonymization.Anonymizer ) (* insightsclient.Source , error ) {
263
+ err := gather .RecordArchiveMetadata (functionReports , rec , anonymizer )
264
+ if err != nil {
265
+ return nil , err
266
+ }
267
+ err = rec .Flush ()
268
+ if err != nil {
269
+ return nil , err
270
+ }
271
+ return recdriver .LastArchive ()
272
+ }
273
+
274
+ // updateDataGatherStatus updates status' time attributes, state and conditions
275
+ // of the provided DataGather resource
276
+ func updateDataGatherStatus (ctx context.Context ,
277
+ insightsClient insightsv1alpha1cli.InsightsV1alpha1Client ,
278
+ dataGatherCR * insightsv1alpha1.DataGather ,
279
+ newState insightsv1alpha1.DataGatherState , conditions []metav1.Condition ) (* insightsv1alpha1.DataGather , error ) {
280
+ switch newState {
281
+ case insightsv1alpha1 .Completed :
282
+ dataGatherCR .Status .FinishTime = metav1 .Now ()
283
+ case insightsv1alpha1 .Failed :
284
+ dataGatherCR .Status .FinishTime = metav1 .Now ()
285
+ case insightsv1alpha1 .Running :
286
+ dataGatherCR .Status .StartTime = metav1 .Now ()
287
+ case insightsv1alpha1 .Pending :
288
+ // no op
289
+ }
290
+ dataGatherCR .Status .State = newState
291
+ if conditions != nil {
292
+ dataGatherCR .Status .Conditions = append (dataGatherCR .Status .Conditions , conditions ... )
293
+ }
294
+ return insightsClient .DataGathers ().UpdateStatus (ctx , dataGatherCR , metav1.UpdateOptions {})
295
+ }
0 commit comments