/
event.go
180 lines (155 loc) · 5.21 KB
/
event.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Copyright 2018 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"
"os"
"strings"
"time"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
clientWatch "k8s.io/client-go/tools/watch"
"k8s.io/kubernetes/pkg/util/interrupt"
crdclientset "github.com/GoogleCloudPlatform/spark-on-k8s-operator/pkg/client/clientset/versioned"
)
var FollowEvents bool
var eventCommand = &cobra.Command{
Use: "event <name>",
Short: "Shows SparkApplication events",
Long: `Shows events associated with SparkApplication of a given name`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Fprintln(os.Stderr, "must specify a SparkApplication name")
return
}
crdClientset, err := getSparkApplicationClient()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get SparkApplication client: %v\n", err)
return
}
kubeClientset, err := getKubeClient()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get KubeClient: %v\n", err)
return
}
if err := doShowEvents(args[0], crdClientset, kubeClientset); err != nil {
fmt.Fprintf(os.Stderr, "failed to check events of SparkApplication %s: %v\n", args[0], err)
}
},
}
func init() {
eventCommand.Flags().BoolVarP(&FollowEvents, "follow", "f", false,
"whether to stream the events for the specified SparkApplication name")
}
func doShowEvents(name string, crdClientset crdclientset.Interface, kubeClientset kubernetes.Interface) error {
app, err := getSparkApplication(name, crdClientset)
if err != nil {
return fmt.Errorf("failed to get SparkApplication %s: %v", name, err)
}
app.Kind = "SparkApplication"
eventsInterface := kubeClientset.CoreV1().Events(Namespace)
if FollowEvents {
// watch for all events for this specific SparkApplication name
selector := eventsInterface.GetFieldSelector(&app.Name, &app.Namespace, &app.Kind, nil)
options := metav1.ListOptions{FieldSelector: selector.String(), Watch: true}
events, err := eventsInterface.Watch(options)
if err != nil {
return err
}
if err := streamEvents(events, app.CreationTimestamp.Unix()); err != nil {
return err
}
} else {
// print only events for current SparkApplication UID
stringUID := string(app.UID)
selector := eventsInterface.GetFieldSelector(&app.Name, &app.Namespace, &app.Kind, &stringUID)
options := metav1.ListOptions{FieldSelector: selector.String()}
events, err := eventsInterface.List(options)
if err != nil {
return err
}
if err := printEvents(events); err != nil {
return err
}
}
return nil
}
func prepareNewTable() *tablewriter.Table {
table := tablewriter.NewWriter(os.Stdout)
table.SetColMinWidth(0, 10)
table.SetColMinWidth(1, 6)
table.SetColMinWidth(2, 50)
return table
}
func prepareEventsHeader(table *tablewriter.Table) *tablewriter.Table {
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetHeader([]string{"Type", "Age", "Message"})
table.SetHeaderLine(true)
return table
}
func printEvents(events *v1.EventList) error {
// Render all event rows
table := prepareNewTable()
table = prepareEventsHeader(table)
for _, event := range events.Items {
table.Append([]string{
event.Type,
getSinceTime(event.LastTimestamp),
strings.TrimSpace(event.Message),
})
}
table.Render()
return nil
}
func streamEvents(events watch.Interface, streamSince int64) error {
// Render just table header, without a additional header line as we stream
table := prepareNewTable()
table = prepareEventsHeader(table)
table.SetHeaderLine(false)
table.Render()
// Set 10 minutes inactivity timeout
watchExpire := time.Duration(10 * time.Minute)
intr := interrupt.New(nil, events.Stop)
return intr.Run(func() error {
// Start rendering contents of the table without table header as it is already printed
table = prepareNewTable()
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
ctx := context.TODO()
ctx, _ = context.WithTimeout(ctx, watchExpire)
_, err := clientWatch.UntilWithoutRetry(ctx, events, func(ev watch.Event) (bool, error) {
if event, isEvent := ev.Object.(*v1.Event); isEvent {
// Ensure to display events which are newer than last creation time of SparkApplication
// for this specific application name
if streamSince <= event.CreationTimestamp.Unix() {
// Render each row separately
table.ClearRows()
table.Append([]string{
event.Type,
getSinceTime(event.LastTimestamp),
strings.TrimSpace(event.Message),
})
table.Render()
}
} else {
fmt.Printf("info: %v", ev.Object)
}
return false, nil
})
return err
})
}