forked from grafana/k6-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test execution synchronization and argument passing, and simplify…
… code
- Loading branch information
Showing
13 changed files
with
423 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,4 @@ metadata: | |
name: k6-sample | ||
spec: | ||
parallelism: 4 | ||
script: k6-test | ||
separate: false | ||
script: k6-test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/go-logr/logr" | ||
"github.com/k6io/operator/api/v1alpha1" | ||
"github.com/k6io/operator/pkg/resources/jobs" | ||
batchv1 "k8s.io/api/batch/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
) | ||
|
||
// CreateJobs that will spawn k6 pods, running distributed tests | ||
func CreateJobs(ctx context.Context, log logr.Logger, k6 *v1alpha1.K6, r *K6Reconciler) (ctrl.Result, error) { | ||
|
||
var err error | ||
var res ctrl.Result | ||
|
||
log.Info("Creating test jobs") | ||
|
||
if res, err = createJobSpecs(ctx, log, k6, r); err != nil { | ||
return res, err | ||
} | ||
|
||
k6.Status.Stage = "created" | ||
if err = r.Client.Status().Update(ctx, k6); err != nil { | ||
log.Error(err, "Could not update status of custom resource") | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
func createJobSpecs(ctx context.Context, log logr.Logger, k6 *v1alpha1.K6, r *K6Reconciler) (ctrl.Result, error) { | ||
found := &batchv1.Job{} | ||
namespacedName := types.NamespacedName{ | ||
Name: fmt.Sprintf("%s-1", k6.Name), | ||
Namespace: k6.Namespace, | ||
} | ||
|
||
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.") | ||
return ctrl.Result{}, err | ||
} | ||
|
||
for i := 1; i <= int(k6.Spec.Parallelism); i++ { | ||
if err := launchTest(ctx, k6, i, log, r); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
func launchTest(ctx context.Context, k6 *v1alpha1.K6, index int, log logr.Logger, r *K6Reconciler) error { | ||
var job *batchv1.Job | ||
var err error | ||
|
||
msg := fmt.Sprintf("Launching k6 test #%d", index) | ||
log.Info(msg) | ||
|
||
if job, err = jobs.NewRunnerJob(k6, index); err != nil { | ||
log.Error(err, "Failed to generate k6 test job") | ||
return err | ||
} | ||
|
||
if err = ctrl.SetControllerReference(k6, job, r.Scheme); err != nil { | ||
log.Error(err, "Failed to set controller reference for job") | ||
return err | ||
} | ||
|
||
if err = r.Create(ctx, job); err != nil { | ||
log.Error(err, "Failed to launch k6 test") | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/go-logr/logr" | ||
"github.com/k6io/operator/api/v1alpha1" | ||
"github.com/k6io/operator/pkg/resources/jobs" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"time" | ||
) | ||
|
||
// StartJobs in the Ready phase using a curl container | ||
func StartJobs(ctx context.Context, log logr.Logger, k6 *v1alpha1.K6, r *K6Reconciler) (ctrl.Result, error) { | ||
log.Info("Waiting for pods to get ready") | ||
|
||
err := wait.PollImmediate(time.Second*5, time.Second*60, func() (done bool, err error) { | ||
selector := labels.SelectorFromSet(map[string]string{ | ||
"app": "k6", | ||
"k6_cr": k6.Name, | ||
}) | ||
|
||
opts := &client.ListOptions{LabelSelector: selector} | ||
pl := &v1.PodList{} | ||
|
||
if e := r.List(ctx, pl, opts); e != nil { | ||
log.Error(e, "Could not list pods") | ||
return false, e | ||
} | ||
|
||
var count int | ||
for _, pod := range pl.Items { | ||
if pod.Status.Phase != "Running" { | ||
continue | ||
} | ||
count++ | ||
} | ||
|
||
log.Info(fmt.Sprintf("%d/%d pods ready", count, k6.Spec.Parallelism)) | ||
|
||
if count != int(k6.Spec.Parallelism) { | ||
return false, nil | ||
} | ||
|
||
var ips []string | ||
|
||
for _, pod := range pl.Items { | ||
ips = append(ips, pod.Status.PodIP) | ||
} | ||
|
||
starter := jobs.NewStarterJob(k6, ips) | ||
|
||
if err = ctrl.SetControllerReference(k6, starter, r.Scheme); err != nil { | ||
log.Error(err, "Failed to set controller reference for job") | ||
} | ||
|
||
if err = r.Create(ctx, starter); err != nil { | ||
log.Error(err, "Failed to launch k6 test starter") | ||
return true, err | ||
} | ||
|
||
return true, nil | ||
}) | ||
|
||
if err != nil { | ||
log.Error(err, "Failed to start all jobs") | ||
return ctrl.Result{}, err | ||
} | ||
|
||
k6.Status.Stage = "started" | ||
if err = r.Client.Status().Update(ctx, k6); err != nil { | ||
log.Error(err, "Could not update status of custom resource") | ||
return ctrl.Result{}, err | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.