forked from kubeflow/spark-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
166 lines (137 loc) · 4.18 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
Copyright 2017 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"context"
"fmt"
"io"
"os"
"time"
"github.com/spf13/cobra"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
crdclientset "github.com/kubeflow/spark-operator/pkg/client/clientset/versioned"
)
var ExecutorId int32
var FollowLogs bool
var logCommand = &cobra.Command{
Use: "log <name>",
Short: "log is a sub-command of sparkctl that fetches logs of a Spark application.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Fprintln(os.Stderr, "must specify a SparkApplication name")
return
}
kubeClientset, err := getKubeClient()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get Kubernetes client: %v\n", err)
return
}
crdClientset, err := getSparkApplicationClient()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get SparkApplication client: %v\n", err)
return
}
if err := doLog(args[0], FollowLogs, kubeClientset, crdClientset); err != nil {
fmt.Fprintf(os.Stderr, "failed to get driver logs of SparkApplication %s: %v\n", args[0], err)
}
},
}
func init() {
logCommand.Flags().Int32VarP(&ExecutorId, "executor", "e", -1,
"id of the executor to fetch logs for")
logCommand.Flags().BoolVarP(&FollowLogs, "follow", "f", false, "whether to stream the logs")
}
func doLog(
name string,
followLogs bool,
kubeClient clientset.Interface,
crdClient crdclientset.Interface) error {
timeout := 30 * time.Second
podNameChannel := getPodNameChannel(name, crdClient)
var podName string
select {
case podName = <-podNameChannel:
case <-time.After(timeout):
return fmt.Errorf("not found pod name")
}
waitLogsChannel := waitForLogsFromPodChannel(podName, kubeClient, crdClient)
select {
case <-waitLogsChannel:
case <-time.After(timeout):
return fmt.Errorf("timeout to fetch logs from pod \"%s\"", podName)
}
if followLogs {
return streamLogs(os.Stdout, kubeClient, podName)
} else {
return printLogs(os.Stdout, kubeClient, podName)
}
}
func getPodNameChannel(
sparkApplicationName string,
crdClient crdclientset.Interface) chan string {
channel := make(chan string, 1)
go func() {
for true {
app, _ := crdClient.SparkoperatorV1beta2().SparkApplications(Namespace).Get(
context.TODO(),
sparkApplicationName,
metav1.GetOptions{})
if app.Status.DriverInfo.PodName != "" {
channel <- app.Status.DriverInfo.PodName
break
}
}
}()
return channel
}
func waitForLogsFromPodChannel(
podName string,
kubeClient clientset.Interface,
crdClient crdclientset.Interface) chan bool {
channel := make(chan bool, 1)
go func() {
for true {
_, err := kubeClient.CoreV1().Pods(Namespace).GetLogs(podName, &apiv1.PodLogOptions{}).Do(context.TODO()).Raw()
if err == nil {
channel <- true
break
}
}
}()
return channel
}
// printLogs is a one time operation that prints the fetched logs of the given pod.
func printLogs(out io.Writer, kubeClientset clientset.Interface, podName string) error {
rawLogs, err := kubeClientset.CoreV1().Pods(Namespace).GetLogs(podName, &apiv1.PodLogOptions{}).Do(context.TODO()).Raw()
if err != nil {
return err
}
fmt.Fprintln(out, string(rawLogs))
return nil
}
// streamLogs streams the logs of the given pod until there are no more logs available.
func streamLogs(out io.Writer, kubeClientset clientset.Interface, podName string) error {
request := kubeClientset.CoreV1().Pods(Namespace).GetLogs(podName, &apiv1.PodLogOptions{Follow: true})
reader, err := request.Stream(context.TODO())
if err != nil {
return err
}
defer reader.Close()
if _, err := io.Copy(out, reader); err != nil {
return err
}
return nil
}