21
21
import java .util .Arrays ;
22
22
import java .util .Collections ;
23
23
import java .util .List ;
24
+ import java .util .function .Supplier ;
24
25
25
26
/**
26
27
* LogCollector class containing all methods used for logs and YAML collection.
@@ -188,10 +189,14 @@ public void collectClusterWideResourcesToFolder(boolean logPerFile, String folde
188
189
if (logPerFile ) {
189
190
collectClusterWideResourcesPerFile (clusterWideFolderPath , resourceType );
190
191
} else {
191
- String yaml = kubeCmdClient .getClusterWideResourcesAsYaml (resourceType );
192
+ String yaml = executeCollectionCall (
193
+ String .format ("collect descriptions of type: %s" , resourceType ),
194
+ () -> kubeCmdClient .getClusterWideResourcesAsYaml (resourceType )
195
+ );
192
196
String resFileName = LogCollectorUtils .getYamlFileNameForResource (resourceType );
193
197
String filePath = LogCollectorUtils
194
198
.getFullPathForFolderPathAndFileName (clusterWideFolderPath , resFileName );
199
+
195
200
writeDataToFile (filePath , yaml );
196
201
}
197
202
});
@@ -210,9 +215,12 @@ public void collectClusterWideResourcesToFolder(boolean logPerFile, String folde
210
215
*/
211
216
public void collectLogsFromPodsInNamespace (String namespaceName , String namespaceFolderPath ) {
212
217
LOGGER .info ("Collecting logs from all Pods in Namespace: {}" , namespaceName );
213
- List <Pod > pods = kubeClient .listPods (namespaceName );
218
+ List <Pod > pods = executeCollectionCall (
219
+ String .format ("list Pods in Namespace: %s" , namespaceName ),
220
+ () -> kubeClient .listPods (namespaceName )
221
+ );
214
222
215
- if (!pods .isEmpty ()) {
223
+ if (pods != null && !pods .isEmpty ()) {
216
224
String podsFolderPath = createResourceDirectoryInNamespaceDir (namespaceFolderPath , CollectorConstants .POD );
217
225
218
226
pods .forEach (pod -> {
@@ -262,7 +270,10 @@ private void collectLogsFromPodContainer(
262
270
String podName ,
263
271
String containerName
264
272
) {
265
- String containerLog = kubeCmdClient .inNamespace (namespaceName ).logs (podName , containerName );
273
+ String containerLog = executeCollectionCall (
274
+ String .format ("collect logs from Pod:%s" , podName ),
275
+ () -> kubeCmdClient .inNamespace (namespaceName ).logs (podName , containerName )
276
+ );
266
277
String podConLogFileName = LogCollectorUtils .getLogFileNameForPodContainer (podName , containerName );
267
278
String filePath = LogCollectorUtils .getFullPathForFolderPathAndFileName (podsFolderPath , podConLogFileName );
268
279
@@ -277,7 +288,10 @@ private void collectLogsFromPodContainer(
277
288
* @param podName name of Pod from which the description should be collected
278
289
*/
279
290
private void collectPodDescription (String namespaceName , String podsFolderPath , String podName ) {
280
- String podDesc = kubeCmdClient .inNamespace (namespaceName ).describe (CollectorConstants .POD , podName );
291
+ String podDesc = executeCollectionCall (
292
+ String .format ("collect description of Pod:%s" , podName ),
293
+ () -> kubeCmdClient .inNamespace (namespaceName ).describe (CollectorConstants .POD , podName )
294
+ );
281
295
String podDescFileName = LogCollectorUtils .getLogFileNameForPodDescription (podName );
282
296
String filePath = LogCollectorUtils .getFullPathForFolderPathAndFileName (podsFolderPath , podDescFileName );
283
297
@@ -296,7 +310,10 @@ private void collectClusterWideResourcesPerFile(String clusterWideFolderPath,Str
296
310
String fullFolderPath = createResourceDirectoryInNamespaceDir (clusterWideFolderPath , resourceType );
297
311
298
312
resources .forEach (resourceName -> {
299
- String yaml = kubeCmdClient .getClusterWideResourceAsYaml (resourceType , resourceName );
313
+ String yaml = executeCollectionCall (
314
+ String .format ("collect YAML description for %s:%s" , resourceType , resourceName ),
315
+ () -> kubeCmdClient .getClusterWideResourceAsYaml (resourceType , resourceName )
316
+ );
300
317
301
318
String resFileName = LogCollectorUtils .getYamlFileNameForResource (resourceName );
302
319
String fileName = LogCollectorUtils .getFullPathForFolderPathAndFileName (fullFolderPath , resFileName );
@@ -313,7 +330,10 @@ private void collectClusterWideResourcesPerFile(String clusterWideFolderPath,Str
313
330
*/
314
331
public void collectEventsFromNamespace (String namespaceName , String namespaceFolderPath ) {
315
332
LOGGER .info ("Collecting events from Namespace: {}" , namespaceName );
316
- String events = kubeCmdClient .inNamespace (namespaceName ).getEvents ();
333
+ String events = executeCollectionCall (
334
+ String .format ("collect %s from %s" , CollectorConstants .EVENTS , namespaceName ),
335
+ () -> kubeCmdClient .inNamespace (namespaceName ).getEvents ()
336
+ );
317
337
String eventsFileName = LogCollectorUtils .getLogFileNameForResource (CollectorConstants .EVENTS );
318
338
String fileName = LogCollectorUtils .getFullPathForFolderPathAndFileName (namespaceFolderPath , eventsFileName );
319
339
@@ -349,13 +369,19 @@ private void collectDescriptionOfResourceInNamespace(
349
369
String resourceType
350
370
) {
351
371
LOGGER .info ("Collecting YAMLs of {} from Namespace: {}" , resourceType , namespaceName );
352
- List <String > resources = kubeCmdClient .inNamespace (namespaceName ).list (resourceType );
372
+ List <String > resources = executeCollectionCall (
373
+ String .format ("list resources of type: %s in Namespace: %s" , resourceType , namespaceName ),
374
+ () -> kubeCmdClient .inNamespace (namespaceName ).list (resourceType )
375
+ );
353
376
354
377
if (resources != null && !resources .isEmpty ()) {
355
378
String fullFolderPath = createResourceDirectoryInNamespaceDir (namespaceFolderPath , resourceType );
356
379
357
380
resources .forEach (resourceName -> {
358
- String yaml = kubeCmdClient .inNamespace (namespaceName ).getResourceAsYaml (resourceType , resourceName );
381
+ String yaml = executeCollectionCall (
382
+ String .format ("collect YAML description for %s:%s" , resourceType , resourceName ),
383
+ () -> kubeCmdClient .inNamespace (namespaceName ).getResourceAsYaml (resourceType , resourceName )
384
+ );
359
385
360
386
String resFileName = LogCollectorUtils .getYamlFileNameForResource (resourceName );
361
387
String fileName = LogCollectorUtils .getFullPathForFolderPathAndFileName (fullFolderPath , resFileName );
@@ -433,4 +459,24 @@ private void writeDataToFile(String fullFilePath, String data) {
433
459
}
434
460
}
435
461
}
462
+
463
+ /**
464
+ * Method for executing the collection (or list) call, which handles the exceptions when the resource is not found
465
+ * (or was removed during the process). That way the LogCollector will continue with collection of other resources.
466
+ *
467
+ * @param errorOperationMessage message for the operation that is being executed, for error logging
468
+ * @param executeCall Supplier with the lambda method for collection of the data
469
+ *
470
+ * @return result of the execution of the method -> YAML descriptions, logs, or lists of resources
471
+ *
472
+ * @param <T> type of the return value -> same as the return type of the executed call
473
+ */
474
+ private <T > T executeCollectionCall (String errorOperationMessage , Supplier <T > executeCall ) {
475
+ try {
476
+ return executeCall .get ();
477
+ } catch (Exception e ) {
478
+ LOGGER .warn ("Failed to {}, due to: {}" , errorOperationMessage , e .getMessage ());
479
+ return null ;
480
+ }
481
+ }
436
482
}
0 commit comments