diff --git a/events/data_test.go b/events/data_test.go index 009c4418..c8bd4e92 100644 --- a/events/data_test.go +++ b/events/data_test.go @@ -31,6 +31,7 @@ var ( var ( normal1eventstring = `{Name: "service-r2.1772fc5571f425ff", UID: "a756a650-bf61-4da6-8d69-d5ae40bd943c", Namespace: "ceos2"}` warning1eventstring = `{Name: "r1.177351c307a38f38", UID: "a0450cc2-26e5-46e6-8ef8-be9dec02cd35", Namespace: "ceos1"}` + warning2eventstring = `{Name: "r1.17735337a69ffd33", UID: "efa6a664-ccf3-4ba8-a30b-09e0ebeaa269", Namespace: "ceos6"}` ) var ( @@ -88,118 +89,130 @@ var ( ` normal1eventstatus = EventStatus{ - "Name": "service-r2.1772fc5571f425ff", - "UID": "a756a650-bf61-4da6-8d69-d5ae40bd943c", - "Namespace": "ceos2", - "Message": "announcing from node \"kne-control-plane\" with protocol \"layer2\"", - "Type": "Normal", - "Event": { - "metadata": { - "name": "service-r2.1772fc5571f425ff", - "namespace": "ceos2", - "uid": "a756a650-bf61-4da6-8d69-d5ae40bd943c", - "resourceVersion": "3243", - "creationTimestamp": "2023-07-18T14:24:14Z", - "managedFields": [ - { - "manager": "speaker", - "operation": "Update", - "apiVersion": "v1", - "time": "2023-07-18T14:24:14Z", - "fieldsType": "FieldsV1", - "fieldsV1": { - "f:count": {}, - "f:firstTimestamp": {}, - "f:involvedObject": {}, - "f:lastTimestamp": {}, - "f:message": {}, - "f:reason": {}, - "f:source": { - "f:component": {} - }, - "f:type": {} - } + Name: "service-r2.1772fc5571f425ff", + UID: "a756a650-bf61-4da6-8d69-d5ae40bd943c", + Namespace: "ceos2", + Message: "announcing from node \"kne-control-plane\" with protocol \"layer2\"", + Type: "Normal", + } + + warning1eventdata = ` + { + "metadata": { + "name": "r1.177351c307a38f38", + "namespace": "ceos1", + "uid": "a0450cc2-26e5-46e6-8ef8-be9dec02cd35", + "resourceVersion": "325871", + "creationTimestamp": "2023-07-19T16:29:43Z", + "managedFields": [ + { + "manager": "kube-scheduler", + "operation": "Update", + "apiVersion": "v1", + "time": "2023-07-19T16:39:50Z", + "fieldsType": "FieldsV1", + "fieldsV1": { + "f:count": {}, + "f:firstTimestamp": {}, + "f:involvedObject": {}, + "f:lastTimestamp": {}, + "f:message": {}, + "f:reason": {}, + "f:source": { + "f:component": {} + }, + "f:type": {} } - ] - }, - "involvedObject": { - "kind": "Service", - "namespace": "ceos2", - "name": "service-r2", - "uid": "2459a3c5-c353-4a50-aa70-dc897ffb051e", - "apiVersion": "v1", - "resourceVersion": "3076" - }, - "reason": "nodeAssigned", - "message": "announcing from node \"kne-control-plane\" with protocol \"layer2\"", - "source": { - "component": "metallb-speaker" - }, - "firstTimestamp": "2023-07-18T14:24:14Z", - "lastTimestamp": "2023-07-18T14:24:14Z", - "count": 1, - "type": "Normal", - "eventTime": null, - "reportingComponent": "", - "reportingInstance": "" - } + } + ] + }, + "involvedObject": { + "kind": "Pod", + "namespace": "ceos1", + "name": "r1", + "uid": "b69a3fd5-2f25-42aa-91c4-146c40e53053", + "apiVersion": "v1", + "resourceVersion": "323358" + }, + "reason": "FailedScheduling", + "message": "0/1 nodes are available: 1 Insufficient cpu. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..", + "source": { + "component": "default-scheduler" + }, + "firstTimestamp": "2023-07-19T16:29:43Z", + "lastTimestamp": "2023-07-19T16:39:50Z", + "count": 3, + "type": "Warning", + "eventTime": null, + "reportingComponent": "", + "reportingInstance": "" } - + ` warning1eventstatus = EventStatus{ - "Name": "r1.177351c307a38f38", - "UID": "a0450cc2-26e5-46e6-8ef8-be9dec02cd35", - "Namespace": "ceos1", - "Message": "0/1 nodes are available: 1 Insufficient cpu. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..", - "Type": "Warning", - "Event": { - "metadata": { - "name": "r1.177351c307a38f38", - "namespace": "ceos1", - "uid": "a0450cc2-26e5-46e6-8ef8-be9dec02cd35", - "resourceVersion": "325871", - "creationTimestamp": "2023-07-19T16:29:43Z", - "managedFields": [ - { - "manager": "kube-scheduler", - "operation": "Update", - "apiVersion": "v1", - "time": "2023-07-19T16:39:50Z", - "fieldsType": "FieldsV1", - "fieldsV1": { - "f:count": {}, - "f:firstTimestamp": {}, - "f:involvedObject": {}, - "f:lastTimestamp": {}, - "f:message": {}, - "f:reason": {}, - "f:source": { - "f:component": {} - }, - "f:type": {} - } + Name: "r1.177351c307a38f38", + UID: "a0450cc2-26e5-46e6-8ef8-be9dec02cd35", + Namespace: "ceos1", + Message: "0/1 nodes are available: 1 Insufficient cpu. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..", + Type: "Warning", + } + + warning2eventdata = ` + { + "metadata": { + "name": "r1.17735337a69ffd33", + "namespace": "ceos6", + "uid": "efa6a664-ccf3-4ba8-a30b-09e0ebeaa269", + "resourceVersion": "1621", + "creationTimestamp": "2023-07-19T16:56:23Z", + "managedFields": [ + { + "manager": "kube-scheduler", + "operation": "Update", + "apiVersion": "v1", + "time": "2023-07-19T16:56:23Z", + "fieldsType": "FieldsV1", + "fieldsV1": { + "f:count": {}, + "f:firstTimestamp": {}, + "f:involvedObject": {}, + "f:lastTimestamp": {}, + "f:message": {}, + "f:reason": {}, + "f:source": { + "f:component": {} + }, + "f:type": {} } - ] - }, - "involvedObject": { - "kind": "Pod", - "namespace": "ceos1", - "name": "r1", - "uid": "b69a3fd5-2f25-42aa-91c4-146c40e53053", - "apiVersion": "v1", - "resourceVersion": "323358" - }, - "reason": "FailedScheduling", - "message": "0/1 nodes are available: 1 Insufficient cpu. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..", - "source": { - "component": "default-scheduler" - }, - "firstTimestamp": "2023-07-19T16:29:43Z", - "lastTimestamp": "2023-07-19T16:39:50Z", - "count": 3, - "type": "Warning", - "eventTime": null, - "reportingComponent": "", - "reportingInstance": "" - } + } + ] + }, + "involvedObject": { + "kind": "Pod", + "namespace": "ceos6", + "name": "r1", + "uid": "140da1fb-0ff4-4b9d-b512-41c64bd9b1e1", + "apiVersion": "v1", + "resourceVersion": "1620" + }, + "reason": "FailedScheduling", + "message": "0/1 nodes are available: 1 Insufficient memory. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..", + "source": { + "component": "default-scheduler" + }, + "firstTimestamp": "2023-07-19T16:56:23Z", + "lastTimestamp": "2023-07-19T16:56:23Z", + "count": 1, + "type": "Warning", + "eventTime": null, + "reportingComponent": "", + "reportingInstance": "" + } + ` + warning2eventstatus = EventStatus{ + Name: "r1.17735337a69ffd33", + UID: "efa6a664-ccf3-4ba8-a30b-09e0ebeaa269", + Namespace: "ceos6", + Message: "0/1 nodes are available: 1 Insufficient memory. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..", + Type: "Warning", } -) \ No newline at end of file +) diff --git a/events/events_test.go b/events/events_test.go index 1bfcdc40..8e4c97c2 100644 --- a/events/events_test.go +++ b/events/events_test.go @@ -24,6 +24,8 @@ func TestGetEventStatus(t *testing.T) { var eventGetUpdatesStatus = [][]EventStatus{ {normal1eventstatus}, + {warning1eventstatus}, + {warning2eventstatus}, } next := -1 @@ -114,6 +116,8 @@ func TestWatchEventStatus(t *testing.T) { var wanted = []*EventStatus{ &normal1eventstatus, + &warning1eventstatus, + &warning2eventstatus, } var updates = []*corev1.Event{ normal1event, @@ -181,7 +185,7 @@ func TestString(t *testing.T) { event *EventStatus status string }{ - {"event1", &EventStatus{Name: "event1", Namespace: "ns", UID: "event-1"}, `{Name: "event1", UID: "event-1", Namespace: "ns"`}, // No containers + {"event1", &EventStatus{Name: "event1", Namespace: "ns", UID: "event-1"}, `{Name: "event1", UID: "event-1", Namespace: "ns"`}, } { t.Run(tt.name, func(t *testing.T) { status := tt.event.String() @@ -194,7 +198,7 @@ func TestString(t *testing.T) { func TestEqual(t *testing.T) { different := &EventStatus{} - for i, event := range []*EventStatus{&normal1eventstatus} { + for i, event := range []*EventStatus{&normal1eventstatus, &warning1eventstatus, &warning2eventstatus} { lintEvent := event if !event.Equal(lintEvent) { t.Errorf("#%d: Equal returned false on equal events", i) diff --git a/events/status.go b/events/status.go index 139ecda9..394a8d17 100644 --- a/events/status.go +++ b/events/status.go @@ -125,7 +125,6 @@ func (w *Watcher) displayEvent(s *EventStatus) bool { newNamespace := s.Namespace != w.currentNamespace if newNamespace { w.currentNamespace = s.Namespace - w.display("NS: %s", s.Namespace) newNamespace = false } w.display("NS: %s", s.Namespace) @@ -135,8 +134,8 @@ func (w *Watcher) displayEvent(s *EventStatus) bool { message := s.Message for _, m := range errorMsgs { - // Error out if namespace is currentnamesapce and message contains predefinedcheck namespace - if w.currentNamespace == s.Namespace && strings.Contains(message, m) { + // Error out if message contains predefined message + if strings.Contains(message, m) { w.errCh <- fmt.Errorf("Event failed due to %s . Message: %s", s.Event.Reason, message) w.cancel() return false