@@ -34,67 +34,72 @@ export async function processData(dirPath, k8sResources) {
3434 console . log ( 'Processing and Saving Data' ) ;
3535
3636 for ( const [ k8sType , fetcher ] of Object . entries ( k8sResources ) ) {
37- const resources = await fetcher ( ) ;
37+ try {
38+ console . log ( `Processing Data for ${ k8sType } ` ) ;
39+ const resources = await fetcher ( ) ;
3840
39- if ( ! resources ) {
40- continue ;
41- }
42-
43- const semaphore = getSemaphore ( k8sType , 10 ) ;
44- console . log ( `Processing Data for ${ k8sType } ` ) ;
45-
46- if ( k8sType == 'pods' ) {
47- for ( const pod of resources . items ) {
48- await semaphore . acquire ( ) ;
49- try {
50- delete pod . metadata . managedFields ;
51-
52- await writeYaml ( pod , `spec_${ pod . metadata . name } ` , `${ dirPath } /${ k8sType } /${ pod . metadata . name } ` ) ;
41+ if ( ! resources || ! resources . items || resources . items . length === 0 ) {
42+ continue ;
43+ }
5344
54- const logs = await getPodLogs ( pod ) ;
55- console . log ( `Gathering logs for pod ${ pod . metadata . name } ` ) ;
56- for ( const [ containerName , logData ] of Object . entries ( logs ) ) {
57- await Deno . writeTextFile (
58- `${ dirPath } /${ k8sType } /${ pod . metadata . name } /log_${ containerName } .log` ,
59- logData ,
60- ) ;
45+ const semaphore = getSemaphore ( k8sType , 10 ) ;
46+
47+ if ( k8sType == 'pods' ) {
48+ for ( const pod of resources . items ) {
49+ await semaphore . acquire ( ) ;
50+ try {
51+ delete pod . metadata . managedFields ;
52+
53+ await writeYaml ( pod , `spec_${ pod . metadata . name } ` , `${ dirPath } /${ k8sType } /${ pod . metadata . name } ` ) ;
54+
55+ const logs = await getPodLogs ( pod ) ;
56+ console . log ( `Gathering logs for pod ${ pod . metadata . name } ` ) ;
57+ for ( const [ containerName , logData ] of Object . entries ( logs ) ) {
58+ await Deno . writeTextFile (
59+ `${ dirPath } /${ k8sType } /${ pod . metadata . name } /log_${ containerName } .log` ,
60+ logData ,
61+ ) ;
62+ }
63+ } finally {
64+ semaphore . release ( ) ;
6165 }
62- } finally {
63- semaphore . release ( ) ;
6466 }
67+ continue ;
6568 }
66- continue ;
67- }
6869
69- if ( k8sType == 'events.k8s.io' ) {
70- const formattedEvents = resources . items . map ( ( event ) => {
71- const lastSeen = event . metadata . creationTimestamp
72- ? new Date ( event . metadata . creationTimestamp ) . toISOString ( )
73- : 'Invalid Date' ;
74- const type = event . type || 'Unknown' ;
75- const reason = event . reason || 'Unknown' ;
76- const object = `${ event . involvedObject . kind } /${ event . involvedObject . name } ` ;
77- const message = event . message || 'No message' ;
70+ if ( k8sType == 'events.k8s.io' ) {
71+ const formattedEvents = resources . items . map ( ( event ) => {
72+ const lastSeen = event . metadata . creationTimestamp
73+ ? new Date ( event . metadata . creationTimestamp ) . toISOString ( )
74+ : 'Invalid Date' ;
75+ const type = event . type || 'Unknown' ;
76+ const reason = event . reason || 'Unknown' ;
77+ const object = `${ event . involvedObject . kind } /${ event . involvedObject . name } ` ;
78+ const message = event . message || 'No message' ;
79+
80+ return `${ lastSeen } \t${ type } \t${ reason } \t${ object } \t${ message } ` ;
81+ } ) ;
7882
79- return ` ${ lastSeen } \t ${ type } \t ${ reason } \t ${ object } \t ${ message } ` ;
80- } ) ;
83+ const header = 'LAST SEEN\tTYPE\tREASON\tOBJECT\tMESSAGE\n' ;
84+ const content = header + formattedEvents . join ( '\n' ) ;
8185
82- const header = 'LAST SEEN\tTYPE\tREASON\tOBJECT\tMESSAGE\n' ;
83- const content = header + formattedEvents . join ( '\n' ) ;
86+ await Deno . writeTextFile ( `${ dirPath } /${ k8sType } .csv` , content ) ;
8487
85- await Deno . writeTextFile ( `${ dirPath } /${ k8sType } .csv` , content ) ;
88+ continue ;
89+ }
8690
91+ await Promise . all ( resources . items . map ( async ( data ) => {
92+ await semaphore . acquire ( ) ;
93+ try {
94+ delete data . metadata . managedFields ;
95+ await writeYaml ( data , `${ data . metadata . name } _get` , `${ dirPath } /${ k8sType } ` ) ;
96+ } finally {
97+ semaphore . release ( ) ;
98+ }
99+ } ) ) ;
100+ } catch ( error ) {
101+ console . warn ( `Failed to fetch ${ k8sType } : ${ error . message } ` ) ;
87102 continue ;
88103 }
89-
90- await Promise . all ( resources . items . map ( async ( data ) => {
91- await semaphore . acquire ( ) ;
92- try {
93- delete data . metadata . managedFields ;
94- await writeYaml ( data , `${ data . metadata . name } _get` , `${ dirPath } /${ k8sType } ` ) ;
95- } finally {
96- semaphore . release ( ) ;
97- }
98- } ) ) ;
99104 }
100105}
0 commit comments