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

Commit 0a30e5c

Browse files
authored
Merge pull request #1927 from njhale/events/default-actor
Add server-side defaults for event fields
2 parents dd6cf7a + 193ab54 commit 0a30e5c

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ type EventInstance struct {
4343
Description string `json:"description,omitempty"`
4444

4545
// Observed represents the time the Event was first observed.
46-
Observed MicroTime `json:"observed" wrangler:"type=string"`
46+
// +optional
47+
Observed MicroTime `json:"observed,omitempty" wrangler:"type=string"`
4748

4849
// Details provides additional information about the cluster at the time the Event occurred.
4950
//

pkg/event/event.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import (
66

77
apiv1 "github.com/acorn-io/runtime/pkg/apis/api.acorn.io/v1"
88
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
9-
"github.com/sirupsen/logrus"
109
"k8s.io/apimachinery/pkg/runtime"
11-
"k8s.io/apiserver/pkg/endpoints/request"
1210
kclient "sigs.k8s.io/controller-runtime/pkg/client"
1311
)
1412

@@ -24,16 +22,6 @@ func (r RecorderFunc) Record(ctx context.Context, e *apiv1.Event) error {
2422

2523
func NewRecorder(c kclient.Client) RecorderFunc {
2624
return func(ctx context.Context, e *apiv1.Event) error {
27-
if e.Actor == "" {
28-
// Set actor from ctx if possible
29-
logrus.Debug("No Actor set, attempting to set default from ctx")
30-
if user, ok := request.UserFrom(ctx); ok {
31-
e.Actor = user.GetName()
32-
} else {
33-
logrus.Debug("Ctx has no user info, generating anonymous event")
34-
}
35-
}
36-
3725
// Set a generated name based on the event content.
3826
id, err := ContentID(e)
3927
if err != nil {

pkg/event/id.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ func ContentID(e *apiv1.Event) (string, error) {
1515
fieldSet := strings.Join([]string{
1616
e.Type,
1717
string(e.Severity),
18-
e.Actor,
1918
e.Source.String(),
2019
e.Description,
2120
strconv.FormatInt(e.Observed.UnixMicro(), 10),

pkg/openapi/generated/openapi_generated.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/server/registry/apigroups/acorn/events/strategy.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,27 @@ import (
99
"github.com/acorn-io/mink/pkg/strategy"
1010
"github.com/acorn-io/mink/pkg/types"
1111
apiv1 "github.com/acorn-io/runtime/pkg/apis/api.acorn.io/v1"
12+
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
1213
"github.com/acorn-io/runtime/pkg/channels"
1314
"github.com/sirupsen/logrus"
15+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1416
"k8s.io/apimachinery/pkg/watch"
17+
"k8s.io/apiserver/pkg/endpoints/request"
1518
"k8s.io/apiserver/pkg/storage"
1619
)
1720

1821
type eventStrategy struct {
1922
strategy.CompleteStrategy
2023
}
2124

25+
func (s *eventStrategy) Create(ctx context.Context, obj types.Object) (types.Object, error) {
26+
return s.CompleteStrategy.Create(ctx, setDefaults(ctx, obj.(*apiv1.Event)))
27+
}
28+
29+
func (s *eventStrategy) Update(ctx context.Context, obj types.Object) (types.Object, error) {
30+
return s.CompleteStrategy.Update(ctx, setDefaults(ctx, obj.(*apiv1.Event)))
31+
}
32+
2233
func (s *eventStrategy) Watch(ctx context.Context, namespace string, opts storage.ListOptions) (<-chan watch.Event, error) {
2334
// Unmarshal custom field selectors and strip them from the filter options before
2435
// passing to lower-level strategies (that don't support them).
@@ -60,6 +71,24 @@ func (s *eventStrategy) List(ctx context.Context, namespace string, opts storage
6071
return q.filterList(unfiltered.(*apiv1.EventList)), nil
6172
}
6273

74+
func setDefaults(ctx context.Context, e *apiv1.Event) *apiv1.Event {
75+
if e.Actor == "" {
76+
// Set actor from ctx if possible
77+
logrus.Debug("No Actor set, attempting to set default from request context")
78+
if user, ok := request.UserFrom(ctx); ok {
79+
e.Actor = user.GetName()
80+
} else {
81+
logrus.Debug("Request context has no user info, creating anonymous event")
82+
}
83+
}
84+
85+
if e.Observed.IsZero() {
86+
e.Observed = v1.MicroTime(metav1.NowMicro())
87+
}
88+
89+
return e
90+
}
91+
6392
type query struct {
6493
// tail when > 0, determines the number of latest events to return.
6594
tail int64

0 commit comments

Comments
 (0)