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

Add get events command to irictl-machine #1089

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 0 additions & 16 deletions broker/machinebroker/apiutils/apiutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,3 @@ func IsManagedBy(o metav1.Object, manager string) bool {
actual, ok := o.GetLabels()[machinebrokerv1alpha1.ManagerLabel]
return ok && actual == manager
}

func GetIRIObjectMetadata(o metav1.Object) *irimeta.ObjectMetadata {
var deletedAt int64
if !o.GetDeletionTimestamp().IsZero() {
deletedAt = o.GetDeletionTimestamp().UnixNano()
}

return &irimeta.ObjectMetadata{
Id: o.GetName(),
Annotations: o.GetAnnotations(),
Labels: o.GetLabels(),
Generation: o.GetGeneration(),
CreatedAt: o.GetCreationTimestamp().UnixNano(),
DeletedAt: deletedAt,
}
}
76 changes: 44 additions & 32 deletions broker/machinebroker/server/event_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,56 @@ import (
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"

computev1alpha1 "github.com/ironcore-dev/ironcore/api/compute/v1alpha1"
"github.com/ironcore-dev/ironcore/broker/machinebroker/apiutils"
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
)

const InvolvedObjectName = "involvedObject.name"
const (
InvolvedObjectKind = "Machine"
InvolvedObjectKindSelector = "involvedObject.kind"
InvolvedObjectAPIversionSelector = "involvedObject.apiVersion"
ushabelgur marked this conversation as resolved.
Show resolved Hide resolved
)

func (s *Server) listEvents(ctx context.Context, machineID string) ([]*iri.Event, error) {
func (s *Server) listEvents(ctx context.Context) ([]*iri.Event, error) {
machineEventList := &v1.EventList{}
selectorField := fields.Set{}
selectorField[InvolvedObjectName] = machineID
selectorField[InvolvedObjectKindSelector] = InvolvedObjectKind
selectorField[InvolvedObjectAPIversionSelector] = computev1alpha1.SchemeGroupVersion.String()
ushabelgur marked this conversation as resolved.
Show resolved Hide resolved

if err := s.cluster.Client().List(ctx, machineEventList,
client.InNamespace(s.cluster.Namespace()), client.MatchingFieldsSelector{Selector: selectorField.AsSelector()},
); err != nil {
return nil, fmt.Errorf("error listing machine events: %w", err)
return nil, err
}

var iriEvents []*iri.Event
var eventTime int64
for _, machineEvent := range machineEventList.Items {
iriEvent := &iri.Event{
Metadata: apiutils.GetIRIObjectMetadata(&machineEvent.ObjectMeta),
Spec: &iri.EventSpec{
Reason: machineEvent.Reason,
Message: machineEvent.Message,
Type: machineEvent.Type,
EventTime: machineEvent.FirstTimestamp.Unix(),
},
ironcoreMachine, err := s.getIronCoreMachine(ctx, machineEvent.InvolvedObject.Name)
if err != nil {
continue
lukasfrank marked this conversation as resolved.
Show resolved Hide resolved
} else {
machineObjectMetadata, err := apiutils.GetObjectMetadata(&ironcoreMachine.ObjectMeta)
if err != nil {
continue
} else {
eventTime = machineEvent.LastTimestamp.Unix()
if eventTime < 0 {
lukasfrank marked this conversation as resolved.
Show resolved Hide resolved
eventTime = machineEvent.FirstTimestamp.Unix()
}
iriEvent := &iri.Event{
Spec: &iri.EventSpec{
InvolvedObjectMeta: machineObjectMetadata,
Reason: machineEvent.Reason,
Message: machineEvent.Message,
Type: machineEvent.Type,
EventTime: eventTime,
},
}
iriEvents = append(iriEvents, iriEvent)
}
lukasfrank marked this conversation as resolved.
Show resolved Hide resolved
}
iriEvents = append(iriEvents, iriEvent)
}
return iriEvents, nil
}
Expand All @@ -55,10 +76,14 @@ func (s *Server) filterEvents(events []*iri.Event, filter *iri.EventFilter) []*i
sel = labels.SelectorFromSet(filter.LabelSelector)
)
for _, iriEvent := range events {
if !sel.Matches(labels.Set(iriEvent.Metadata.Labels)) {
if !sel.Matches(labels.Set(iriEvent.Spec.InvolvedObjectMeta.Labels)) {
continue
}
if (filter.EventsFromTime >= 0 && iriEvent.Spec.EventTime >= filter.EventsFromTime) && (filter.EventsToTime >= 0 && iriEvent.Spec.EventTime <= filter.EventsToTime) {
if filter.EventsFromTime > 0 && filter.EventsToTime > 0 {
if iriEvent.Spec.EventTime >= filter.EventsFromTime && iriEvent.Spec.EventTime <= filter.EventsToTime {
res = append(res, iriEvent)
}
} else {
res = append(res, iriEvent)
}
lukasfrank marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -67,27 +92,14 @@ func (s *Server) filterEvents(events []*iri.Event, filter *iri.EventFilter) []*i
}

func (s *Server) ListEvents(ctx context.Context, req *iri.ListEventsRequest) (*iri.ListEventsResponse, error) {
ironcoreMachineList, err := s.listIroncoreMachines(ctx)
iriEvents, err := s.listEvents(ctx)
if err != nil {
return nil, err
return nil, fmt.Errorf("error listing machine events : %w", err)
}
var machineEvents []*iri.MachineEvents
for i := range ironcoreMachineList.Items {
ironcoreMachine := &ironcoreMachineList.Items[i]
iriEvents, err := s.listEvents(ctx, ironcoreMachine.Name)
if err != nil {
return nil, err
}

iriEvents = s.filterEvents(iriEvents, req.Filter)
ironcoreMachineMeta, err := apiutils.GetObjectMetadata(&ironcoreMachine.ObjectMeta)
if err != nil {
return nil, fmt.Errorf("error listing machine events: %w", err)
}
machineEvents = append(machineEvents, &iri.MachineEvents{InvolvedObjectMeta: ironcoreMachineMeta, Events: iriEvents})
}
iriEvents = s.filterEvents(iriEvents, req.Filter)

return &iri.ListEventsResponse{
MachineEvents: machineEvents,
Events: iriEvents,
}, nil
}
19 changes: 9 additions & 10 deletions broker/machinebroker/server/event_list_test.go
lukasfrank marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,14 @@ var _ = Describe("ListEvents", func() {

Expect(err).NotTo(HaveOccurred())

Expect(resp.MachineEvents).To(ConsistOf(SatisfyAll(
HaveField("InvolvedObjectMeta.Id", Equal(ironcoreMachine.Name)),
HaveField("Events", ConsistOf(SatisfyAll(
HaveField("Spec", SatisfyAll(
HaveField("Reason", Equal("testing")),
HaveField("Message", Equal("this is test event")),
HaveField("Type", Equal(corev1.EventTypeNormal)),
)),
))),
)))
Expect(resp.Events).To(ConsistOf(
HaveField("Spec", SatisfyAll(
HaveField("InvolvedObjectMeta.Id", Equal(ironcoreMachine.Name)),
HaveField("Reason", Equal("testing")),
HaveField("Message", Equal("this is test event")),
HaveField("Type", Equal(corev1.EventTypeNormal)),
)),
),
)
})
})
Loading