@@ -2,6 +2,7 @@ package collect
22
33import (
44 "encoding/json"
5+ "errors"
56 "fmt"
67 "time"
78
@@ -32,46 +33,69 @@ func Run(runCollector *troubleshootv1beta1.Run, redact bool) error {
3233 return err
3334 }
3435
35- runOutput := & RunOutput {
36- PodLogs : make (map [string ][]byte ),
36+ defer func () {
37+ if err := client .CoreV1 ().Pods (pod .Namespace ).Delete (pod .Name , & metav1.DeleteOptions {}); err != nil {
38+ fmt .Printf ("Failed to delete pod %s: %v\n " , pod .Name , err )
39+ }
40+ }()
41+
42+ if runCollector .Timeout == "" {
43+ return runWithoutTimeout (pod , runCollector , redact )
3744 }
3845
39- now := time .Now ()
40- then := now .Add (time .Duration (20 * time .Second ))
46+ timeout , err := time .ParseDuration (runCollector .Timeout )
47+ if err != nil {
48+ return err
49+ }
4150
42- if runCollector .Timeout != "" {
43- parsedDuration , err := time .ParseDuration (runCollector .Timeout )
44- if err != nil {
45- fmt .Printf ("unable to parse time duration %s\n " , runCollector .Timeout )
46- } else {
47- then = now .Add (parsedDuration )
48- }
51+ runChan := make (chan error , 1 )
52+ go func () {
53+ runChan <- runWithoutTimeout (pod , runCollector , redact )
54+ }()
55+
56+ select {
57+ case <- time .After (timeout ):
58+ return errors .New ("timeout" )
59+ case err := <- runChan :
60+ return err
61+ }
62+ }
63+
64+ func runWithoutTimeout (pod * corev1.Pod , runCollector * troubleshootv1beta1.Run , redact bool ) error {
65+ cfg , err := config .GetConfig ()
66+ if err != nil {
67+ return err
68+ }
69+
70+ client , err := kubernetes .NewForConfig (cfg )
71+ if err != nil {
72+ return err
4973 }
5074
5175 for {
52- if time .Now ().After (then ) {
76+ status , err := client .CoreV1 ().Pods (pod .Namespace ).Get (pod .Name , metav1.GetOptions {})
77+ if err != nil {
78+ return err
79+ }
80+ if status .Status .Phase == "Running" {
5381 break
5482 }
83+ time .Sleep (time .Second * 1 )
84+ }
5585
56- time .Sleep (time .Second )
86+ runOutput := & RunOutput {
87+ PodLogs : make (map [string ][]byte ),
5788 }
5889
5990 limits := troubleshootv1beta1.LogLimits {
6091 MaxLines : 10000 ,
6192 }
62- podLogs , err := getPodLogs (client , * pod , & limits )
63- if err != nil {
64- return err
65- }
93+ podLogs , err := getPodLogs (client , * pod , & limits , true )
6694
6795 for k , v := range podLogs {
6896 runOutput .PodLogs [k ] = v
6997 }
7098
71- if err := client .CoreV1 ().Pods (pod .Namespace ).Delete (pod .Name , & metav1.DeleteOptions {}); err != nil {
72- return err
73- }
74-
7599 if redact {
76100 runOutput , err = runOutput .Redact ()
77101 if err != nil {
0 commit comments