Skip to content
This repository has been archived by the owner on Aug 31, 2022. It is now read-only.

fix: GetTimestampMs handles different event timestamp #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 14 additions & 3 deletions pkg/kube/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package kube

import (
"encoding/json"
corev1 "k8s.io/api/core/v1"
"strings"
"time"

corev1 "k8s.io/api/core/v1"
)

type EnhancedEvent struct {
Expand Down Expand Up @@ -50,10 +51,20 @@ func (e *EnhancedEvent) ToJSON() []byte {
}

func (e *EnhancedEvent) GetTimestampMs() int64 {
return e.FirstTimestamp.UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
timestamp := e.FirstTimestamp.Time
if timestamp.IsZero() {
timestamp = e.EventTime.Time
}

return timestamp.UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
}

func (e *EnhancedEvent) GetTimestampISO8601() string {
timestamp := e.FirstTimestamp.Time
if timestamp.IsZero() {
timestamp = e.EventTime.Time
}

layout := "2006-01-02T15:04:05.000Z"
return e.FirstTimestamp.Format(layout)
return timestamp.Format(layout)
}
8 changes: 6 additions & 2 deletions pkg/kube/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewEventWatcher(config *rest.Config, namespace string, throttlePeriod int64
labelCache: NewLabelCache(config),
annotationCache: NewAnnotationCache(config),
fn: fn,
throttlePeriod: time.Second*time.Duration(throttlePeriod),
throttlePeriod: time.Second * time.Duration(throttlePeriod),
}

informer.AddEventHandler(watcher)
Expand All @@ -54,7 +54,11 @@ func (e *EventWatcher) OnUpdate(oldObj, newObj interface{}) {
func (e *EventWatcher) onEvent(event *corev1.Event) {
// TODO: Re-enable this after development
// It's probably an old event we are catching, it's not the best way but anyways
if time.Since(event.LastTimestamp.Time) > e.throttlePeriod {
timestamp := event.LastTimestamp.Time
if timestamp.IsZero() {
timestamp = event.EventTime.Time
}
if time.Since(timestamp) > e.throttlePeriod {
return
}

Expand Down