From 60b6526c8802fea4b9f99077bd540a36f6cb8e7c Mon Sep 17 00:00:00 2001 From: Olha Yevtushenko Date: Thu, 26 Sep 2024 19:10:22 +0300 Subject: [PATCH] plz: add cloud logs configuration by default (temporary) --- controllers/plz_controller.go | 2 ++ controllers/plz_factory.go | 2 +- pkg/testrun/plz.go | 9 ++++++--- pkg/testrun/plz_test.go | 9 +++++++-- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/controllers/plz_controller.go b/controllers/plz_controller.go index 92e70e93..fc61c559 100644 --- a/controllers/plz_controller.go +++ b/controllers/plz_controller.go @@ -55,6 +55,7 @@ type PrivateLoadZoneReconciler struct { // poller should be made PLZ specific; // e.g. with a map: PLZ name -> poller. poller *cloud.TestRunPoller + token string // needed for cloud logs } //+kubebuilder:rbac:groups=k6.io,resources=privateloadzones,verbs=get;list;watch;create;update;patch;delete @@ -88,6 +89,7 @@ func (r *PrivateLoadZoneReconciler) Reconcile(ctx context.Context, req ctrl.Requ } r.poller = cloud.NewTestRunPoller(cloud.ApiURL(k6CloudHost()), token, plz.Name, logger) + r.token = token } if plz.DeletionTimestamp.IsZero() && (plz.IsUnknown(v1alpha1.PLZRegistered) || plz.IsFalse(v1alpha1.PLZRegistered)) { diff --git a/controllers/plz_factory.go b/controllers/plz_factory.go index 35096b44..a145f446 100644 --- a/controllers/plz_factory.go +++ b/controllers/plz_factory.go @@ -41,7 +41,7 @@ func (r *PrivateLoadZoneReconciler) startFactory(plz *v1alpha1.PrivateLoadZone, continue } - k6 = testrun.NewPLZTestRun(plz, trData, k6CloudHost()) + k6 = testrun.NewPLZTestRun(plz, r.token, trData, k6CloudHost()) logger.Info(fmt.Sprintf("PLZ test run has been prepared with image `%s` and `%d` instances", k6.Spec.Runner.Image, k6.Spec.Parallelism), "testRunId", testRunId) diff --git a/pkg/testrun/plz.go b/pkg/testrun/plz.go index bbe42ef7..a40727ac 100644 --- a/pkg/testrun/plz.go +++ b/pkg/testrun/plz.go @@ -16,7 +16,7 @@ func TestName(testRunId string) string { } // ingestURL is a temp hack -func NewPLZTestRun(plz *v1alpha1.PrivateLoadZone, trData *cloud.TestRunData, ingestUrl string) *v1alpha1.TestRun { +func NewPLZTestRun(plz *v1alpha1.PrivateLoadZone, token string, trData *cloud.TestRunData, ingestUrl string) *v1alpha1.TestRun { volume := corev1.Volume{ Name: "archive-volume", VolumeSource: corev1.VolumeSource{ @@ -72,8 +72,11 @@ func NewPLZTestRun(plz *v1alpha1.PrivateLoadZone, trData *cloud.TestRunData, ing }, Parallelism: int32(trData.InstanceCount), Separate: false, - Arguments: "--out cloud --no-thresholds", - Cleanup: v1alpha1.Cleanup("post"), + Arguments: fmt.Sprintf(`--out cloud --no-thresholds --log-output=loki=https://cloudlogs.k6.io/api/v1/push,label.lz=%s,label.test_run_id=%s,header.Authorization="Token %s"`, + plz.Name, + trData.TestRunID(), + token), + Cleanup: v1alpha1.Cleanup("post"), TestRunID: trData.TestRunID(), Token: plz.Spec.Token, diff --git a/pkg/testrun/plz_test.go b/pkg/testrun/plz_test.go index 0cdebef7..e72dd969 100644 --- a/pkg/testrun/plz_test.go +++ b/pkg/testrun/plz_test.go @@ -2,6 +2,7 @@ package testrun import ( "fmt" + "strings" "testing" "github.com/go-test/deep" @@ -53,7 +54,7 @@ func Test_NewPLZTestRun(t *testing.T) { }, Parallelism: int32(0), Separate: false, - Arguments: "--out cloud --no-thresholds", + Arguments: "--out cloud --no-thresholds --log-output=loki=https://cloudlogs.k6.io/api/v1/push,label.lz=,label.test_run_id=0,header.Authorization=\"Token token\"", Cleanup: v1alpha1.Cleanup("post"), TestRunID: "0", @@ -101,6 +102,10 @@ func Test_NewPLZTestRun(t *testing.T) { cloudFieldsTestRun = requiredFieldsTestRun // build up on top of required field case cloudFieldsTestRun.ObjectMeta.Name = TestName(fmt.Sprintf("%d", someTestRunID)) cloudFieldsTestRun.Spec.TestRunID = fmt.Sprintf("%d", someTestRunID) + cloudFieldsTestRun.Spec.Arguments = strings.Replace(requiredFieldsTestRun.Spec.Arguments, + "test_run_id=0", + fmt.Sprintf("test_run_id=%d", someTestRunID), + 1) cloudFieldsTestRun.Spec.Runner.InitContainers = []v1alpha1.InitContainer{ containers.NewS3InitContainer( someArchiveURL, @@ -223,7 +228,7 @@ func Test_NewPLZTestRun(t *testing.T) { testCase := testCase t.Run(testCase.name, func(t *testing.T) { t.Parallel() - got := NewPLZTestRun(testCase.plz, testCase.cloudData, testCase.ingestUrl) + got := NewPLZTestRun(testCase.plz, "token", testCase.cloudData, testCase.ingestUrl) if diff := deep.Equal(got, testCase.expected); diff != nil { t.Errorf("NewPLZTestRun returned unexpected data, diff: %s", diff) }