Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add finished state change and clean up post execution #79

Merged
merged 2 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -81,12 +81,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 @@ -1692,6 +1698,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)
brian-groux-hs marked this conversation as resolved.
Show resolved Hide resolved
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
brian-groux-hs marked this conversation as resolved.
Show resolved Hide resolved
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
}