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

Kubernetes-commit: c6935ad50e0809c1dc2d3ac9000abda0ecb49bcc
This commit is contained in:
Harjas 2021-09-14 03:04:02 +05:30 committed by Kubernetes Publisher
parent b137e419be
commit 809211b6c8
4 changed files with 78 additions and 20 deletions

12
go.mod
View File

@ -31,10 +31,10 @@ require (
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210820121016-41cdb8703e55
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.0.0-20210910033053-f745d22351a7
k8s.io/api v0.0.0-20210913233208-5c8efac7a372
k8s.io/apimachinery v0.0.0-20210910032850-71bf7ba06cd4
k8s.io/cli-runtime v0.0.0-20210909235447-7c51b598b842
k8s.io/client-go v0.0.0-20210910114230-9026029b9a9b
k8s.io/cli-runtime v0.0.0-20210910202839-27e21c984482
k8s.io/client-go v0.0.0-20210913233538-29730ab4f5aa
k8s.io/component-base v0.0.0-20210909233903-eae60e423670
k8s.io/component-helpers v0.0.0-20210909234013-3b406f5c0560
k8s.io/klog/v2 v2.20.0
@ -47,10 +47,10 @@ require (
)
replace (
k8s.io/api => k8s.io/api v0.0.0-20210910033053-f745d22351a7
k8s.io/api => k8s.io/api v0.0.0-20210913233208-5c8efac7a372
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20210910032850-71bf7ba06cd4
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20210909235447-7c51b598b842
k8s.io/client-go => k8s.io/client-go v0.0.0-20210910114230-9026029b9a9b
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20210910202839-27e21c984482
k8s.io/client-go => k8s.io/client-go v0.0.0-20210913233538-29730ab4f5aa
k8s.io/code-generator => k8s.io/code-generator v0.0.0-20210910032656-cefb511e9417
k8s.io/component-base => k8s.io/component-base v0.0.0-20210909233903-eae60e423670
k8s.io/component-helpers => k8s.io/component-helpers v0.0.0-20210909234013-3b406f5c0560

12
go.sum
View File

@ -900,14 +900,14 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/api v0.0.0-20210910033053-f745d22351a7 h1:ZO1tBSKR6YKwn6ZMmnrcY1aaJtFyWPHRm0OINamcsX4=
k8s.io/api v0.0.0-20210910033053-f745d22351a7/go.mod h1:sQuJwpcAhgOCSGReco17GgtkSflhybK6puoB8WhPIzg=
k8s.io/api v0.0.0-20210913233208-5c8efac7a372 h1:JzwsOgwSlkfdNB3Cl/AkYoGr8rEgDASJtuWTRF5mrXQ=
k8s.io/api v0.0.0-20210913233208-5c8efac7a372/go.mod h1:sQuJwpcAhgOCSGReco17GgtkSflhybK6puoB8WhPIzg=
k8s.io/apimachinery v0.0.0-20210910032850-71bf7ba06cd4 h1:ST56tx94/5d19mi/fkKMHIm+Q3o1M6YMChuDA4Br4ho=
k8s.io/apimachinery v0.0.0-20210910032850-71bf7ba06cd4/go.mod h1:ZPgmwNP53+C5GcWxpp1Nk2UcdC35zmuEjgRliNyZqgw=
k8s.io/cli-runtime v0.0.0-20210909235447-7c51b598b842 h1:iGLUCbhGWkfottQ3u43bT8bZsscDxCwLYJTUVez3Zjs=
k8s.io/cli-runtime v0.0.0-20210909235447-7c51b598b842/go.mod h1:Hh5+c+fVi62IfTy8tMmxSuq8Ny6pLW3DAeGqg0Bad2o=
k8s.io/client-go v0.0.0-20210910114230-9026029b9a9b h1:GXBby62fSVme/q4JrEwO4qGteHUCVHGQUjVkRiVS9aA=
k8s.io/client-go v0.0.0-20210910114230-9026029b9a9b/go.mod h1:vUi6b26+Y/8jilD6zK66eZexCW+hwnjHSsYYZAO/iqE=
k8s.io/cli-runtime v0.0.0-20210910202839-27e21c984482 h1:iODIArPHSXHCYDl+vjvDNEE+5xcC2UPpYjbw5EsRGOw=
k8s.io/cli-runtime v0.0.0-20210910202839-27e21c984482/go.mod h1:9YtXNNrQl0s1OjkfypqtFYoGbM5wobgFIjNQ9MeQxxY=
k8s.io/client-go v0.0.0-20210913233538-29730ab4f5aa h1:svFLtujJNvMveDcJq68jx1wWWrohNgg5nBnwNGyymOU=
k8s.io/client-go v0.0.0-20210913233538-29730ab4f5aa/go.mod h1:C+/QdDoft+q738Z6s8Coyy3F0YEZqTfAbQ59vwIf+LM=
k8s.io/code-generator v0.0.0-20210910032656-cefb511e9417/go.mod h1:7e+hwbxwCTSDTAarEUgfWyobZsUnHWWlYarT2jgNiNM=
k8s.io/component-base v0.0.0-20210909233903-eae60e423670 h1:Q9PMSBver8seWekbtkqcKrrjncscm1AYjWoBl6+/bdA=
k8s.io/component-base v0.0.0-20210909233903-eae60e423670/go.mod h1:k++RedPnodUZvaB0SFPAdAljhOmueulpyr9wDkx1M+A=

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))
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 = translateTimestampSince(e.FirstTimestamp)
if e.FirstTimestamp.IsZero() {
interval = translateMicroTimestampSince(e.EventTime)
}
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 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 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,
},
},
}