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

openshift-machine-api warning events gatherer #658

Merged
merged 22 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions pkg/gatherers/clusterconfig/clusterconfig_gatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ var gatheringFunctions = map[string]gathererFuncPtr{
"support_secret": (*Gatherer).GatherSupportSecret,
"active_alerts": (*Gatherer).GatherActiveAlerts,
"ceph_cluster": (*Gatherer).GatherCephCluster,
"openshift_machine_api_events": (*Gatherer).GatherOpenshiftMachineApiEvents,
}

func New(
Expand Down
75 changes: 75 additions & 0 deletions pkg/gatherers/clusterconfig/events_filtering.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package clusterconfig

import (
"sort"
"time"

v1 "k8s.io/api/core/v1"
)

//TODO: Simplify if logic
//filterEvents() returns events that occoured since last interval
func filterEvents(interval time.Duration, events *v1.EventList, Type string) v1.EventList {
oldestEventTime := time.Now().Add(-interval)
var filteredEvents = v1.EventList{}
rhrmo marked this conversation as resolved.
Show resolved Hide resolved
switch Type {
case "Warning":
for i := range events.Items {
if events.Items[i].Type != "Normal" {
// if LastTimestamp is zero then try to check the event series
if events.Items[i].LastTimestamp.IsZero() {
if events.Items[i].Series != nil {
if events.Items[i].Series.LastObservedTime.Time.After(oldestEventTime) {
filteredEvents.Items = append(filteredEvents.Items, events.Items[i])
}
}
} else {
if events.Items[i].LastTimestamp.Time.After(oldestEventTime) {
filteredEvents.Items = append(filteredEvents.Items, events.Items[i])
}
}
}
}
rhrmo marked this conversation as resolved.
Show resolved Hide resolved
default:
for i := range events.Items {
// if LastTimestamp is zero then try to check the event series
if events.Items[i].LastTimestamp.IsZero() {
if events.Items[i].Series != nil {
if events.Items[i].Series.LastObservedTime.Time.After(oldestEventTime) {
filteredEvents.Items = append(filteredEvents.Items, events.Items[i])
}
}
} else {
if events.Items[i].LastTimestamp.Time.After(oldestEventTime) {
filteredEvents.Items = append(filteredEvents.Items, events.Items[i])
}
}
}
}

return filteredEvents
}

//eventListToCompactedEventList() coverts EventList() into CompactedEventList()
func eventListToCompactedEventList(events v1.EventList) CompactedEventList {
compactedEvents := CompactedEventList{Items: make([]CompactedEvent, len(events.Items))}
for i := 0; i < len(events.Items); i++ {
rhrmo marked this conversation as resolved.
Show resolved Hide resolved
compactedEvents.Items[i] = CompactedEvent{
rhrmo marked this conversation as resolved.
Show resolved Hide resolved
Namespace: events.Items[i].Namespace,
LastTimestamp: events.Items[i].LastTimestamp.Time,
Reason: events.Items[i].Reason,
Message: events.Items[i].Message,
Type: events.Items[i].Type,
}
if events.Items[i].LastTimestamp.Time.IsZero() {
if events.Items[i].Series != nil {
compactedEvents.Items[i].LastTimestamp = events.Items[i].Series.LastObservedTime.Time
}
}
}
sort.Slice(compactedEvents.Items, func(i, j int) bool {
return compactedEvents.Items[i].LastTimestamp.Before(compactedEvents.Items[j].LastTimestamp)
})

return compactedEvents
}
77 changes: 77 additions & 0 deletions pkg/gatherers/clusterconfig/events_filtering_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package clusterconfig

import (
"reflect"
"testing"
"time"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Test_filterEvents_LastTimeStamp(t *testing.T) {
oldEvent := v1.Event{
ObjectMeta: metav1.ObjectMeta{Name: "oldEvent"},
LastTimestamp: metav1.Time{},
Type: "Normal",
}
newEvent := v1.Event{
ObjectMeta: metav1.ObjectMeta{Name: "newEvent"},
LastTimestamp: metav1.Now(),
Type: "Normal",
}

events := v1.EventList{}
events.Items = append(events.Items, newEvent, oldEvent)

filteredEvents := filterEvents(time.Duration(1*time.Minute), &events, "")

if !reflect.DeepEqual(filteredEvents.Items[0], newEvent) {
t.Errorf("filterEvents() = %v, want %v", filteredEvents.Items[0], newEvent)
}
}

func Test_filterEvents_Warning(t *testing.T) {
normalEvent := v1.Event{
ObjectMeta: metav1.ObjectMeta{Name: "normalEvent"},
LastTimestamp: metav1.Now(),
Type: "Normal",
}
warningEvent := v1.Event{
ObjectMeta: metav1.ObjectMeta{Name: "warningEvent"},
LastTimestamp: metav1.Now(),
Type: "Warning",
}

events := v1.EventList{}
events.Items = append(events.Items, normalEvent, warningEvent)

filteredEvents := filterEvents(time.Duration(1*time.Minute), &events, "Warning")

if !reflect.DeepEqual(filteredEvents.Items[0], warningEvent) {
t.Errorf("filterEvents() = %v, want %v", filteredEvents.Items[0], warningEvent)
}
}

func Test_eventListToCompactedEventList(t *testing.T) {
timeNow := time.Now()
event := v1.Event{
ObjectMeta: metav1.ObjectMeta{Name: "event", Namespace: "test namespace"},
LastTimestamp: metav1.NewTime(timeNow),
Type: "Normal",
Reason: "test reason",
Message: "test message",
}
compactedEvent := CompactedEvent{
Namespace: "test namespace",
LastTimestamp: timeNow,
Reason: "test reason",
Message: "test message",
Type: "Normal",
}
compactedEventList := eventListToCompactedEventList(v1.EventList{Items: []v1.Event{event}})

if !reflect.DeepEqual(compactedEvent, compactedEventList.Items[0]) {
t.Errorf("eventListToCompactedEventList() = %v, want %v", compactedEventList.Items[0], compactedEvent)
}
}
44 changes: 44 additions & 0 deletions pkg/gatherers/clusterconfig/openshift_machine_api_events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package clusterconfig

import (
"context"
"time"

"github.com/openshift/insights-operator/pkg/record"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
)

// GatherOpenshiftMachineApiEvents collects warning ("abnormal") events
// from "openshift-machine-api" namespace
//
// * Location of events in archive: events/
// * Id in config: clusterconfig/openshift_machine_api_events
// * Since versions:
// * 4.12+
func (g *Gatherer) GatherOpenshiftMachineApiEvents(ctx context.Context) ([]record.Record, []error) {
gatherKubeClient, err := kubernetes.NewForConfig(g.gatherProtoKubeConfig)
if err != nil {
return nil, []error{err}
}
records, err := gatherOpenshiftMachineApiEvents(ctx, gatherKubeClient.CoreV1(), g.interval)
if err != nil {
return nil, []error{err}
}
return records, nil
}

func gatherOpenshiftMachineApiEvents(ctx context.Context,
coreClient corev1client.CoreV1Interface,
interval time.Duration) ([]record.Record, error) {
events, err := coreClient.Events("openshift-machine-api").List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
// filter the event list to only recent events with type different than "Normal"
filteredEvents := filterEvents(interval, events, "Warning")
compactedEvents := eventListToCompactedEventList(filteredEvents)

return []record.Record{{Name: "events/openshift-machine-api", Item: record.JSONMarshaller{Object: &compactedEvents}}}, nil
rhrmo marked this conversation as resolved.
Show resolved Hide resolved
}
61 changes: 61 additions & 0 deletions pkg/gatherers/clusterconfig/openshift_machine_api_events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package clusterconfig

import (
"context"
"reflect"
"testing"
"time"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubefake "k8s.io/client-go/kubernetes/fake"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
)

func Test_WarningEvents_gatherOpenshiftMachineApiEvents(t *testing.T) {
normalEvent := v1.Event{
ObjectMeta: metav1.ObjectMeta{Name: "normalEvent"},
LastTimestamp: metav1.Now(),
Type: "Normal",
Reason: "normal",
}
warningEvent := v1.Event{
ObjectMeta: metav1.ObjectMeta{Name: "warningEvent"},
LastTimestamp: metav1.Now(),
Type: "Normal",
Reason: "warning",
}

type args struct {
ctx context.Context
coreClient corev1client.CoreV1Interface
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty openshift-machine-api events",
args: args{
ctx: context.TODO(),
coreClient: kubefake.NewSimpleClientset(normalEvent.DeepCopy(), warningEvent.DeepCopy()).CoreV1(),
},
wantErr: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := gatherOpenshiftMachineApiEvents(tt.args.ctx, tt.args.coreClient, 1*time.Minute)
if (err != nil) != tt.wantErr {
t.Errorf("gatherOpenshiftMachineApiEvents() error = %v, wantErr %v", err, tt.wantErr)
return
}
if reflect.DeepEqual(got, nil) {
t.Errorf("gatherOpenshiftMachineApiEvents() got nil")
}
})
}
}
38 changes: 3 additions & 35 deletions pkg/gatherers/clusterconfig/operators_pods_and_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"regexp"
"sort"
"strings"
"time"

