Skip to content

Commit

Permalink
Add finished state change and clean up post execution
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-groux-hs authored and yorugac committed Feb 11, 2022
1 parent b6c3258 commit 350ca49
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
12 changes: 7 additions & 5 deletions api/v1alpha1/k6_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type K6Spec struct {
Quiet string `json:"quiet,omitempty"`
Paused string `json:"paused,omitempty"`
Scuttle K6Scuttle `json:"scuttle,omitempty"`
// Cleanup Cleanup `json:"cleanup,omitempty"` // TODO
Cleanup Cleanup `json:"cleanup,omitempty"`
}

// K6Script describes where the script to execute the tests is found
Expand All @@ -82,12 +82,14 @@ type K6Configmap struct {
File string `json:"file,omitempty"`
}

// Cleanup allows for automatic cleanup of resources pre or post execution
// +kubebuilder:validation:Enum=pre;post
// type Cleanup string
//TODO: cleanup pre-execution?

// Cleanup allows for automatic cleanup of resources post execution
// +kubebuilder:validation:Enum=post
type Cleanup string

// Stage describes which stage of the test execution lifecycle our runners are in
// +kubebuilder:validation:Enum=created;started
// +kubebuilder:validation:Enum=created;started;finished
type Stage string

// K6Status defines the observed state of K6
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions config/crd/bases/k6.io_k6s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ spec:
properties:
arguments:
type: string
cleanup:
description: Cleanup allows for automatic cleanup of resources post
execution
enum:
- post
type: string
parallelism:
format: int32
type: integer
Expand Down Expand Up @@ -1694,6 +1700,7 @@ spec:
enum:
- created
- started
- finished
type: string
type: object
type: object
Expand Down
9 changes: 7 additions & 2 deletions controllers/k6_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/go-logr/logr"
"github.com/grafana/k6-operator/api/v1alpha1"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -61,10 +62,13 @@ func (r *K6Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
case "created":
return StartJobs(ctx, log, k6, r)
case "started":
// wait for test to finish and then mark as finished
return ctrl.Result{}, nil
return ReconcileJobs(ctx, log, k6, r)
case "finished":
// delete if configured
if k6.Spec.Cleanup == "post" {
log.Info("Cleaning up all resources")
r.Delete(ctx, k6)
}
// notify if configured
return ctrl.Result{}, nil
}
Expand All @@ -78,5 +82,6 @@ func (r *K6Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
func (r *K6Reconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.K6{}).
Owns(&batchv1.Job{}).
Complete(r)
}
50 changes: 50 additions & 0 deletions controllers/k6_reconcile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package controllers

import (
"context"
"fmt"

"github.com/go-logr/logr"
"github.com/grafana/k6-operator/api/v1alpha1"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/labels"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// Reconcile k6 status with job status
func ReconcileJobs(ctx context.Context, log logr.Logger, k6 *v1alpha1.K6, r *K6Reconciler) (ctrl.Result, error) {
selector := labels.SelectorFromSet(map[string]string{
"app": "k6",
"k6_cr": k6.Name,
})

opts := &client.ListOptions{LabelSelector: selector, Namespace: k6.Namespace}
jl := &batchv1.JobList{}

if err := r.List(ctx, jl, opts); err != nil {
log.Error(err, "Could not list jobs")
return ctrl.Result{}, err
}

//TODO: We should distinguish between suceeded/failed
var finished int32
for _, job := range jl.Items {
if job.Status.Active != 0 {
continue
}
finished++
}

log.Info(fmt.Sprintf("%d/%d jobs complete", finished, k6.Spec.Parallelism+1))

// parallelism (pods) + starter pod = total expected
if finished == k6.Spec.Parallelism+1 {
k6.Status.Stage = "finished"
if err := r.Client.Status().Update(ctx, k6); err != nil {
log.Error(err, "Could not update status of custom resource")
}
}

return ctrl.Result{}, nil
}

0 comments on commit 350ca49

Please sign in to comment.