Skip to content

Commit 127183e

Browse files
committed
Merge pull request #389 from floreks/events-fix
Fixed event warning icon not displayed in events table
2 parents 8995ee4 + 217fa67 commit 127183e

File tree

7 files changed

+91
-115
lines changed

7 files changed

+91
-115
lines changed

src/app/backend/events.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Events struct {
3535

3636
// Single event representation.
3737
type Event struct {
38-
// Event message.
38+
// A human-readable description of the status of related object.
3939
Message string `json:"message"`
4040

4141
// Component from which the event is generated.
@@ -59,7 +59,8 @@ type Event struct {
5959
// The time at which the most recent occurrence of this event was recorded.
6060
LastSeen unversioned.Time `json:"lastSeen"`
6161

62-
// Reason why this event was generated.
62+
// Short, machine understandable string that gives the reason
63+
// for this event being generated.
6364
Reason string `json:"reason"`
6465

6566
// Event type (at the moment only normal and warning are supported).
@@ -78,19 +79,23 @@ func GetEvents(client *client.Client, namespace, replicationControllerName strin
7879
return nil, err
7980
}
8081

81-
events := AppendEvents(rsEvents, Events{
82-
Namespace: namespace,
83-
Events: make([]Event, 0),
84-
})
85-
8682
// Get events for pods in replication controller.
8783
podEvents, err := GetReplicationControllerPodsEvents(client, namespace, replicationControllerName)
8884

8985
if err != nil {
9086
return nil, err
9187
}
9288

93-
events = AppendEvents(podEvents, events)
89+
apiEvents := append(rsEvents, podEvents...)
90+
91+
if !isTypeFilled(apiEvents) {
92+
apiEvents = fillEventsType(apiEvents)
93+
}
94+
95+
events := AppendEvents(apiEvents, Events{
96+
Namespace: namespace,
97+
Events: make([]Event, 0),
98+
})
9499

95100
log.Printf("Found %d events related to %s replication controller in %s namespace", len(events.Events),
96101
replicationControllerName, namespace)

src/app/backend/eventscommon.go

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,11 @@ import (
2323

2424
// Partial string to correctly filter warning events.
2525
// Has to be lower case for correct case insensitive comparison.
26-
const FAILED_REASON_PARTIAL = "failed"
27-
28-
// Contains basic information about event related to a pod
29-
type PodEvent struct {
30-
// Short, machine understandable string that gives the reason
31-
// for this event being generated.
32-
Reason string `json:"reason"`
33-
34-
// A human-readable description of the status of related pod.
35-
Message string `json:"message"`
36-
}
26+
const FailedReasonPartial = "failed"
3727

3828
// Returns warning pod events based on given list of pods.
3929
// TODO(floreks) : Import and use Set instead of custom function to get rid of duplicates
40-
func GetPodsEventWarnings(client client.Interface, pods []api.Pod) (result []PodEvent, err error) {
30+
func GetPodsEventWarnings(client client.Interface, pods []api.Pod) (result []Event, err error) {
4131
for _, pod := range pods {
4232
if !isRunningOrSucceeded(pod) {
4333
log.Printf("Getting warning events from pod: %s", pod.Name)
@@ -56,20 +46,21 @@ func GetPodsEventWarnings(client client.Interface, pods []api.Pod) (result []Pod
5646

5747
// Returns list of Pod Event model objects based on kubernetes API event list object
5848
// Event list object is filtered to get only warning events.
59-
func getPodsEventWarnings(eventList *api.EventList) []PodEvent {
60-
result := make([]PodEvent, 0)
49+
func getPodsEventWarnings(eventList *api.EventList) []Event {
50+
result := make([]Event, 0)
6151

6252
var events []api.Event
63-
if isTypeFilled(eventList.Items) {
64-
events = filterEventsByType(eventList.Items, api.EventTypeWarning)
65-
} else {
66-
events = filterEventsByReason(eventList.Items, FAILED_REASON_PARTIAL)
53+
if !isTypeFilled(eventList.Items) {
54+
eventList.Items = fillEventsType(eventList.Items)
6755
}
6856

57+
events = filterEventsByType(eventList.Items, api.EventTypeWarning)
58+
6959
for _, event := range events {
70-
result = append(result, PodEvent{
60+
result = append(result, Event{
7161
Message: event.Message,
7262
Reason: event.Reason,
63+
Type: event.Type,
7364
})
7465
}
7566

@@ -93,23 +84,6 @@ func filterEventsByType(events []api.Event, eventType string) []api.Event {
9384
return result
9485
}
9586

96-
// Filters kubernetes API event objects based on reason property.
97-
// Empty string will return all events.
98-
func filterEventsByReason(events []api.Event, partial string) []api.Event {
99-
if len(partial) == 0 || len(events) == 0 {
100-
return events
101-
}
102-
103-
result := make([]api.Event, 0)
104-
for _, event := range events {
105-
if strings.Contains(strings.ToLower(event.Reason), partial) {
106-
result = append(result, event)
107-
}
108-
}
109-
110-
return result
111-
}
112-
11387
// Returns true if all given events type is filled, false otherwise.
11488
// This is needed as some older versions of kubernetes do not have Type property filled.
11589
func isTypeFilled(events []api.Event) bool {
@@ -126,10 +100,23 @@ func isTypeFilled(events []api.Event) bool {
126100
return true
127101
}
128102

103+
// Based on event Reason fills event Type in order to allow correct filtering by Type.
104+
func fillEventsType(events []api.Event) []api.Event {
105+
for i, _ := range events {
106+
if strings.Contains(strings.ToLower(events[i].Reason), FailedReasonPartial) {
107+
events[i].Type = api.EventTypeWarning
108+
} else {
109+
events[i].Type = api.EventTypeNormal
110+
}
111+
}
112+
113+
return events
114+
}
115+
129116
// Removes duplicate strings from the slice
130-
func removeDuplicates(slice []PodEvent) []PodEvent {
117+
func removeDuplicates(slice []Event) []Event {
131118
visited := make(map[string]bool, 0)
132-
result := make([]PodEvent, 0)
119+
result := make([]Event, 0)
133120

134121
for _, elem := range slice {
135122
if !visited[elem.Reason] {

src/app/backend/replicationcontrollercommon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type ReplicationControllerPodInfo struct {
4545
Failed int `json:"failed"`
4646

4747
// Unique warning messages related to pods in this Replication Controller.
48-
Warnings []PodEvent `json:"warnings"`
48+
Warnings []Event `json:"warnings"`
4949
}
5050

5151
// Returns structure containing ReplicationController and Pods for the given replication controller.

src/app/backend/replicationcontrollerlist.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
// Callback function in order to get the pod status errors
28-
type GetPodsEventWarningsFunc func(pods []api.Pod) ([]PodEvent, error)
28+
type GetPodsEventWarningsFunc func(pods []api.Pod) ([]Event, error)
2929

3030
// Callback function in order to get node by name.
3131
type GetNodeFunc func(nodeName string) (*api.Node, error)
@@ -51,7 +51,7 @@ type ReplicationController struct {
5151
// Label of this Replication Controller.
5252
Labels map[string]string `json:"labels"`
5353

54-
// Aggregate information about pods belonging to this repolica set.
54+
// Aggregate information about pods belonging to this Replication Controller.
5555
Pods ReplicationControllerPodInfo `json:"pods"`
5656

5757
// Container images of the Replication Controller.
@@ -97,7 +97,7 @@ func GetReplicationControllerList(client *client.Client) (*ReplicationController
9797
// Anonymous callback function to get pods warnings.
9898
// Function fulfils GetPodsEventWarningsFunc type contract.
9999
// Based on list of api pods returns list of pod related warning events
100-
getPodsEventWarningsFn := func(pods []api.Pod) ([]PodEvent, error) {
100+
getPodsEventWarningsFn := func(pods []api.Pod) ([]Event, error) {
101101
errors, err := GetPodsEventWarnings(client, pods)
102102

103103
if err != nil {

src/app/externs/backendapi.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,14 @@ backendApi.Event;
106106
*/
107107
backendApi.ReplicationControllerList;
108108

109-
/**
110-
* @typedef {{
111-
* reason: string,
112-
* message: string
113-
* }}
114-
*/
115-
backendApi.PodEvent;
116-
117109
/**
118110
* @typedef {{
119111
* current: number,
120112
* desired: number,
121113
* running: number,
122114
* pending: number,
123115
* failed: number,
124-
* warnings: !Array<!backendApi.PodEvent>
116+
* warnings: !Array<!backendApi.Event>
125117
* }}
126118
*/
127119
backendApi.ReplicationControllerPodInfo;

src/test/backend/eventscommon_test.go

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ func TestGetPodsEventWarningsApi(t *testing.T) {
6868
func TestGetPodsEventWarnings(t *testing.T) {
6969
cases := []struct {
7070
events *api.EventList
71-
expected []PodEvent
71+
expected []Event
7272
}{
73-
{&api.EventList{Items: nil}, []PodEvent{}},
73+
{&api.EventList{Items: nil}, []Event{}},
7474
{
7575
&api.EventList{
7676
Items: []api.Event{
@@ -81,10 +81,11 @@ func TestGetPodsEventWarnings(t *testing.T) {
8181
},
8282
},
8383
},
84-
[]PodEvent{
84+
[]Event{
8585
{
8686
Message: "msg",
8787
Reason: "reason",
88+
Type: api.EventTypeWarning,
8889
},
8990
},
9091
},
@@ -97,10 +98,11 @@ func TestGetPodsEventWarnings(t *testing.T) {
9798
},
9899
},
99100
},
100-
[]PodEvent{
101+
[]Event{
101102
{
102103
Message: "msg",
103104
Reason: "failed",
105+
Type: api.EventTypeWarning,
104106
},
105107
},
106108
},
@@ -113,7 +115,7 @@ func TestGetPodsEventWarnings(t *testing.T) {
113115
},
114116
},
115117
},
116-
[]PodEvent{},
118+
[]Event{},
117119
},
118120
}
119121

@@ -171,38 +173,38 @@ func TestFilterEventsByType(t *testing.T) {
171173

172174
func TestRemoveDuplicates(t *testing.T) {
173175
cases := []struct {
174-
slice []PodEvent
175-
expected []PodEvent
176+
slice []Event
177+
expected []Event
176178
}{
177-
{nil, []PodEvent{}},
179+
{nil, []Event{}},
178180
{
179-
[]PodEvent{
181+
[]Event{
180182
{Reason: "test"},
181183
{Reason: "test2"},
182184
{Reason: "test"},
183185
},
184-
[]PodEvent{
186+
[]Event{
185187
{Reason: "test"},
186188
{Reason: "test2"},
187189
},
188190
},
189191
{
190-
[]PodEvent{
192+
[]Event{
191193
{Reason: "test"},
192194
{Reason: "test"},
193195
{Reason: "test"},
194196
},
195-
[]PodEvent{
197+
[]Event{
196198
{Reason: "test"},
197199
},
198200
},
199201
{
200-
[]PodEvent{
202+
[]Event{
201203
{Reason: "test"},
202204
{Reason: "test2"},
203205
{Reason: "test3"},
204206
},
205-
[]PodEvent{
207+
[]Event{
206208
{Reason: "test"},
207209
{Reason: "test2"},
208210
{Reason: "test3"},
@@ -267,44 +269,6 @@ func TestIsRunningOrSucceeded(t *testing.T) {
267269
}
268270
}
269271

270-
func TestFilterEventsByReason(t *testing.T) {
271-
cases := []struct {
272-
events []api.Event
273-
partial string
274-
expected []api.Event
275-
}{
276-
{nil, "", nil},
277-
{nil, "failed", nil},
278-
{
279-
[]api.Event{
280-
{
281-
Message: "msg",
282-
Reason: "reason",
283-
},
284-
{
285-
Message: "msg-2",
286-
Reason: "failed",
287-
},
288-
},
289-
"failed",
290-
[]api.Event{
291-
{
292-
Message: "msg-2",
293-
Reason: "failed",
294-
},
295-
},
296-
},
297-
}
298-
299-
for _, c := range cases {
300-
actual := filterEventsByReason(c.events, c.partial)
301-
if !reflect.DeepEqual(actual, c.expected) {
302-
t.Errorf("filterEventsByReason(%#v, %#v) == \n%#v\nexpected \n%#v\n",
303-
c.events, c.partial, actual, c.expected)
304-
}
305-
}
306-
}
307-
308272
func TestIsTypeFilled(t *testing.T) {
309273
cases := []struct {
310274
events []api.Event
@@ -339,3 +303,31 @@ func TestIsTypeFilled(t *testing.T) {
339303
}
340304
}
341305
}
306+
307+
func TestFillEventsType(t *testing.T) {
308+
cases := []struct {
309+
events []api.Event
310+
expected []api.Event
311+
}{
312+
{nil, nil},
313+
{[]api.Event{}, []api.Event{}},
314+
{
315+
[]api.Event{
316+
{Reason: "failed"},
317+
{Reason: "test"},
318+
},
319+
[]api.Event{
320+
{Reason: "failed", Type: api.EventTypeWarning},
321+
{Reason: "test", Type: api.EventTypeNormal},
322+
},
323+
},
324+
}
325+
326+
for _, c := range cases {
327+
actual := fillEventsType(c.events)
328+
if !reflect.DeepEqual(actual, c.expected) {
329+
t.Errorf("fillEventsType(%#v) == \n%#v\nexpected \n%#v\n",
330+
c.events, actual, c.expected)
331+
}
332+
}
333+
}

0 commit comments

Comments
 (0)