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 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
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,
}
}
83 changes: 45 additions & 38 deletions broker/machinebroker/server/event_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,84 +10,91 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

computev1alpha1 "github.com/ironcore-dev/ironcore/api/compute/v1alpha1"
"github.com/ironcore-dev/ironcore/broker/machinebroker/apiutils"
irievent "github.com/ironcore-dev/ironcore/iri/apis/event/v1alpha1"
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) ([]*irievent.Event, error) {
log := ctrl.LoggerFrom(ctx)
machineEventList := &v1.EventList{}
selectorField := fields.Set{}
selectorField[InvolvedObjectName] = machineID

selectorField := fields.Set{
InvolvedObjectKindSelector: InvolvedObjectKind,
InvolvedObjectAPIversionSelector: computev1alpha1.SchemeGroupVersion.String(),
}
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 iriEvents []*irievent.Event
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 {
log.V(1).Info("Unable to get ironcore machine", "MachineName", machineEvent.InvolvedObject.Name)
continue
lukasfrank marked this conversation as resolved.
Show resolved Hide resolved
}
machineObjectMetadata, err := apiutils.GetObjectMetadata(&ironcoreMachine.ObjectMeta)
if err != nil {
continue
}
iriEvent := &irievent.Event{
Spec: &irievent.EventSpec{
InvolvedObjectMeta: machineObjectMetadata,
Reason: machineEvent.Reason,
Message: machineEvent.Message,
Type: machineEvent.Type,
EventTime: machineEvent.LastTimestamp.Unix(),
},
}
iriEvents = append(iriEvents, iriEvent)
}
return iriEvents, nil
}

func (s *Server) filterEvents(events []*iri.Event, filter *iri.EventFilter) []*iri.Event {
func (s *Server) filterEvents(events []*irievent.Event, filter *iri.EventFilter) []*irievent.Event {
if filter == nil {
return events
}

var (
res []*iri.Event
res []*irievent.Event
sel = labels.SelectorFromSet(filter.LabelSelector)
)
for _, iriEvent := range events {
if !sel.Matches(labels.Set(iriEvent.Metadata.Labels)) {
continue
}
if (filter.EventsFromTime >= 0 && iriEvent.Spec.EventTime >= filter.EventsFromTime) && (filter.EventsToTime >= 0 && iriEvent.Spec.EventTime <= filter.EventsToTime) {
if sel.Matches(labels.Set(iriEvent.Spec.InvolvedObjectMeta.Labels)) {
lukasfrank marked this conversation as resolved.
Show resolved Hide resolved
if filter.EventsFromTime > 0 && filter.EventsToTime > 0 {
if iriEvent.Spec.EventTime >= filter.EventsFromTime && iriEvent.Spec.EventTime <= filter.EventsToTime {
res = append(res, iriEvent)
}
continue
}
res = append(res, iriEvent)
}

}
return res
}

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
}
23 changes: 12 additions & 11 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 @@ -68,19 +68,20 @@ var _ = Describe("ListEvents", func() {
eventRecorder.Event(ironcoreMachine, corev1.EventTypeNormal, "testing", "this is test event")

By("listing the machine events")
resp, err := srv.ListEvents(ctx, &iri.ListEventsRequest{})
resp, err := srv.ListEvents(ctx, &iri.ListEventsRequest{Filter: &iri.EventFilter{
LabelSelector: map[string]string{machinepoolletv1alpha1.MachineUIDLabel: "foobar"},
}})

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)),
)),
),
)
})
})
1 change: 1 addition & 0 deletions hack/update-proto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function generate() {
}

generate "iri/apis/meta/v1alpha1"
generate "iri/apis/event/v1alpha1"
generate "iri/apis/machine/v1alpha1"
generate "iri/apis/volume/v1alpha1"
generate "iri/apis/bucket/v1alpha1"
Loading