@@ -146,8 +146,7 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
146146 }
147147 defer os .RemoveAll (bundlePath )
148148
149- versionFilename , err := writeVersionFile (bundlePath )
150- if err != nil {
149+ if err = writeVersionFile (bundlePath ); err != nil {
151150 return "" , errors .Wrap (err , "write version file" )
152151 }
153152
@@ -158,8 +157,6 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
158157 desiredCollectors = ensureCollectorInList (desiredCollectors , troubleshootv1beta1.Collect {ClusterInfo : & troubleshootv1beta1.ClusterInfo {}})
159158 desiredCollectors = ensureCollectorInList (desiredCollectors , troubleshootv1beta1.Collect {ClusterResources : & troubleshootv1beta1.ClusterResources {}})
160159
161- collectorDirs := []string {}
162-
163160 config , err := KubernetesConfigFlags .ToRESTConfig ()
164161 if err != nil {
165162 return "" , errors .Wrap (err , "failed to convert kube flags to rest config" )
@@ -173,107 +170,77 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
173170 ClientConfig : config ,
174171 }
175172
173+ progressChan <- collector .GetDisplayName ()
174+
176175 result , err := collector .RunCollectorSync ()
177176 if err != nil {
178177 progressChan <- fmt .Errorf ("failed to run collector %q: %v" , collector .GetDisplayName (), err )
179178 continue
180179 }
181180
182- newCollectorDirs , err : = parseAndSaveCollectorOutput (string (result ), bundlePath )
181+ err = parseAndSaveCollectorOutput (string (result ), bundlePath )
183182 if err != nil {
184183 progressChan <- fmt .Errorf ("failed to parse collector spec %q: %v" , collector .GetDisplayName (), err )
185184 continue
186185 }
187-
188- if len (newCollectorDirs ) == 0 {
189- continue
190- }
191-
192- // TODO: better progress....
193- for _ , d := range newCollectorDirs {
194- progressChan <- d
195- }
196- collectorDirs = append (collectorDirs , newCollectorDirs ... )
197- }
198-
199- tarGz := archiver.TarGz {
200- Tar : & archiver.Tar {
201- ImplicitTopLevelFolder : false ,
202- },
203- }
204-
205- // version file should be first in tar archive for quick extraction
206- paths := []string {
207- versionFilename ,
208- }
209- for _ , collectorDir := range collectorDirs {
210- paths = append (paths , collectorDir )
211186 }
212187
213188 filename , err := findFileName ("support-bundle" , "tar.gz" )
214189 if err != nil {
215190 return "" , errors .Wrap (err , "find file name" )
216191 }
217192
218- if err := tarGz . Archive ( paths , filename ); err != nil {
219- return "" , errors .Wrap (err , "create archive " )
193+ if err := tarSupportBundleDir ( bundlePath , filename ); err != nil {
194+ return "" , errors .Wrap (err , "create bundle file " )
220195 }
221196
222197 return filename , nil
223198}
224199
225- func parseAndSaveCollectorOutput (output string , bundlePath string ) ([]string , error ) {
226- rootDirs := make (map [string ]bool )
227-
200+ func parseAndSaveCollectorOutput (output string , bundlePath string ) error {
228201 input := make (map [string ]interface {})
229202 if err := json .Unmarshal ([]byte (output ), & input ); err != nil {
230- return nil , errors .Wrap (err , "unmarshal output" )
203+ return errors .Wrap (err , "unmarshal output" )
231204 }
232205
233206 for filename , maybeContents := range input {
234207 fileDir , fileName := filepath .Split (filename )
235208 outPath := filepath .Join (bundlePath , fileDir )
236- rootDirs [outPath ] = true
237209
238210 if err := os .MkdirAll (outPath , 0777 ); err != nil {
239- return nil , errors .Wrap (err , "create output file" )
211+ return errors .Wrap (err , "create output file" )
240212 }
241213
242214 switch maybeContents .(type ) {
243215 case string :
244216 decoded , err := base64 .StdEncoding .DecodeString (maybeContents .(string ))
245217 if err != nil {
246- return nil , errors .Wrap (err , "decode collector output" )
218+ return errors .Wrap (err , "decode collector output" )
247219 }
248220
249221 if err := writeFile (filepath .Join (outPath , fileName ), decoded ); err != nil {
250- return nil , errors .Wrap (err , "write collector output" )
222+ return errors .Wrap (err , "write collector output" )
251223 }
252224
253225 case map [string ]interface {}:
254226 for k , v := range maybeContents .(map [string ]interface {}) {
255227 s , _ := filepath .Split (filepath .Join (outPath , fileName , k ))
256228 if err := os .MkdirAll (s , 0777 ); err != nil {
257- return nil , errors .Wrap (err , "write output directories" )
229+ return errors .Wrap (err , "write output directories" )
258230 }
259231
260232 decoded , err := base64 .StdEncoding .DecodeString (v .(string ))
261233 if err != nil {
262- return nil , errors .Wrap (err , "decode output" )
234+ return errors .Wrap (err , "decode output" )
263235 }
264236 if err := writeFile (filepath .Join (outPath , fileName , k ), decoded ); err != nil {
265- return nil , errors .Wrap (err , "write output" )
237+ return errors .Wrap (err , "write output" )
266238 }
267239 }
268240 }
269241 }
270242
271- dirs := make ([]string , 0 )
272- for dir := range rootDirs {
273- dirs = append (dirs , dir )
274- }
275-
276- return dirs , nil
243+ return nil
277244}
278245
279246func uploadSupportBundle (r * troubleshootv1beta1.ResultRequest , archivePath string ) error {
@@ -339,3 +306,32 @@ func callbackSupportBundleAPI(r *troubleshootv1beta1.ResultRequest, archivePath
339306
340307 return nil
341308}
309+
310+ func tarSupportBundleDir (inputDir , outputFilename string ) error {
311+ tarGz := archiver.TarGz {
312+ Tar : & archiver.Tar {
313+ ImplicitTopLevelFolder : false ,
314+ },
315+ }
316+
317+ paths := []string {
318+ filepath .Join (inputDir , VersionFilename ), // version file should be first in tar archive for quick extraction
319+ }
320+
321+ topLevelFiles , err := ioutil .ReadDir (inputDir )
322+ if err != nil {
323+ return errors .Wrap (err , "list bundle directory contents" )
324+ }
325+ for _ , f := range topLevelFiles {
326+ if f .Name () == VersionFilename {
327+ continue
328+ }
329+ paths = append (paths , filepath .Join (inputDir , f .Name ()))
330+ }
331+
332+ if err := tarGz .Archive (paths , outputFilename ); err != nil {
333+ return errors .Wrap (err , "create archive" )
334+ }
335+
336+ return nil
337+ }
0 commit comments