Skip to content
This repository was archived by the owner on Mar 16, 2024. It is now read-only.

Commit 28dc523

Browse files
authored
Merge pull request #1822 from njhale/events/generalize-ttl
Generalize event TTL logic
2 parents e5533b0 + 657db17 commit 28dc523

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

pkg/apis/internal.acorn.io/v1/event.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,17 @@ type EventInstance struct {
5454
Details GenericMap `json:"details,omitempty"`
5555
}
5656

57+
// GetObserved returns the time that the Event was first observed.
58+
func (e EventInstance) GetObserved() MicroTime {
59+
return e.Observed
60+
}
61+
5762
// MicroTime represents a time with microsecond level precision.
5863
//
5964
// It extends metav1.MicroTime to allow unmarshaling from RFC3339.
6065
type MicroTime metav1.MicroTime
6166

62-
// DeepCopy returns a deep-copy of the MicroTime value. The underlying time.Time
67+
// DeepCopyInto returns a deep-copy of the MicroTime value. The underlying time.Time
6368
// type is effectively immutable in the time API, so it is safe to
6469
// copy-by-assign, despite the presence of (unexported) Pointer fields.
6570
func (t *MicroTime) DeepCopyInto(out *MicroTime) {

pkg/controller/eventinstance/eventinstance.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ func GCExpired() router.HandlerFunc {
2525
}
2626

2727
return handler{
28-
getTTL: func(ctx context.Context, getter kclient.Reader) (time.Duration, error) {
28+
getTTL: func(
29+
ctx context.Context,
30+
getter kclient.Reader,
31+
) (time.Duration, error) {
2932
cfg, err := config.Get(ctx, getter)
3033
if err != nil {
3134
return 0, err
@@ -56,13 +59,25 @@ func GCExpired() router.HandlerFunc {
5659
}.gcExpired
5760
}
5861

62+
// GCable describes types that can be GCed by the router.HandlerFunc returned by GCExpired.
63+
type GCable interface {
64+
// GetObserved returns the time of the initial observation.
65+
GetObserved() v1.MicroTime
66+
}
67+
5968
type handler struct {
6069
// getTTL returns the TTL to use for event expiration.
61-
getTTL func(context.Context, kclient.Reader) (time.Duration, error)
70+
getTTL func(
71+
context.Context,
72+
kclient.Reader,
73+
) (time.Duration, error)
6274
}
6375

64-
func (h handler) gcExpired(req router.Request, resp router.Response) error {
65-
e := req.Object.(*v1.EventInstance)
76+
func (h handler) gcExpired(
77+
req router.Request,
78+
resp router.Response,
79+
) error {
80+
e := req.Object
6681

6782
// Get the currently configured TTL
6883
ttl, err := h.getTTL(req.Ctx, req.Client)
@@ -71,22 +86,26 @@ func (h handler) gcExpired(req router.Request, resp router.Response) error {
7186
}
7287

7388
// Check expiration
74-
if now, expiration := time.Now(), e.Observed.Add(ttl); now.Before(expiration) {
89+
if now, expiration := time.Now(), e.(GCable).GetObserved().Add(ttl); now.Before(expiration) {
7590
// Still fresh, wait until expiration
7691
resp.RetryAfter(time.Until(expiration))
7792
return nil
7893
}
7994

8095
// Expired, delete the event
81-
if err := req.Client.Delete(req.Ctx, e, kclient.Preconditions{
96+
if err := req.Client.Delete(req.Ctx, req.Object, kclient.Preconditions{
8297
// Adding these preconditions prevents us from deleting an event based on old information.
8398
// e.g. The observed time has been updated and the event is no longer expired.
84-
UID: &e.UID,
85-
ResourceVersion: &e.ResourceVersion,
99+
UID: ptr(e.GetUID()),
100+
ResourceVersion: ptr(e.GetResourceVersion()),
86101
}); err != nil && !apierrors.IsNotFound(err) {
87102
// Assume any error other than not found is transient, return error to requeue w/ backoff
88103
return err
89104
}
90105

91106
return nil
92107
}
108+
109+
func ptr[T any](t T) *T {
110+
return &t
111+
}

0 commit comments

Comments
 (0)