Skip to content

Commit

Permalink
make setup into a unique call
Browse files Browse the repository at this point in the history
  • Loading branch information
yorugac committed Jan 17, 2024
1 parent 3118657 commit f954964
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
20 changes: 20 additions & 0 deletions controllers/k6_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/grafana/k6-operator/api/v1alpha1"
"github.com/grafana/k6-operator/pkg/cloud"
"github.com/grafana/k6-operator/pkg/resources/jobs"
"github.com/grafana/k6-operator/pkg/testrun"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -85,6 +86,8 @@ func StartJobs(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI, r *Te
return res, nil
}

// services

var hostnames []string
sl := &v1.ServiceList{}

Expand All @@ -104,6 +107,23 @@ func StartJobs(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI, r *Te
}
}

// setup

log.Info("Invoking setup() on the first runner")

setupData, err := testrun.RunSetup(ctx, hostnames[0])
if err != nil {
return ctrl.Result{}, err
}

log.Info("Sending setup data to the runners")

if err = testrun.SetSetupData(ctx, hostnames, setupData); err != nil {
return ctrl.Result{}, err
}

// starter

starter := jobs.NewStarterJob(k6, hostnames)

if err = ctrl.SetControllerReference(k6, starter, r.Scheme); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/resources/jobs/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func NewRunnerJob(k6 v1alpha1.TestRunI, index int, token string) (*batchv1.Job,
command = append(command, "--paused")
}

command = append(command, "--no-setup")

// Add an instance tag: in case metrics are stored, they need to be distinguished by instance
command = append(command, "--tag", fmt.Sprintf("instance_id=%d", index))

Expand Down
62 changes: 62 additions & 0 deletions pkg/testrun/k6client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package testrun

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"

"github.com/grafana/k6-operator/pkg/types"
k6Client "go.k6.io/k6/api/v1/client"
)

// This will probably be removed once distributed mode in k6 is implemented.

func RunSetup(ctx context.Context, hostname string) (_ json.RawMessage, err error) {
c, err := k6Client.New(fmt.Sprintf("%v:6565", hostname), k6Client.WithHTTPClient(&http.Client{
Timeout: 0,
}))

var response types.SetupData
if err := c.CallAPI(ctx, "POST", &url.URL{Path: "/v1/setup"}, nil, &response); err != nil {
return nil, err
}

if response.Data.Attributes.Data != nil {
var tmpSetupDataObj interface{}
if err := json.Unmarshal(response.Data.Attributes.Data, &tmpSetupDataObj); err != nil {
return nil, err
}
}

return response.Data.Attributes.Data, nil
}

func SetSetupData(ctx context.Context, hostnames []string, data json.RawMessage) (err error) {
for _, hostname := range hostnames {
c, err := k6Client.New(fmt.Sprintf("%v:6565", hostname), k6Client.WithHTTPClient(&http.Client{
Timeout: 0,
}))
if err != nil {
return err
}

if err = c.CallAPI(ctx, "PUT", &url.URL{Path: "/v1/setup"}, data, nil); err != nil {
return err
}
}

return nil
}

func RunTeardown(ctx context.Context, hostname string, data json.RawMessage) (err error) {
c, err := k6Client.New(fmt.Sprintf("%v:6565", hostname), k6Client.WithHTTPClient(&http.Client{
Timeout: 0,
}))
if err != nil {
return
}

return c.CallAPI(ctx, "POST", &url.URL{Path: "/v1/teardown"}, nil, nil)
}
16 changes: 16 additions & 0 deletions pkg/types/k6status.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package types

import "encoding/json"

// k6 REST API types.
// TODO: refactor with existing definitions in k6 api/v1?

Expand All @@ -17,3 +19,17 @@ type StatusAPIRequestDataAttributes struct {
Paused bool `json:"paused"`
Stopped bool `json:"stopped"`
}

type SetupData struct {
Data setUpData `json:"data"`
}

type setUpData struct {
Type string `json:"type"`
ID string `json:"id"`
Attributes setupResponseAttributes `json:"attributes"`
}

type setupResponseAttributes struct {
Data json.RawMessage `json:"data"`
}

0 comments on commit f954964

Please sign in to comment.