1
1
import { stringify as toYaml } from '@std/yaml' ;
2
2
import { tgz } from '@deno-library/compress' ;
3
3
import { getPodLogs } from './k8s.js' ;
4
+ import { getSemaphore } from '@henrygd/semaphore' ;
4
5
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
+ }
9
17
}
10
18
11
19
export 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
+ }
18
31
}
19
32
20
33
export async function processData ( dirPath , k8sResources ) {
@@ -29,16 +42,21 @@ export async function processData(dirPath, k8sResources) {
29
42
30
43
if ( k8sType == 'pods' ) {
31
44
for ( const pod of resources . items ) {
32
- delete pod . metadata . managedFields ;
45
+ await semaphore . acquire ( ) ;
46
+ try {
47
+ delete pod . metadata . managedFields ;
33
48
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 } ` ) ;
35
50
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 ( ) ;
42
60
}
43
61
}
44
62
continue ;
@@ -60,14 +78,19 @@ export async function processData(dirPath, k8sResources) {
60
78
const header = 'LAST SEEN\tTYPE\tREASON\tOBJECT\tMESSAGE\n' ;
61
79
const content = header + formattedEvents . join ( '\n' ) ;
62
80
63
- Deno . writeTextFileSync ( `${ dirPath } /${ k8sType } .csv` , content ) ;
81
+ await Deno . writeTextFile ( `${ dirPath } /${ k8sType } .csv` , content ) ;
64
82
65
83
continue ;
66
84
}
67
85
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
+ } ) ) ;
72
95
}
73
96
}
0 commit comments