Skip to content

Commit e5bd333

Browse files
committed
add consistent logic on error with initializer spec
1 parent b73ae50 commit e5bd333

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

controllers/common.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import (
1919
"sigs.k8s.io/controller-runtime/pkg/client"
2020
)
2121

22+
const (
23+
errMessageTooLong = "Creation of %s takes too long: your configuration might be off. Check if %v were created successfully."
24+
)
25+
2226
// It may take some time to retrieve inspect output so indicate with boolean if it's ready
2327
// and use returnErr only for errors that require a change of behaviour. All other errors
2428
// should just be logged.

controllers/k6_initialize.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ func InitializeJobs(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI,
4444
return res, nil
4545
}
4646

47-
func RunValidations(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI, r *TestRunReconciler) (res ctrl.Result, err error) {
47+
func RunValidations(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI, r *TestRunReconciler) (
48+
res ctrl.Result, ready bool, err error) {
4849
// initializer is a quick job so check in frequently
4950
res = ctrl.Result{RequeueAfter: time.Second * 5}
5051

@@ -63,10 +64,10 @@ func RunValidations(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI,
6364
}
6465

6566
// inspectTestRun made a log message already so just return without requeue
66-
return ctrl.Result{}, nil
67+
return ctrl.Result{}, ready, err
6768
}
6869
if !inspectReady {
69-
return res, nil
70+
return res, ready, nil
7071
}
7172

7273
log.Info(fmt.Sprintf("k6 inspect: %+v", inspectOutput))
@@ -83,11 +84,11 @@ func RunValidations(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI,
8384
k6.GetStatus().Stage = "error"
8485

8586
if _, err := r.UpdateStatus(ctx, k6, log); err != nil {
86-
return ctrl.Result{}, err
87+
return ctrl.Result{}, ready, err
8788
}
8889

8990
// Don't requeue in case of this error; unless it's made into a warning as described above.
90-
return ctrl.Result{}, nil
91+
return ctrl.Result{}, ready, nil
9192
}
9293

9394
if cli.HasCloudOut {
@@ -104,10 +105,12 @@ func RunValidations(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI,
104105
}
105106

106107
if _, err := r.UpdateStatus(ctx, k6, log); err != nil {
107-
return ctrl.Result{}, err
108+
return ctrl.Result{}, ready, err
108109
}
109110

110-
return res, nil
111+
ready = true
112+
113+
return res, ready, nil
111114
}
112115

113116
// SetupCloudTest inspects the output of initializer and creates a new

controllers/k6_start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func StartJobs(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI, r *Te
7070
} else {
7171
// let's try this approach
7272
if time.Since(t).Minutes() > 5 {
73-
msg := "Creation of runner pods takes too long: your configuration might be off. Check if runner jobs and pods were created successfully."
73+
msg := fmt.Sprintf(errMessageTooLong, "runner pods", "runner jobs and pods")
7474
log.Info(msg)
7575

7676
if v1alpha1.IsTrue(k6, v1alpha1.CloudTestRun) {

controllers/testrun_controller.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package controllers
1616

1717
import (
1818
"context"
19+
"errors"
1920
"fmt"
2021
"time"
2122

@@ -129,8 +130,27 @@ func (r *TestRunReconciler) reconcile(ctx context.Context, req ctrl.Request, log
129130
return ctrl.Result{}, nil
130131

131132
case "initialization":
132-
if v1alpha1.IsUnknown(k6, v1alpha1.CloudTestRun) {
133-
return RunValidations(ctx, log, k6, r)
133+
res, ready, err := RunValidations(ctx, log, k6, r)
134+
if err != nil || !ready {
135+
if t, ok := v1alpha1.LastUpdate(k6, v1alpha1.TestRunRunning); !ok {
136+
// this should never happen
137+
return res, errors.New("Cannot find condition TestRunRunning")
138+
} else {
139+
// let's try this approach
140+
if time.Since(t).Minutes() > 5 {
141+
msg := fmt.Sprintf(errMessageTooLong, "initializer pod", "initializer job and pod")
142+
log.Info(msg)
143+
144+
if v1alpha1.IsTrue(k6, v1alpha1.CloudTestRun) {
145+
events := cloud.ErrorEvent(cloud.K6OperatorStartError).
146+
WithDetail(msg).
147+
WithAbort()
148+
cloud.SendTestRunEvents(r.k6CloudClient, v1alpha1.TestRunID(k6), log, events)
149+
}
150+
}
151+
}
152+
153+
return res, err
134154
}
135155

136156
if v1alpha1.IsFalse(k6, v1alpha1.CloudTestRun) {

0 commit comments

Comments
 (0)