Expand All @@ -33,6 +32,7 @@ type CompactedEvent struct {
LastTimestamp time.Time `json:"lastTimestamp"`
Reason string `json:"reason"`
Message string `json:"message"`
Type string `json:"type"`
}

// CompactedEventList is collection of events
Expand Down Expand Up @@ -189,40 +189,8 @@ func gatherNamespaceEvents(ctx context.Context,
return nil, err
}
// filter the event list to only recent events
oldestEventTime := time.Now().Add(-interval)
var filteredEventIndex []int
for i := range events.Items {
// if LastTimestamp is zero then try to check the event series
if events.Items[i].LastTimestamp.IsZero() {
if events.Items[i].Series != nil {
if events.Items[i].Series.LastObservedTime.Time.After(oldestEventTime) {
filteredEventIndex = append(filteredEventIndex, i)
}
}
} else {
if events.Items[i].LastTimestamp.Time.After(oldestEventTime) {
filteredEventIndex = append(filteredEventIndex, i)
}
}
}
if len(filteredEventIndex) == 0 {
return nil, nil
}
compactedEvents := CompactedEventList{Items: make([]CompactedEvent, len(filteredEventIndex))}
for i, index := range filteredEventIndex {
compactedEvents.Items[i] = CompactedEvent{
Namespace: events.Items[index].Namespace,
LastTimestamp: events.Items[index].LastTimestamp.Time,
Reason: events.Items[index].Reason,
Message: events.Items[index].Message,
}
if events.Items[index].LastTimestamp.Time.IsZero() {
compactedEvents.Items[i].LastTimestamp = events.Items[index].Series.LastObservedTime.Time
}
}
sort.Slice(compactedEvents.Items, func(i, j int) bool {
return compactedEvents.Items[i].LastTimestamp.Before(compactedEvents.Items[j].LastTimestamp)
})
filteredEvents := filterEvents(interval, events, "")
compactedEvents := eventListToCompactedEventList(filteredEvents)

return []record.Record{{Name: fmt.Sprintf("events/%s", namespace), Item: record.JSONMarshaller{Object: &compactedEvents}}}, nil
}
Expand Down