forked from kubernetes/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.go
245 lines (205 loc) · 7.27 KB
/
common.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright 2017 The Kubernetes Authors.
//
// 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
//
// http://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 event
import (
"github.com/kubernetes/dashboard/src/app/backend/api"
"github.com/kubernetes/dashboard/src/app/backend/resource/common"
"github.com/kubernetes/dashboard/src/app/backend/resource/dataselect"
"k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
client "k8s.io/client-go/kubernetes"
)
// EmptyEventList is a empty list of events.
var EmptyEventList = &common.EventList{
Events: make([]common.Event, 0),
ListMeta: api.ListMeta{
TotalItems: 0,
},
}
// GetEvents gets events associated to resource with given name.
func GetEvents(client client.Interface, namespace, resourceName string) ([]v1.Event, error) {
fieldSelector, err := fields.ParseSelector("involvedObject.name" + "=" + resourceName)
if err != nil {
return nil, err
}
channels := &common.ResourceChannels{
EventList: common.GetEventListChannelWithOptions(
client,
common.NewSameNamespaceQuery(namespace),
metaV1.ListOptions{
LabelSelector: labels.Everything().String(),
FieldSelector: fieldSelector.String(),
},
1),
}
eventList := <-channels.EventList.List
if err := <-channels.EventList.Error; err != nil {
return nil, err
}
return FillEventsType(eventList.Items), nil
}
// GetPodsEvents gets events targeting given list of pods.
func GetPodsEvents(client client.Interface, namespace string, pods []v1.Pod) (
[]v1.Event, error) {
nsQuery := common.NewSameNamespaceQuery(namespace)
if namespace == v1.NamespaceAll {
nsQuery = common.NewNamespaceQuery([]string{})
}
channels := &common.ResourceChannels{
EventList: common.GetEventListChannel(client, nsQuery, 1),
}
eventList := <-channels.EventList.List
if err := <-channels.EventList.Error; err != nil {
return nil, err
}
events := filterEventsByPodsUID(eventList.Items, pods)
return events, nil
}
// GetPodEvents gets pods events associated to pod name and namespace
func GetPodEvents(client client.Interface, namespace, podName string) ([]v1.Event, error) {
channels := &common.ResourceChannels{
PodList: common.GetPodListChannel(client,
common.NewSameNamespaceQuery(namespace),
1),
EventList: common.GetEventListChannel(client, common.NewSameNamespaceQuery(namespace), 1),
}
podList := <-channels.PodList.List
if err := <-channels.PodList.Error; err != nil {
return nil, err
}
eventList := <-channels.EventList.List
if err := <-channels.EventList.Error; err != nil {
return nil, err
}
l := make([]v1.Pod, 0)
for _, pi := range podList.Items {
if pi.Name == podName {
l = append(l, pi)
}
}
events := filterEventsByPodsUID(eventList.Items, l)
return FillEventsType(events), nil
}
// GetNodeEvents gets events associated to node with given name.
func GetNodeEvents(client client.Interface, dsQuery *dataselect.DataSelectQuery, nodeName string) (*common.EventList, error) {
eventList := common.EventList{
Events: make([]common.Event, 0),
}
scheme := runtime.NewScheme()
groupVersion := schema.GroupVersion{Group: "", Version: "v1"}
scheme.AddKnownTypes(groupVersion, &v1.Node{})
mc := client.CoreV1().Nodes()
node, err := mc.Get(nodeName, metaV1.GetOptions{})
if err != nil {
return &eventList, err
}
events, err := client.CoreV1().Events(v1.NamespaceAll).Search(scheme, node)
if err != nil {
return &eventList, err
}
eventList = CreateEventList(FillEventsType(events.Items), dsQuery)
return &eventList, nil
}
// GetNamespaceEvents gets events associated to a namespace with given name.
func GetNamespaceEvents(client client.Interface, dsQuery *dataselect.DataSelectQuery, namespace string) (common.EventList, error) {
events, _ := client.CoreV1().Events(namespace).List(api.ListEverything)
return CreateEventList(FillEventsType(events.Items), dsQuery), nil
}
// Based on event Reason fills event Type in order to allow correct filtering by Type.
func FillEventsType(events []v1.Event) []v1.Event {
for i := range events {
// Fill in only events with empty type.
if len(events[i].Type) == 0 {
if isFailedReason(events[i].Reason, FailedReasonPartials...) {
events[i].Type = v1.EventTypeWarning
} else {
events[i].Type = v1.EventTypeNormal
}
}
}
return events
}
// ToEvent converts event api Event to Event model object.
func ToEvent(event v1.Event) common.Event {
result := common.Event{
ObjectMeta: api.NewObjectMeta(event.ObjectMeta),
TypeMeta: api.NewTypeMeta(api.ResourceKindEvent),
Message: event.Message,
SourceComponent: event.Source.Component,
SourceHost: event.Source.Host,
SubObject: event.InvolvedObject.FieldPath,
Count: event.Count,
FirstSeen: event.FirstTimestamp,
LastSeen: event.LastTimestamp,
Reason: event.Reason,
Type: event.Type,
}
return result
}
// GetResourceEvents gets events associated to specified resource.
func GetResourceEvents(client client.Interface, dsQuery *dataselect.DataSelectQuery, namespace, name string) (
*common.EventList, error) {
resourceEvents, err := GetEvents(client, namespace, name)
if err != nil {
return EmptyEventList, err
}
events := CreateEventList(resourceEvents, dsQuery)
return &events, nil
}
// CreateEventList converts array of api events to common EventList structure
func CreateEventList(events []v1.Event, dsQuery *dataselect.DataSelectQuery) common.EventList {
eventList := common.EventList{
Events: make([]common.Event, 0),
ListMeta: api.ListMeta{TotalItems: len(events)},
}
events = fromCells(dataselect.GenericDataSelect(toCells(events), dsQuery))
for _, event := range events {
eventDetail := ToEvent(event)
eventList.Events = append(eventList.Events, eventDetail)
}
return eventList
}
// The code below allows to perform complex data section on []api.Event
type EventCell v1.Event
func (self EventCell) GetProperty(name dataselect.PropertyName) dataselect.ComparableValue {
switch name {
case dataselect.NameProperty:
return dataselect.StdComparableString(self.ObjectMeta.Name)
case dataselect.CreationTimestampProperty:
return dataselect.StdComparableTime(self.ObjectMeta.CreationTimestamp.Time)
case dataselect.NamespaceProperty:
return dataselect.StdComparableString(self.ObjectMeta.Namespace)
default:
// if name is not supported then just return a constant dummy value, sort will have no effect.
return nil
}
}
func toCells(std []v1.Event) []dataselect.DataCell {
cells := make([]dataselect.DataCell, len(std))
for i := range std {
cells[i] = EventCell(std[i])
}
return cells
}
func fromCells(cells []dataselect.DataCell) []v1.Event {
std := make([]v1.Event, len(cells))
for i := range std {
std[i] = v1.Event(cells[i].(EventCell))
}
return std
}