kubectl: Use fields from event series when computing describe events for a object (#104482)

* take into account new fields for event

* add event with old event fields for test

* fix: remove unnecessary "+1" from event series count

* fix: update the assertion for failing test case
This commit is contained in:
Harjas 2021-09-14 03:04:02 +05:30 committed by GitHub
parent 62a6df9e0b
commit c6935ad50e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 8 deletions

View File

@ -4140,13 +4140,16 @@ func DescribeEvents(el *corev1.EventList, w PrefixWriter) {
w.Write(LEVEL_1, "----\t------\t----\t----\t-------\n")
for _, e := range el.Items {
var interval string
if e.Count > 1 {
interval = fmt.Sprintf("%s (x%d over %s)", translateTimestampSince(e.LastTimestamp), e.Count, translateTimestampSince(e.FirstTimestamp))
} else {
interval = translateTimestampSince(e.FirstTimestamp)
if e.FirstTimestamp.IsZero() {
interval = translateMicroTimestampSince(e.EventTime)
firstTimestampSince := translateMicroTimestampSince(e.EventTime)
if e.EventTime.IsZero() {
firstTimestampSince = translateTimestampSince(e.FirstTimestamp)
}
if e.Series != nil {
interval = fmt.Sprintf("%s (x%d over %s)", translateMicroTimestampSince(e.Series.LastObservedTime), e.Series.Count, firstTimestampSince)
} else if e.Count > 1 {
interval = fmt.Sprintf("%s (x%d over %s)", translateTimestampSince(e.LastTimestamp), e.Count, firstTimestampSince)
} else {
interval = firstTimestampSince
}
source := e.Source.Component
if source == "" {

View File

@ -2004,6 +2004,27 @@ func TestDescribeDeployment(t *testing.T) {
ReadyReplicas: 0,
AvailableReplicas: 1,
},
}, &corev1.Event{
ObjectMeta: metav1.ObjectMeta{
Name: "bar-000",
Namespace: "foo",
},
InvolvedObject: corev1.ObjectReference{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: "bar",
Namespace: "foo",
UID: "00000000-0000-0000-0000-000000000001",
},
Type: corev1.EventTypeNormal,
Reason: "ScalingReplicaSet",
Message: "Scaled up replica set bar-002 to 1",
ReportingController: "deployment-controller",
EventTime: metav1.NewMicroTime(time.Now().Add(-20 * time.Minute)),
Series: &corev1.EventSeries{
Count: 3,
LastObservedTime: metav1.NewMicroTime(time.Now().Add(-12 * time.Minute)),
},
}, &corev1.Event{
ObjectMeta: metav1.ObjectMeta{
Name: "bar-001",
@ -2042,6 +2063,23 @@ func TestDescribeDeployment(t *testing.T) {
Component: "deployment-controller",
},
FirstTimestamp: metav1.NewTime(time.Now().Add(-2 * time.Minute)),
}, &corev1.Event{
ObjectMeta: metav1.ObjectMeta{
Name: "bar-003",
Namespace: "foo",
},
InvolvedObject: corev1.ObjectReference{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: "bar",
Namespace: "foo",
UID: "00000000-0000-0000-0000-000000000001",
},
Type: corev1.EventTypeNormal,
Reason: "ScalingReplicaSet",
Message: "Scaled down replica set bar-002 to 1",
ReportingController: "deployment-controller",
EventTime: metav1.NewMicroTime(time.Now().Add(-1 * time.Minute)),
},
},
expects: []string{
@ -2050,8 +2088,10 @@ func TestDescribeDeployment(t *testing.T) {
"OldReplicaSets: bar-001 (2/2 replicas created)",
"NewReplicaSet: bar-002 (1/1 replicas created)",
"Events:\n",
"Normal ScalingReplicaSet 12m (x3 over 20m) deployment-controller Scaled up replica set bar-002 to 1",
"Normal ScalingReplicaSet 10m deployment-controller Scaled up replica set bar-001 to 2",
"Normal ScalingReplicaSet 2m deployment-controller Scaled up replica set bar-002 to 1",
"Normal ScalingReplicaSet 60s deployment-controller Scaled down replica set bar-002 to 1",
},
},
}
@ -3694,6 +3734,7 @@ func TestDescribeEvents(t *testing.T) {
Items: []corev1.Event{
{
ObjectMeta: metav1.ObjectMeta{
Name: "event-1",
Namespace: "foo",
},
Source: corev1.EventSource{Component: "kubelet"},
@ -3703,6 +3744,20 @@ func TestDescribeEvents(t *testing.T) {
Count: 1,
Type: corev1.EventTypeNormal,
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "event-2",
Namespace: "foo",
},
Source: corev1.EventSource{Component: "kubelet"},
Message: "Item 1",
EventTime: metav1.NewMicroTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
Series: &corev1.EventSeries{
Count: 1,
LastObservedTime: metav1.NewMicroTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
},
Type: corev1.EventTypeNormal,
},
},
}