11import { stringify as toYaml } from '@std/yaml' ;
22import { tgz } from '@deno-library/compress' ;
33import { getPodLogs } from './k8s.js' ;
4+ import { getSemaphore } from '@henrygd/semaphore' ;
45
5- export function writeYaml ( data , name , dirPath ) {
6- Deno . mkdirSync ( dirPath , { recursive : true } ) ;
7- const filePath = `${ dirPath } /${ name } .yaml` ;
8- Deno . writeTextFileSync ( filePath , toYaml ( data , { skipInvalid : true } ) ) ;
6+ const semaphore = getSemaphore ( 'supportPackageSemaphore' , 10 ) ;
7+
8+ export async function writeYaml ( data , name , dirPath ) {
9+ await semaphore . acquire ( ) ;
10+ try {
11+ await Deno . mkdir ( dirPath , { recursive : true } ) ;
12+ const filePath = `${ dirPath } /${ name } .yaml` ;
13+ await Deno . writeTextFile ( filePath , toYaml ( data , { skipInvalid : true } ) ) ;
14+ } finally {
15+ semaphore . release ( ) ;
16+ }
917}
1018
1119export async function preparePackage ( dirPath ) {
12- const supportPackageZip = `${ dirPath } .tar.gz` ;
13- console . log ( 'Preparing the Support Package' ) ;
14- await tgz . compress ( dirPath , supportPackageZip ) ;
15- console . log ( 'Cleaning up temp directory' ) ;
16- Deno . removeSync ( dirPath , { recursive : true } ) ;
17- console . log ( `\nPlease attach ${ supportPackageZip } to your support ticket.` ) ;
20+ await semaphore . acquire ( ) ;
21+ try {
22+ const supportPackageZip = `${ dirPath } .tar.gz` ;
23+ console . log ( 'Preparing the Support Package' ) ;
24+ await tgz . compress ( dirPath , supportPackageZip ) ;
25+ console . log ( 'Cleaning up temp directory' ) ;
26+ await Deno . remove ( dirPath , { recursive : true } ) ;
27+ console . log ( `\nPlease attach ${ supportPackageZip } to your support ticket.` ) ;
28+ } finally {
29+ semaphore . release ( ) ;
30+ }
1831}
1932
2033export async function processData ( dirPath , k8sResources ) {
@@ -29,16 +42,21 @@ export async function processData(dirPath, k8sResources) {
2942
3043 if ( k8sType == 'pods' ) {
3144 for ( const pod of resources . items ) {
32- delete pod . metadata . managedFields ;
45+ await semaphore . acquire ( ) ;
46+ try {
47+ delete pod . metadata . managedFields ;
3348
34- writeYaml ( pod , `spec_${ pod . metadata . name } ` , `${ dirPath } /${ k8sType } /${ pod . metadata . name } ` ) ;
49+ await writeYaml ( pod , `spec_${ pod . metadata . name } ` , `${ dirPath } /${ k8sType } /${ pod . metadata . name } ` ) ;
3550
36- const logs = await getPodLogs ( pod ) ;
37- for ( const [ containerName , logData ] of Object . entries ( logs ) ) {
38- Deno . writeTextFileSync (
39- `${ dirPath } /${ k8sType } /${ pod . metadata . name } /log_${ containerName } .log` ,
40- logData ,
41- ) ;
51+ const logs = await getPodLogs ( pod ) ;
52+ for ( const [ containerName , logData ] of Object . entries ( logs ) ) {
53+ await Deno . writeTextFile (
54+ `${ dirPath } /${ k8sType } /${ pod . metadata . name } /log_${ containerName } .log` ,
55+ logData ,
56+ ) ;
57+ }
58+ } finally {
59+ semaphore . release ( ) ;
4260 }
4361 }
4462 continue ;
@@ -60,14 +78,19 @@ export async function processData(dirPath, k8sResources) {
6078 const header = 'LAST SEEN\tTYPE\tREASON\tOBJECT\tMESSAGE\n' ;
6179 const content = header + formattedEvents . join ( '\n' ) ;
6280
63- Deno . writeTextFileSync ( `${ dirPath } /${ k8sType } .csv` , content ) ;
81+ await Deno . writeTextFile ( `${ dirPath } /${ k8sType } .csv` , content ) ;
6482
6583 continue ;
6684 }
6785
68- resources . items . map ( ( data ) => {
69- delete data . metadata . managedFields ;
70- writeYaml ( data , `${ data . metadata . name } _get` , `${ dirPath } /${ k8sType } ` ) ;
71- } ) ;
86+ await Promise . all ( resources . items . map ( async ( data ) => {
87+ await semaphore . acquire ( ) ;
88+ try {
89+ delete data . metadata . managedFields ;
90+ await writeYaml ( data , `${ data . metadata . name } _get` , `${ dirPath } /${ k8sType } ` ) ;
91+ } finally {
92+ semaphore . release ( ) ;
93+ }
94+ } ) ) ;
7295 }
7396}
0 commit comments