-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add finished state change and clean up post execution
- Loading branch information
1 parent
b6c3258
commit 350ca49
Showing
5 changed files
with
72 additions
and
7 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -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 | ||
} |