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 13 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
18 changes: 18 additions & 0 deletions docs/insights-archive-sample/events/openshift-machine-api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"items":[
{
"namespace":"openshift-machine-api",
"lastTimestamp":"2022-08-24T11:40:44+02:00",
"reason":"FailedUpdate",
"message":"cluster-lrqft-worker-us-east-2a-jsmg4: reconciler failed to Update machine: requeue in: 20s",
"type":"Warning"
},
{
"namespace":"openshift-machine-api",
"lastTimestamp":"2022-08-24T11:41:05+02:00",
"reason":"FailedUpdate",
"message":"cluster-lrqft-worker-us-east-2c-rlfpx: reconciler failed to Update machine: requeue in: 20s",
"type":"Warning"
}
]
}
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
72 changes: 72 additions & 0 deletions pkg/gatherers/clusterconfig/events_filtering.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package clusterconfig

import (
"sort"
"time"

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

//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 isEventAbnormal(&events.Items[i]) && isEventNew(&events.Items[i], oldestEventTime) {
filteredEvents.Items = append(filteredEvents.Items, events.Items[i])
}
}
default:
for i := range events.Items {
if isEventNew(&events.Items[i], oldestEventTime) {
filteredEvents.Items = append(filteredEvents.Items, events.Items[i])
}
}
rhrmo marked this conversation as resolved.
Show resolved Hide resolved
}

return filteredEvents
}

// if LastTimestamp is zero then try to check the event series
func isEventNew(event *v1.Event, oldestEventTime time.Time) bool {
rhrmo marked this conversation as resolved.
Show resolved Hide resolved
if event.LastTimestamp.Time.After(oldestEventTime) {
return true
} else if event.LastTimestamp.IsZero() {
if event.Series != nil {
if event.Series.LastObservedTime.Time.After(oldestEventTime) {
return true
}
}
}
return false
}

func isEventAbnormal(event *v1.Event) bool {
return event.Type != "Normal"
}

//eventListToCompactedEventList() coverts EventList() into CompactedEventList()
func eventListToCompactedEventList(events *v1.EventList) CompactedEventList {
compactedEvents := CompactedEventList{Items: make([]CompactedEvent, len(events.Items))}
for i := range events.Items {
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
}
187 changes: 187 additions & 0 deletions pkg/gatherers/clusterconfig/events_filtering_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package clusterconfig

import (
"reflect"
"testing"
"time"

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

func Test_filterEvents(t *testing.T) {
timeNow := time.Now()
tests := []struct {
name string
events v1.EventList
types string
expected v1.EventList
}{
{
name: "Last TimeStamp",
events: v1.EventList{
Items: []v1.Event{
{
ObjectMeta: metav1.ObjectMeta{Name: "oldEvent1"},
LastTimestamp: metav1.Time{},
Type: "Normal",
},
{
ObjectMeta: metav1.ObjectMeta{Name: "newEvent1"},
LastTimestamp: metav1.NewTime(timeNow),
Type: "Normal",
},
{
ObjectMeta: metav1.ObjectMeta{Name: "oldEvent2"},
LastTimestamp: metav1.Time{},
Type: "Warning",
},
{
ObjectMeta: metav1.ObjectMeta{Name: "newEvent2"},
LastTimestamp: metav1.NewTime(timeNow),
Type: "Warning",
},
{
ObjectMeta: metav1.ObjectMeta{Name: "newEvent3"},
LastTimestamp: metav1.NewTime(timeNow),
Type: "Normal",
},
},
},
types: "",
expected: v1.EventList{
Items: []v1.Event{
{
ObjectMeta: metav1.ObjectMeta{Name: "newEvent1"},
LastTimestamp: metav1.NewTime(timeNow),
Type: "Normal",
},
{
ObjectMeta: metav1.ObjectMeta{Name: "newEvent2"},
LastTimestamp: metav1.NewTime(timeNow),
Type: "Warning",
},
{
ObjectMeta: metav1.ObjectMeta{Name: "newEvent3"},
LastTimestamp: metav1.NewTime(timeNow),
Type: "Normal",
},
},
},
},
{
name: "Filterning abnormal events",
events: v1.EventList{
Items: []v1.Event{
{
ObjectMeta: metav1.ObjectMeta{Name: "normalEvent1"},
LastTimestamp: metav1.NewTime(timeNow),
Type: "Normal",
},
{
ObjectMeta: metav1.ObjectMeta{Name: "warningEvent1"},
LastTimestamp: metav1.NewTime(timeNow),
Type: "Warning",
},
{
ObjectMeta: metav1.ObjectMeta{Name: "normalEvent2"},
LastTimestamp: metav1.NewTime(timeNow),
Type: "Normal",
},
{
ObjectMeta: metav1.ObjectMeta{Name: "warningEvent2"},
LastTimestamp: metav1.NewTime(timeNow),
Type: "Warning",
},
{
ObjectMeta: metav1.ObjectMeta{Name: "warningEvent3"},
LastTimestamp: metav1.NewTime(timeNow),
Type: "Warning",
},
},
},
types: "Warning",
expected: v1.EventList{
Items: []v1.Event{
{
ObjectMeta: metav1.ObjectMeta{Name: "warningEvent1"},
LastTimestamp: metav1.NewTime(timeNow),
Type: "Warning",
},
{
ObjectMeta: metav1.ObjectMeta{Name: "warningEvent2"},
LastTimestamp: metav1.NewTime(timeNow),
Type: "Warning",
},
{
ObjectMeta: metav1.ObjectMeta{Name: "warningEvent3"},
LastTimestamp: metav1.NewTime(timeNow),
Type: "Warning",
},
},
},
},
}
rhrmo marked this conversation as resolved.
Show resolved Hide resolved

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
filteredEvents := filterEvents(1*time.Minute, &test.events, test.types)
if !reflect.DeepEqual(filteredEvents, test.expected) {
t.Errorf("filterEvents() = %v, want %v", filteredEvents, test.expected)
}
})
}
}

func Test_isEventNew(t *testing.T) {
tests := []struct {
event v1.Event
expected bool
}{
{
event: v1.Event{
ObjectMeta: metav1.ObjectMeta{Name: "newEvent"},
LastTimestamp: metav1.Now(),
Type: "Normal",
},
expected: true,
},
{
event: v1.Event{
ObjectMeta: metav1.ObjectMeta{Name: "oldEvent"},
LastTimestamp: metav1.NewTime(time.Now().Add(-6 * time.Minute)),
rhrmo marked this conversation as resolved.
Show resolved Hide resolved
Type: "Normal",
},
expected: false,
},
}

for _, test := range tests {
if isEventNew(&test.event, time.Now().Add(-5*time.Minute)) != test.expected {
t.Errorf("isEventNew() = %v, got %v", !test.expected, test.expected)
}
}
}

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)
}
}
47 changes: 47 additions & 0 deletions pkg/gatherers/clusterconfig/openshift_machine_api_events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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")
if len(filteredEvents.Items) == 0 {
return nil, nil
}
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")
}
})
}
}
Loading