Skip to content

Commit

Permalink
cloud, plz: add public detail and abort to events
Browse files Browse the repository at this point in the history
  • Loading branch information
yorugac committed Oct 31, 2023
1 parent 6698739 commit 3d79148
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
19 changes: 9 additions & 10 deletions controllers/k6_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,8 @@ func CreateJobs(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI, r *T
token, tokenReady, err = loadToken(ctx, log, r.Client, k6.GetSpec().Token, sOpts)
if err != nil {
// An error here means a very likely mis-configuration of the token.
// Consider updating status to error to let a user know quicker?
// TODO: update status to error to let a user know quicker
log.Error(err, "A problem while getting token.")

if v1alpha1.IsTrue(k6, v1alpha1.CloudTestRun) {
events := cloud.ErrorEvent(cloud.K6OperatorStartError).WithDetail(fmt.Sprintf("Failed to retrieve token: %v", err))
cloud.SendTestRunEvents(r.k6CloudClient, k6.GetSpec().TestRunID, log, events)
}

return ctrl.Result{}, nil
}
if !tokenReady {
Expand All @@ -58,9 +52,10 @@ func CreateJobs(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI, r *T
log.Info("Creating test jobs")

if res, err = createJobSpecs(ctx, log, k6, r, token); err != nil {

if v1alpha1.IsTrue(k6, v1alpha1.CloudTestRun) {
events := cloud.ErrorEvent(cloud.K6OperatorStartError).WithDetail(fmt.Sprintf("Failed to create runner jobs: %v", err))
events := cloud.ErrorEvent(cloud.K6OperatorStartError).
WithDetail(fmt.Sprintf("Failed to create runner jobs: %v", err)).
WithAbort()
cloud.SendTestRunEvents(r.k6CloudClient, k6.GetSpec().TestRunID, log, events)
}

Expand All @@ -86,7 +81,11 @@ func createJobSpecs(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI,
}

if err := r.Get(ctx, namespacedName, found); err == nil || !errors.IsNotFound(err) {
log.Info("Could not start a new test, Make sure you've deleted your previous run.")
if err == nil {
err = fmt.Errorf("job with the name %s exists; make sure you've deleted your previous run", namespacedName.Name)
}

log.Info(err.Error())
return ctrl.Result{}, err
}

Expand Down
46 changes: 25 additions & 21 deletions pkg/cloud/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ type EventPayload struct {
}

type Event struct {
ErrorCode `json:"error_code,omitempty"`
Detail string `json:"error_detail,omitempty"`
Origin `json:"origin,omitempty"`
Reason string `json:"reason,omitempty"`
ErrorCode `json:"error_code,omitempty"`

// reason is used for abort events,
// while details are for any non-abort event
Reason string `json:"reason,omitempty"`
Detail string `json:"error_detail,omitempty"`
PublicDetail string `json:"error_detail_public,omitempty"`
}

type EventType string
Expand Down Expand Up @@ -111,7 +115,8 @@ var (
OriginK6 = Origin("k6")
)

// WithDetail sets detail only for the 1st event
// WithDetail sets detail only for the 1st event.
// If it's abort, WithDetail sets reason field.
func (e *Events) WithDetail(s string) *Events {
if len(*e) == 0 {
return e
Expand All @@ -121,32 +126,31 @@ func (e *Events) WithDetail(s string) *Events {
(*e)[0].Reason = s
} else {
(*e)[0].Detail = s
(*e)[0].PublicDetail = s
}
return e
}

// WithAbort adds abortEvent if errorEvent already exists
// func (e *Events) WithAbort(s string) *Events {
// if len(*e) == 0 {
// return
// }

// if (*e)[0].EventType == errorEvent {
// ae := EventPayload{
// EventType: abort,
// }
// }
// return e
// }
// WithAbort adds abortEvent to errorEvent if it already exists.
func (e *Events) WithAbort() *Events {
if len(*e) == 0 {
return e
}

func AbortEvent(o Origin) *Events {
e := Events([]*EventPayload{{
if (*e)[0].EventType == errorEvent {
*e = append(*e, AbortEvent(OriginUser))
}
return e
}

func AbortEvent(o Origin) *EventPayload {
e := &EventPayload{
EventType: abortEvent,
Event: Event{
Origin: o,
},
}})
return &e
}
return e
}

func ErrorEvent(ec ErrorCode) *Events {
Expand Down

0 comments on commit 3d79148

Please sign in to comment.