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 2 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
78 changes: 78 additions & 0 deletions pkg/gatherers/clusterconfig/openshift_machine_api_events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package clusterconfig

import (
"context"
"fmt"
"sort"
"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: config/events/
func (g *Gatherer) GatherOpenshiftMachineApiEvents(ctx context.Context) ([]record.Record, []error) {
gatherKubeClient, err := kubernetes.NewForConfig(g.gatherProtoKubeConfig)
if err != nil {
return nil, []error{err}
}
namespace := "openshift-machine-api"
records, err := gatherOpenshiftMachineApiEvents(ctx, gatherKubeClient.CoreV1(), namespace, g.interval)
if err != nil {
return nil, []error{err}
}
return records, nil
}

func gatherOpenshiftMachineApiEvents(ctx context.Context,
coreClient corev1client.CoreV1Interface,
namespace string,
rhrmo marked this conversation as resolved.
Show resolved Hide resolved
interval time.Duration) ([]record.Record, error) {
events, err := coreClient.Events(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
// filter the event list to only recent events with type different than "Normal"
oldestEventTime := time.Now().Add(-interval)
var filteredEventIndex []int
for i := range events.Items {
if events.Items[i].Type != "Normal" { //TODO see if there is types enum
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))} //TODO check if this is good output
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)
})

return []record.Record{{Name: fmt.Sprintf("config/events/%s", namespace), Item: record.JSONMarshaller{Object: &compactedEvents}}}, nil
rhrmo marked this conversation as resolved.
Show resolved Hide resolved
}
51 changes: 51 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,51 @@
package clusterconfig

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

"github.com/openshift/insights-operator/pkg/record"
kubefake "k8s.io/client-go/kubernetes/fake"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
)

func Test_WarningEvents_gatherOpenshiftMachineApiEvents(t *testing.T) {
type args struct {
ctx context.Context
coreClient corev1client.CoreV1Interface
namespace string
}
tests := []struct {
name string
args args
want []record.Record
wantErr bool
}{
{
name: "openshift-machine-api events",
args: args{
ctx: context.TODO(),
coreClient: kubefake.NewSimpleClientset().CoreV1(),
namespace: "openshift-machine-api",
},
want: []record.Record{},
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, tt.args.namespace, 1*time.Minute)
if (err != nil) != tt.wantErr {
t.Errorf("gatherOpenshiftMachineApiEvents() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("gatherNameSpaceEvents() = %v, want %v", got, tt.want)
}
})
}
}