Skip to content

Commit

Permalink
fix: logic for error on duplicate job
Browse files Browse the repository at this point in the history
  • Loading branch information
yorugac committed Feb 2, 2024
1 parent b1a5e24 commit 0561eb1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
1 change: 0 additions & 1 deletion api/v1alpha1/k6conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func Initialize(k6 TestRunI) {

// PLZ test run case
if len(k6.GetSpec().TestRunID) > 0 {
UpdateCondition(k6, CloudTestRun, metav1.ConditionTrue)
UpdateCondition(k6, CloudPLZTestRun, metav1.ConditionTrue)
UpdateCondition(k6, CloudTestRunCreated, metav1.ConditionTrue)
UpdateCondition(k6, CloudTestRunFinalized, metav1.ConditionFalse)
Expand Down
6 changes: 4 additions & 2 deletions controllers/k6_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ func createJobSpecs(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI,
log.Info(err.Error())

// is it possible to implement this delay with resourceVersion of the job?

t, condUpdated := v1alpha1.LastUpdate(k6, v1alpha1.CloudTestRun)
// if condition has not been updated yet or has been updated very recently
if !condUpdated || time.Since(t).Seconds() <= 30 {
// If condition is unknown then resource hasn't been updated with `k6 inspect` results.
// If it has been updated but very recently, wait a bit before throwing an error.
if v1alpha1.IsUnknown(k6, v1alpha1.CloudTestRun) || !condUpdated || time.Since(t).Seconds() <= 30 {
// try again before returning an error
return ctrl.Result{RequeueAfter: time.Second * 10}, true, nil
}
Expand Down
10 changes: 7 additions & 3 deletions controllers/testrun_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ func (r *TestRunReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return r.reconcile(ctx, req, log, k6)
}

func isCloudTestRun(k6 v1alpha1.TestRunI) bool {
return v1alpha1.IsTrue(k6, v1alpha1.CloudTestRun) || v1alpha1.IsTrue(k6, v1alpha1.CloudPLZTestRun)
}

func (r *TestRunReconciler) reconcile(ctx context.Context, req ctrl.Request, log logr.Logger, k6 v1alpha1.TestRunI) (ctrl.Result, error) {
var err error
if v1alpha1.IsTrue(k6, v1alpha1.CloudTestRun) {
if isCloudTestRun(k6) {
// bootstrap the client
found, err := r.createClient(ctx, k6, log)
if err != nil {
Expand All @@ -100,7 +104,7 @@ func (r *TestRunReconciler) reconcile(ctx context.Context, req ctrl.Request, log
// Decision making here is now a mix between stages and conditions.
// TODO: refactor further.

if v1alpha1.IsTrue(k6, v1alpha1.CloudTestRun) && v1alpha1.IsFalse(k6, v1alpha1.CloudTestRunAborted) {
if isCloudTestRun(k6) && v1alpha1.IsFalse(k6, v1alpha1.CloudTestRunAborted) {
// check in with the BE for status
if r.ShouldAbort(ctx, k6, log) {
log.Info("Received an abort signal from the k6 Cloud: stopping the test.")
Expand Down Expand Up @@ -141,7 +145,7 @@ func (r *TestRunReconciler) reconcile(ctx context.Context, req ctrl.Request, log
msg := fmt.Sprintf(errMessageTooLong, "initializer pod", "initializer job and pod")
log.Info(msg)

if v1alpha1.IsTrue(k6, v1alpha1.CloudTestRun) {
if isCloudTestRun(k6) {
events := cloud.ErrorEvent(cloud.K6OperatorStartError).
WithDetail(msg).
WithAbort()
Expand Down

0 comments on commit 0561eb1

Please sign in to comment.