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

Commit

Permalink
Add external aliasas for internal runtime event helper types
Browse files Browse the repository at this point in the history
Make it easier for third party services to record events without
importing internal packages by adding type aliases and wrapper functions
in our external API package.

Signed-off-by: Nick Hale <4175918+njhale@users.noreply.github.com>
  • Loading branch information
njhale committed Aug 2, 2023
1 parent 56da045 commit 5c51038
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 35 deletions.
47 changes: 47 additions & 0 deletions pkg/apis/api.acorn.io/v1/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package v1

import (
"time"

internalv1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type Event internalv1.EventInstance

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type EventList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Event `json:"items"`
}

// Alias helper types so that clients don't need to import the internal package when creating events.

// +k8s:deepcopy-gen=false

type EventResource = internalv1.EventResource

// +k8s:deepcopy-gen=false

type MicroTime = internalv1.MicroTime

func NowMicro() MicroTime {
return internalv1.NowMicro()
}

func NewMicroTime(t time.Time) MicroTime {
return internalv1.NewMicroTime(t)
}

const (
EventSeverityInfo = internalv1.EventSeverityInfo
EventSeverityError = internalv1.EventSeverityError
)

func Mapify(v any) (internalv1.GenericMap, error) {
return internalv1.Mapify(v)
}
12 changes: 0 additions & 12 deletions pkg/apis/api.acorn.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,18 +560,6 @@ type ImageAllowRuleList struct {

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type Event v1.EventInstance

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type EventList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Event `json:"items"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type DevSession v1.DevSessionInstance

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/appdefinition/pullappimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ func recordPullEvent(ctx context.Context, recorder event.Recorder, observed meta
previous, target := newImageSummary(previousImage), newImageSummary(targetImage)
e := apiv1.Event{
Type: AppImagePullSuccessEventType,
Severity: v1.EventSeverityInfo,
Severity: apiv1.EventSeverityInfo,
Description: fmt.Sprintf("Pulled %s", target.Name),
AppName: obj.GetName(),
Resource: event.Resource(obj),
Observed: v1.MicroTime(observed),
Observed: apiv1.MicroTime(observed),
}
e.SetNamespace(obj.GetNamespace())

Expand All @@ -231,7 +231,7 @@ func recordPullEvent(ctx context.Context, recorder event.Recorder, observed meta
details.Err = err.Error()
}

if e.Details, err = v1.Mapify(details); err != nil {
if e.Details, err = apiv1.Mapify(details); err != nil {
logrus.Warnf("Failed to mapify event details: %s", err.Error())
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func publicKind(obj runtime.Object) string {
}

// Resource returns a non-nil pointer to a v1.EventResource for the given object.
func Resource(obj kclient.Object) *internalv1.EventResource {
return &internalv1.EventResource{
func Resource(obj kclient.Object) *apiv1.EventResource {
return &apiv1.EventResource{
Kind: publicKind(obj),
Name: obj.GetName(),
UID: obj.GetUID(),
Expand Down
18 changes: 9 additions & 9 deletions pkg/server/registry/apigroups/acorn/apps/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (s *eventRecordingStrategy) Create(ctx context.Context, obj types.Object) (
return created, err
}

details, err := v1.Mapify(AppSpecCreateEventDetails{
details, err := apiv1.Mapify(AppSpecCreateEventDetails{
ResourceVersion: created.GetResourceVersion(),
})
if err != nil {
Expand All @@ -79,12 +79,12 @@ func (s *eventRecordingStrategy) Create(ctx context.Context, obj types.Object) (
Namespace: obj.GetNamespace(),
},
Type: AppCreateEventType,
Severity: v1.EventSeverityInfo,
Severity: apiv1.EventSeverityInfo,
Details: details,
Description: fmt.Sprintf("App %s/%s created", obj.GetNamespace(), obj.GetName()),
AppName: obj.GetName(),
Resource: event.Resource(obj),
Observed: v1.NowMicro(),
Observed: apiv1.NowMicro(),
}); err != nil {
logrus.Warnf("Failed to record event: %s", err.Error())
}
Expand All @@ -100,7 +100,7 @@ func (s *eventRecordingStrategy) Delete(ctx context.Context, obj types.Object) (
return deleted, err
}

details, err := v1.Mapify(AppSpecDeleteEventDetails{
details, err := apiv1.Mapify(AppSpecDeleteEventDetails{
ResourceVersion: deleted.GetResourceVersion(),
})
if err != nil {
Expand All @@ -113,12 +113,12 @@ func (s *eventRecordingStrategy) Delete(ctx context.Context, obj types.Object) (
Namespace: obj.GetNamespace(),
},
Type: AppDeleteEventType,
Severity: v1.EventSeverityInfo,
Severity: apiv1.EventSeverityInfo,
Details: details,
Description: fmt.Sprintf("App %s/%s deleted", obj.GetNamespace(), obj.GetName()),
AppName: obj.GetName(),
Resource: event.Resource(obj),
Observed: v1.NowMicro(),
Observed: apiv1.NowMicro(),
}); err != nil {
logrus.Warnf("Failed to record event: %s", err.Error())
}
Expand Down Expand Up @@ -153,7 +153,7 @@ func (s *eventRecordingStrategy) Update(ctx context.Context, obj types.Object) (
return updated, nil
}

details, err := v1.Mapify(AppSpecUpdateEventDetails{
details, err := apiv1.Mapify(AppSpecUpdateEventDetails{
ResourceVersion: updated.GetResourceVersion(),
OldSpec: oldSpec,
Patch: patch,
Expand All @@ -168,12 +168,12 @@ func (s *eventRecordingStrategy) Update(ctx context.Context, obj types.Object) (
Namespace: obj.GetNamespace(),
},
Type: AppSpecUpdateEventType,
Severity: v1.EventSeverityInfo,
Severity: apiv1.EventSeverityInfo,
Details: details,
Description: fmt.Sprintf("Spec field updated for App %s/%s", obj.GetNamespace(), obj.GetName()),
AppName: obj.GetName(),
Resource: event.Resource(obj),
Observed: v1.NowMicro(),
Observed: apiv1.NowMicro(),
}); err != nil {
logrus.Warnf("Failed to record event: %s", err)
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/server/registry/apigroups/acorn/events/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ type query struct {
prefix prefix

// since excludes events observed before it when not nil.
since *internalv1.MicroTime
since *apiv1.MicroTime

// until excludes events observed after it when not nil.
until *internalv1.MicroTime
until *apiv1.MicroTime
}

// filterChannel applies the query to every event received from unfiltered and forwards the result to filtered, if any.
Expand Down Expand Up @@ -154,7 +154,7 @@ func (q query) filterEvent(e watch.Event) *watch.Event {
return &e
}

func (q query) afterWindow(observation internalv1.MicroTime) bool {
func (q query) afterWindow(observation apiv1.MicroTime) bool {
if q.until == nil {
// Window includes all future events
return false
Expand All @@ -163,7 +163,7 @@ func (q query) afterWindow(observation internalv1.MicroTime) bool {
return observation.After(q.until.Time)
}

func (q query) beforeWindow(observation internalv1.MicroTime) bool {
func (q query) beforeWindow(observation apiv1.MicroTime) bool {
if q.since == nil {
// Window includes all existing events
return false
Expand Down Expand Up @@ -255,12 +255,12 @@ func stripQuery(opts storage.ListOptions) (q query, stripped storage.ListOptions
// 2. RFC3339; e.g. "2006-01-02T15:04:05Z07:00"
// 3. RFC3339Micro; e.g. "2006-01-02T15:04:05.999999Z07:00"
// 4. Unix timestamp; e.g. "1136239445"
func parseTimeBound(raw string, now internalv1.MicroTime) (*internalv1.MicroTime, error) {
func parseTimeBound(raw string, now apiv1.MicroTime) (*apiv1.MicroTime, error) {
// Try to parse raw as a duration string
var errs []error
duration, err := time.ParseDuration(raw)
if err == nil {
return z.Pointer(internalv1.NewMicroTime(now.Add(-1 * duration))), nil
return z.Pointer(apiv1.NewMicroTime(now.Add(-1 * duration))), nil
}
errs = append(errs, fmt.Errorf("%s is not a valid duration: %w", raw, err))

Expand All @@ -287,12 +287,12 @@ var supportedLayouts = []string{
"2006-01-02T15:04:05",
}

func parseTime(raw string) (*internalv1.MicroTime, error) {
func parseTime(raw string) (*apiv1.MicroTime, error) {
var errs []error
for _, layout := range supportedLayouts {
t, err := time.Parse(layout, raw)
if err == nil {
return z.Pointer(internalv1.NewMicroTime(t)), nil
return z.Pointer(apiv1.NewMicroTime(t)), nil
}

errs = append(errs, err)
Expand All @@ -307,7 +307,7 @@ func parseUnix(raw string) (*internalv1.MicroTime, error) {
return nil, err
}

return z.Pointer(internalv1.NewMicroTime(time.Unix(sec, 0))), nil
return z.Pointer(apiv1.NewMicroTime(time.Unix(sec, 0))), nil
}

type prefix string
Expand Down

0 comments on commit 5c51038

Please sign in to comment.