@@ -469,7 +469,7 @@ Collecting PGO CLI logs...
469
469
}
470
470
471
471
// All pgBackRest Logs on the Postgres Instances
472
- err = gatherDbBackrestLogs (ctx , clientset , restConfig , namespace , clusterName , tw , cmd )
472
+ err = gatherDbBackrestLogs (ctx , clientset , restConfig , namespace , clusterName , outputDir , outputFile , tw , cmd )
473
473
if err != nil {
474
474
writeInfo (cmd , fmt .Sprintf ("Error gathering pgBackRest DB Hosts Logs: %s" , err ))
475
475
}
@@ -481,7 +481,7 @@ Collecting PGO CLI logs...
481
481
}
482
482
483
483
// All pgBackRest Logs on the Repo Host
484
- err = gatherRepoHostLogs (ctx , clientset , restConfig , namespace , clusterName , tw , cmd )
484
+ err = gatherRepoHostLogs (ctx , clientset , restConfig , namespace , clusterName , outputDir , outputFile , tw , cmd )
485
485
if err != nil {
486
486
writeInfo (cmd , fmt .Sprintf ("Error gathering pgBackRest Repo Host Logs: %s" , err ))
487
487
}
@@ -1223,6 +1223,8 @@ func gatherDbBackrestLogs(ctx context.Context,
1223
1223
config * rest.Config ,
1224
1224
namespace string ,
1225
1225
clusterName string ,
1226
+ outputDir string ,
1227
+ outputFile string ,
1226
1228
tw * tar.Writer ,
1227
1229
cmd * cobra.Command ,
1228
1230
) error {
@@ -1291,30 +1293,58 @@ func gatherDbBackrestLogs(ctx context.Context,
1291
1293
}
1292
1294
1293
1295
logFiles := strings .Split (strings .TrimSpace (stdout ), "\n " )
1296
+
1297
+ // localDirectory is created to save data on disk
1298
+ // e.g. outputDir/crunchy_k8s_support_export_2022-08-08-115726-0400/remotePath
1299
+ localDirectory := filepath .Join (outputDir , strings .ReplaceAll (outputFile , ".tar.gz" , "" ))
1300
+
1301
+ // flag to determine whether or not to remove localDirectory after the loop
1302
+ // When an error happens, this flag will switch to false
1303
+ // It's nice to have the extra data around when errors have happened
1304
+ doCleanup := true
1305
+
1294
1306
for _ , logFile := range logFiles {
1295
1307
writeDebug (cmd , fmt .Sprintf ("LOG FILE: %s\n " , logFile ))
1296
- var buf bytes.Buffer
1297
-
1298
- stdout , stderr , err := Executor (exec ).catFile (logFile )
1308
+ // get the file size to stream
1309
+ fileSize , err := getRemoteFileSize (config , namespace , pod .Name , util .ContainerDatabase , logFile )
1299
1310
if err != nil {
1300
- if apierrors .IsForbidden (err ) {
1301
- writeInfo (cmd , err .Error ())
1302
- // Continue and output errors for each log file
1303
- // Allow the user to see and address all issues at once
1304
- continue
1305
- }
1306
- return err
1311
+ writeDebug (cmd , fmt .Sprintf ("could not get file size for %s: %v\n " , logFile , err ))
1312
+ continue
1307
1313
}
1308
1314
1309
- buf .Write ([]byte (stdout ))
1310
- if stderr != "" {
1311
- str := fmt .Sprintf ("\n Error returned: %s\n " , stderr )
1312
- buf .Write ([]byte (str ))
1315
+ // fileSpecSrc is namespace/podname:path/to/file
1316
+ // fileSpecDest is the local destination of the file
1317
+ // These are used to help the user grab the file manually when necessary
1318
+ // e.g. postgres-operator/hippo-instance1-vp9k-0:pgdata/pgbackrest/log/db-stanza-create.log
1319
+ fileSpecSrc := fmt .Sprintf ("%s/%s:%s" , namespace , pod .Name , logFile )
1320
+ fileSpecDest := filepath .Join (localDirectory , logFile )
1321
+ writeInfo (cmd , fmt .Sprintf ("\t Size of %-85s %v" , fileSpecSrc , convertBytes (fileSize )))
1322
+
1323
+ // Stream the file to disk and write the local file to the tar
1324
+ err = streamFileFromPod (config , tw ,
1325
+ localDirectory , clusterName , namespace , pod .Name , util .ContainerDatabase , logFile , fileSize )
1326
+
1327
+ if err != nil {
1328
+ doCleanup = false // prevent the deletion of localDirectory so a user can examine contents
1329
+ writeInfo (cmd , fmt .Sprintf ("\t Error streaming file %s: %v" , logFile , err ))
1330
+ writeInfo (cmd , fmt .Sprintf ("\t Collect manually with kubectl cp -c %s %s %s" ,
1331
+ util .ContainerDatabase , fileSpecSrc , fileSpecDest ))
1332
+ writeInfo (cmd , fmt .Sprintf ("\t Remove %s manually after gathering necessary information" , localDirectory ))
1333
+ continue
1313
1334
}
1314
1335
1315
- path := clusterName + fmt .Sprintf ("/pods/%s/" , pod .Name ) + logFile
1316
- if err := writeTar (tw , buf .Bytes (), path , cmd ); err != nil {
1317
- return err
1336
+ }
1337
+
1338
+ // doCleanup is true when there are no errors above.
1339
+ if doCleanup {
1340
+ // Remove the local directory created to hold the data
1341
+ // Errors in removing localDirectory should instruct the user to remove manually.
1342
+ // This happens often on Windows
1343
+ err = os .RemoveAll (localDirectory )
1344
+ if err != nil {
1345
+ writeInfo (cmd , fmt .Sprintf ("\t Error removing %s: %v" , localDirectory , err ))
1346
+ writeInfo (cmd , fmt .Sprintf ("\t You may need to remove %s manually" , localDirectory ))
1347
+ continue
1318
1348
}
1319
1349
}
1320
1350
@@ -1436,6 +1466,8 @@ func gatherRepoHostLogs(ctx context.Context,
1436
1466
config * rest.Config ,
1437
1467
namespace string ,
1438
1468
clusterName string ,
1469
+ outputDir string ,
1470
+ outputFile string ,
1439
1471
tw * tar.Writer ,
1440
1472
cmd * cobra.Command ,
1441
1473
) error {
@@ -1503,30 +1535,57 @@ func gatherRepoHostLogs(ctx context.Context,
1503
1535
}
1504
1536
1505
1537
logFiles := strings .Split (strings .TrimSpace (stdout ), "\n " )
1538
+
1539
+ // localDirectory is created to save data on disk
1540
+ // e.g. outputDir/crunchy_k8s_support_export_2022-08-08-115726-0400/remotePath
1541
+ localDirectory := filepath .Join (outputDir , strings .ReplaceAll (outputFile , ".tar.gz" , "" ))
1542
+
1543
+ // flag to determine whether or not to remove localDirectory after the loop
1544
+ // When an error happens, this flag will switch to false
1545
+ // It's nice to have the extra data around when errors have happened
1546
+ doCleanup := true
1547
+
1506
1548
for _ , logFile := range logFiles {
1507
1549
writeDebug (cmd , fmt .Sprintf ("LOG FILE: %s\n " , logFile ))
1508
- var buf bytes.Buffer
1509
-
1510
- stdout , stderr , err := Executor (exec ).catFile (logFile )
1550
+ // get the file size to stream
1551
+ fileSize , err := getRemoteFileSize (config , namespace , pod .Name , util .ContainerPGBackrest , logFile )
1511
1552
if err != nil {
1512
- if apierrors .IsForbidden (err ) {
1513
- writeInfo (cmd , err .Error ())
1514
- // Continue and output errors for each log file
1515
- // Allow the user to see and address all issues at once
1516
- continue
1517
- }
1518
- return err
1553
+ writeDebug (cmd , fmt .Sprintf ("could not get file size for %s: %v\n " , logFile , err ))
1554
+ continue
1519
1555
}
1520
1556
1521
- buf .Write ([]byte (stdout ))
1522
- if stderr != "" {
1523
- str := fmt .Sprintf ("\n Error returned: %s\n " , stderr )
1524
- buf .Write ([]byte (str ))
1557
+ // fileSpecSrc is namespace/podname:path/to/file
1558
+ // fileSpecDest is the local destination of the file
1559
+ // These are used to help the user grab the file manually when necessary
1560
+ // e.g. postgres-operator/hippo-repo-host-0:pgbackrest/repo1/log/db-backup.log
1561
+ fileSpecSrc := fmt .Sprintf ("%s/%s:%s" , namespace , pod .Name , logFile )
1562
+ fileSpecDest := filepath .Join (localDirectory , logFile )
1563
+ writeInfo (cmd , fmt .Sprintf ("\t Size of %-85s %v" , fileSpecSrc , convertBytes (fileSize )))
1564
+
1565
+ // Stream the file to disk and write the local file to the tar
1566
+ err = streamFileFromPod (config , tw ,
1567
+ localDirectory , clusterName , namespace , pod .Name , util .ContainerPGBackrest , logFile , fileSize )
1568
+
1569
+ if err != nil {
1570
+ doCleanup = false // prevent the deletion of localDirectory so a user can examine contents
1571
+ writeInfo (cmd , fmt .Sprintf ("\t Error streaming file %s: %v" , logFile , err ))
1572
+ writeInfo (cmd , fmt .Sprintf ("\t Collect manually with kubectl cp -c %s %s %s" ,
1573
+ util .ContainerPGBackrest , fileSpecSrc , fileSpecDest ))
1574
+ writeInfo (cmd , fmt .Sprintf ("\t Remove %s manually after gathering necessary information" , localDirectory ))
1575
+ continue
1525
1576
}
1577
+ }
1526
1578
1527
- path := clusterName + fmt .Sprintf ("/pods/%s/" , pod .Name ) + logFile
1528
- if err := writeTar (tw , buf .Bytes (), path , cmd ); err != nil {
1529
- return err
1579
+ // doCleanup is true when there are no errors above.
1580
+ if doCleanup {
1581
+ // Remove the local directory created to hold the data
1582
+ // Errors in removing localDirectory should instruct the user to remove manually.
1583
+ // This happens often on Windows
1584
+ err = os .RemoveAll (localDirectory )
1585
+ if err != nil {
1586
+ writeInfo (cmd , fmt .Sprintf ("\t Error removing %s: %v" , localDirectory , err ))
1587
+ writeInfo (cmd , fmt .Sprintf ("\t You may need to remove %s manually" , localDirectory ))
1588
+ continue
1530
1589
}
1531
1590
}
1532
1591
0 commit comments