Skip to content

Commit

Permalink
fix: improve test startup logic (grafana#89)
Browse files Browse the repository at this point in the history
* fix: improve test startup logic

* fix: change usage to statusCode

* fix: avoid requesting when hostip is empty

Co-authored-by: yorugac <[email protected]>

* feat: change to service instead of pods

Co-authored-by: yorugac <[email protected]>
  • Loading branch information
victorlcm and yorugac authored Jan 19, 2022
1 parent d268e0e commit 969dc2d
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions controllers/k6_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"context"
"fmt"
"net/http"
"time"

"github.com/go-logr/logr"
Expand All @@ -15,6 +16,17 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

func isServiceReady(log logr.Logger, service *v1.Service) bool {
resp, err := http.Get(fmt.Sprintf("http://%v.%v.svc.cluster.local:6565/v1/status", service.ObjectMeta.Name, service.ObjectMeta.Namespace))

if err != nil {
log.Error(err, fmt.Sprintf("failed to get status from %v", service.ObjectMeta.Name))
return false
}

return resp.StatusCode < 400
}

// 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")
Expand Down Expand Up @@ -58,6 +70,11 @@ func StartJobs(ctx context.Context, log logr.Logger, k6 *v1alpha1.K6, r *K6Recon

for _, service := range sl.Items {
hostnames = append(hostnames, service.ObjectMeta.Name)

if !isServiceReady(log, &service) {
log.Info(fmt.Sprintf("%v service is not ready, aborting", service.ObjectMeta.Name))
return false, nil
}
}

starter := jobs.NewStarterJob(k6, hostnames)
Expand Down

0 comments on commit 969dc2d

Please sign in to comment.