Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix field selector generation #2701

Merged
merged 11 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 53 additions & 17 deletions backend/service/k8s/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package k8s

import (
"context"
"sort"
"strings"

"github.com/iancoleman/strcase"
Expand Down Expand Up @@ -34,6 +35,26 @@ func (s *svc) ListEvents(ctx context.Context, clientset, cluster, namespace, obj
return events, nil
}

// For ease of use, we can sort the events in reverse chronological order
func sortEventsByTime(events []*k8sapiv1.Event) {
sort.Slice(events, func(i, j int) bool {
return events[i].CreationTimeMillis > events[j].CreationTimeMillis
})
}

func convertTypeStringToEnum(str string) k8sapiv1.EventType {
switch str {
case "Normal":
return k8sapiv1.EventType_NORMAL
case "Warning":
return k8sapiv1.EventType_WARNING
case "Error":
return k8sapiv1.EventType_ERROR
default:
return k8sapiv1.EventType_TYPE_UNSPECIFIED
}
}

func ProtoForEvent(cluster string, k8sEvent *corev1.Event) *k8sapiv1.Event {
clusterName := GetKubeClusterName(k8sEvent)
if clusterName == "" {
Expand All @@ -53,32 +74,30 @@ func ProtoForEvent(cluster string, k8sEvent *corev1.Event) *k8sapiv1.Event {
InvolvedObjectName: k8sEvent.InvolvedObject.Name,
Kind: protoForObjectKind(k8sEvent.InvolvedObject.Kind),
CreationTimeMillis: k8sEvent.GetObjectMeta().GetCreationTimestamp().UnixMilli(),
Type: convertTypeStringToEnum(k8sEvent.Type),
}
}

// Note in the case of a blank string being returned, that means there is no field selector,
// and all events will be returned.
func convertTypesToFieldSelector(types []k8sapiv1.EventType) string {
fs := ""
// Note that chaining field selectors only results in AND not OR, thus to have multiples
// we must have multiple field selectors.
// See https://github.com/kubernetes/kubernetes/issues/32946 for more info
func convertTypesToFieldSelectors(types []k8sapiv1.EventType) []string {
fs := []string{}
setOfTypes := make(map[k8sapiv1.EventType]bool)
for _, t := range types {
setOfTypes[t] = true
}

if setOfTypes[k8sapiv1.EventType_NORMAL] {
fs += "type=Normal"
fs = append(fs, "type=Normal")
}
if setOfTypes[k8sapiv1.EventType_WARNING] {
if fs != "" {
fs += ","
}
fs += "type=Warning"
fs = append(fs, "type=Warning")
}
if setOfTypes[k8sapiv1.EventType_ERROR] {
if fs != "" {
fs += ","
}
fs += "type=Error"
fs = append(fs, "type=Error")
}

return fs
Expand All @@ -90,17 +109,34 @@ func (s *svc) ListNamespaceEvents(ctx context.Context, clientset, cluster, names
return nil, err
}

totalEventList := []corev1.Event{}
fs := convertTypesToFieldSelectors(types)
// Note if field selector is blank it will return everything
eventList, err := cs.CoreV1().Events(cs.Namespace()).List(ctx, metav1.ListOptions{FieldSelector: convertTypesToFieldSelector(types)})
if err != nil {
return nil, err
if len(fs) == 0 {
eventList, err := cs.CoreV1().Events(cs.Namespace()).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
totalEventList = append(totalEventList, eventList.Items...)
}
for _, f := range fs {
smonero marked this conversation as resolved.
Show resolved Hide resolved
eventList, err := cs.CoreV1().Events(cs.Namespace()).List(ctx, metav1.ListOptions{FieldSelector: f})
smonero marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
totalEventList = append(totalEventList, eventList.Items...)
}

// in the future, could also potentially return full event object rather than the subset in ProtoForEvent
// in the future, could also potentially return full event object rather than the subset in
// ProtoForEvent()
var events []*k8sapiv1.Event
for i := range eventList.Items {
events = append(events, ProtoForEvent(cs.Cluster(), &eventList.Items[i]))
for _, ev := range totalEventList {
ev := ev
events = append(events, ProtoForEvent(cs.Cluster(), &ev))
}
// Sort in reverse chronological order (by CreationTimeMillis),
// this is needed because we might have multiple types of events
sortEventsByTime(events)

return events, nil
}
Expand Down
18 changes: 9 additions & 9 deletions backend/service/k8s/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,43 +82,43 @@ func TestListEvents(t *testing.T) {
}
}

func TestConvertTypesToFieldSelector(t *testing.T) {
func TestConvertTypesToFieldSelectors(t *testing.T) {
testCases := []struct {
name string
types []k8sapiv1.EventType
expected string
expected []string
}{
{
name: "empty",
types: []k8sapiv1.EventType{},
expected: "",
expected: []string{},
},
{
name: "single",
types: []k8sapiv1.EventType{k8sapiv1.EventType_WARNING},
expected: "type=Warning",
expected: []string{"type=Warning"},
},
{
name: "multiple",
types: []k8sapiv1.EventType{k8sapiv1.EventType_WARNING, k8sapiv1.EventType_NORMAL},
expected: "type=Normal,type=Warning",
expected: []string{"type=Normal", "type=Warning"},
},
{
name: "duplicate",
types: []k8sapiv1.EventType{k8sapiv1.EventType_WARNING, k8sapiv1.EventType_WARNING},
expected: "type=Warning",
expected: []string{"type=Warning"},
},
{
name: "all",
types: []k8sapiv1.EventType{k8sapiv1.EventType_WARNING, k8sapiv1.EventType_NORMAL, k8sapiv1.EventType_ERROR},
expected: "type=Normal,type=Warning,type=Error",
expected: []string{"type=Error", "type=Normal", "type=Warning"},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := convertTypesToFieldSelector(tc.types)
assert.Equal(t, tc.expected, actual)
actual := convertTypesToFieldSelectors(tc.types)
assert.ElementsMatch(t, tc.expected, actual)
})
}
}