@@ -21,7 +21,7 @@ import (
21
21
"fmt"
22
22
"io"
23
23
"os"
24
- "strings "
24
+ "time "
25
25
26
26
"github.com/spf13/cobra"
27
27
@@ -57,7 +57,7 @@ var logCommand = &cobra.Command{
57
57
return
58
58
}
59
59
60
- if err := doLog (args [0 ], kubeClientset , crdClientset ); err != nil {
60
+ if err := doLog (args [0 ], FollowLogs , kubeClientset , crdClientset ); err != nil {
61
61
fmt .Fprintf (os .Stderr , "failed to get driver logs of SparkApplication %s: %v\n " , args [0 ], err )
62
62
}
63
63
},
@@ -69,36 +69,76 @@ func init() {
69
69
logCommand .Flags ().BoolVarP (& FollowLogs , "follow" , "f" , false , "whether to stream the logs" )
70
70
}
71
71
72
- func doLog (name string , kubeClientset clientset. Interface , crdClientset crdclientset. Interface ) error {
73
- app , err := crdClientset . SparkoperatorV1beta2 (). SparkApplications ( Namespace ). Get ( context . TODO (), name , metav1. GetOptions {})
74
- if err != nil {
75
- return fmt . Errorf ( "failed to get SparkApplication %s: %v" , name , err )
76
- }
72
+ func doLog (
73
+ name string ,
74
+ followLogs bool ,
75
+ kubeClient clientset. Interface ,
76
+ crdClient crdclientset. Interface ) error {
77
77
78
+ timeout := 30 * time .Second
79
+
80
+ podNameChannel := getPodNameChannel (name , crdClient )
78
81
var podName string
79
- if ExecutorId < 0 {
80
- podName = app . Status . DriverInfo . PodName
81
- } else {
82
- podName = strings . NewReplacer ( "driver" , fmt . Sprintf ( "exec-%d" , ExecutorId )).
83
- Replace ( app . Status . DriverInfo . PodName )
82
+
83
+ select {
84
+ case podName = <- podNameChannel :
85
+ case <- time . After ( timeout ):
86
+ return fmt . Errorf ( "not found pod name" )
84
87
}
85
88
86
- if podName == "" {
87
- return fmt .Errorf ("unable to fetch logs as the name of the target pod is empty" )
89
+ waitLogsChannel := waitForLogsFromPodChannel (podName , kubeClient , crdClient )
90
+
91
+ select {
92
+ case <- waitLogsChannel :
93
+ case <- time .After (timeout ):
94
+ return fmt .Errorf ("timeout to fetch logs from pod \" %s\" " , podName )
88
95
}
89
96
90
- out := os .Stdout
91
- if FollowLogs {
92
- if err := streamLogs (out , kubeClientset , podName ); err != nil {
93
- return err
94
- }
97
+ if followLogs {
98
+ return streamLogs (os .Stdout , kubeClient , podName )
95
99
} else {
96
- if err := printLogs (out , kubeClientset , podName ); err != nil {
97
- return err
98
- }
100
+ return printLogs (os .Stdout , kubeClient , podName )
99
101
}
102
+ }
100
103
101
- return nil
104
+ func getPodNameChannel (
105
+ sparkApplicationName string ,
106
+ crdClient crdclientset.Interface ) chan string {
107
+
108
+ channel := make (chan string , 1 )
109
+ go func () {
110
+ for true {
111
+ app , _ := crdClient .SparkoperatorV1beta2 ().SparkApplications (Namespace ).Get (
112
+ context .TODO (),
113
+ sparkApplicationName ,
114
+ metav1.GetOptions {})
115
+
116
+ if app .Status .DriverInfo .PodName != "" {
117
+ channel <- app .Status .DriverInfo .PodName
118
+ break
119
+ }
120
+ }
121
+ }()
122
+ return channel
123
+ }
124
+
125
+ func waitForLogsFromPodChannel (
126
+ podName string ,
127
+ kubeClient clientset.Interface ,
128
+ crdClient crdclientset.Interface ) chan bool {
129
+
130
+ channel := make (chan bool , 1 )
131
+ go func () {
132
+ for true {
133
+ _ , err := kubeClient .CoreV1 ().Pods (Namespace ).GetLogs (podName , & apiv1.PodLogOptions {}).Do (context .TODO ()).Raw ()
134
+
135
+ if err == nil {
136
+ channel <- true
137
+ break
138
+ }
139
+ }
140
+ }()
141
+ return channel
102
142
}
103
143
104
144
// printLogs is a one time operation that prints the fetched logs of the given pod.
0 commit comments