Skip to content

Commit 366b022

Browse files
committed
fixup! feat(plz): pass new config from GCk6 to PLZ test runs
1 parent 2002929 commit 366b022

3 files changed

Lines changed: 49 additions & 18 deletions

File tree

pkg/cloud/types.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ var reservedGCk6EnvVars = map[string]struct{}{}
1818
const (
1919
// Reserved vars set for PLZ tests, as described here:
2020
// https://grafana.com/docs/grafana-cloud/testing/k6/author-run/cloud-scripting-extras/cloud-execution-context-variables/
21+
// These are not passed from GCk6, but set by k6-operator directly.
2122
lzCloudExecVar = "K6_CLOUDRUN_LOAD_ZONE"
2223
distrCloudExecVar = "K6_CLOUDRUN_DISTRIBUTION"
2324
trIDCloudExecVar = "K6_CLOUDRUN_TEST_RUN_ID"
24-
// it's exported as it must be set in external package, as part of TestRun CRD flow
25+
// IIDCloudExecVar is exported as it must be set in external package, as part of TestRun CRD flow
2526
IIDCloudExecVar = "K6_CLOUDRUN_INSTANCE_ID"
2627

2728
secretSourceEnvVar = "K6_SECRET_SOURCE"
@@ -96,14 +97,19 @@ type TestRunData struct {
9697
// LZDistribution holds label -> distribution mapping relevant
9798
// for the given script and PLZ
9899
LZDistribution `json:"load_zone_distribution,omitempty"`
100+
101+
// TagArgs and EnvArgs are sorted CLI argument strings, populated by Build().
102+
TagArgs string `json:"-"`
103+
EnvArgs string `json:"-"`
99104
}
100105

101106
func (trd *TestRunData) TestRunID() string {
102107
return fmt.Sprintf("%d", trd.TestRunId)
103108
}
104109

105-
// Build adds specific for GCk6 tags and env vars to data.
106-
// Returns error if it's impossible.
110+
// Build adds specific for GCk6 tags and env vars to data,
111+
// and produces sorted CLI argument strings for tags and environment.
112+
// Returns error if distribution is empty.
107113
func (trd *TestRunData) Build() error {
108114
if len(trd.LZDistribution) != 1 {
109115
return fmt.Errorf("only tests with one load zone are supported, provided: %+v", trd.LZDistribution)
@@ -119,9 +125,27 @@ func (trd *TestRunData) Build() error {
119125
trd.Environment[distrCloudExecVar] = trd.LZLabel()
120126
trd.Environment[trIDCloudExecVar] = trd.TestRunID()
121127

128+
trd.TagArgs = sortedArgs("--tag", trd.Tags)
129+
trd.EnvArgs = sortedArgs("-e", trd.Environment)
130+
122131
return nil
123132
}
124133

134+
// sortedArgs builds a CLI argument string from a map, sorted by key.
135+
func sortedArgs(flag string, m map[string]string) string {
136+
keys := make([]string, 0, len(m))
137+
for k := range m {
138+
keys = append(keys, k)
139+
}
140+
sort.Strings(keys)
141+
142+
var s string
143+
for _, k := range keys {
144+
s += " " + flag + " " + fmt.Sprintf(`%s=%s`, k, m[k])
145+
}
146+
return s
147+
}
148+
125149
type LZConfig struct {
126150
RunnerImage string `json:"load_runner_image,omitempty"`
127151
InstanceCount int `json:"instance_count,omitempty"`
@@ -145,9 +169,9 @@ type CLIArgs struct {
145169
UserAgent string `json:"user_agent,omitempty"`
146170
}
147171

148-
type LZDistribution map[string]distribution
172+
type LZDistribution map[string]Distribution
149173

150-
type distribution struct {
174+
type Distribution struct {
151175
LoadZone string `json:"loadZone"`
152176
Percent int `json:"percent"`
153177
}

pkg/plz/worker.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,12 @@ func (w *PLZWorker) complete(tr *v1alpha1.TestRun, trData *cloud.TestRunData) {
192192
// building argument list to k6
193193
bips := `--blacklist-ip="` + strings.Join(trData.BlacklistIPs, ",") + `"`
194194
bhns := `--block-hostnames="` + strings.Join(trData.BlockedHostnames, ",") + `"`
195-
var tags string
196-
for k, v := range trData.Tags {
197-
tags += " --tag " + fmt.Sprintf(`%s=%s`, k, v)
198-
}
199-
200-
var evs string
201-
for k, v := range trData.Environment {
202-
evs += " -e " + fmt.Sprintf(`%s=%s`, k, v)
203-
}
204-
205195
tr.Spec.Arguments = fmt.Sprintf(`--out cloud %s %s %s --no-thresholds --user-agent=%s --log-output=loki=https://cloudlogs.k6.io/api/v1/push,label.lz=%s,label.test_run_id=%s,header.Authorization="Token $(K6_CLOUD_TOKEN)" %s`,
206196
bips, bhns,
207-
tags, trData.UserAgent,
197+
trData.TagArgs, trData.UserAgent,
208198
w.plz.Name,
209199
trData.TestRunID(),
210-
evs)
200+
trData.EnvArgs)
211201

212202
if trData.IncludeSystemEnvVars {
213203
tr.Spec.Arguments += " --include-system-env-vars"
@@ -237,6 +227,11 @@ func (w *PLZWorker) handle(testRunId string) {
237227
w.logger.Error(err, fmt.Sprintf("Failed to retrieve test run data for `%s`", testRunId))
238228
return
239229
}
230+
if err = trData.Build(); err != nil {
231+
w.logger.Error(err, fmt.Sprintf("Failed to sort out test run data for `%s`", testRunId))
232+
return
233+
}
234+
240235
w.complete(tr, trData)
241236

242237
w.logger.Info(fmt.Sprintf("PLZ test run has been prepared with image `%s` and `%d` instances",

pkg/plz/worker_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ func Test_complete_correctDefinitionOfTestRun(t *testing.T) {
133133
"ENV": "VALUE",
134134
"foo": "bar",
135135
}
136+
someLZDistribution = cloud.LZDistribution{
137+
"some-label": cloud.Distribution{LoadZone: "some-zone", Percent: 100},
138+
}
136139
// podTemplate test values
137140
someAllowPrivEscalation = false
138141
someRunAsUser int64 = 1000
@@ -199,7 +202,11 @@ func Test_complete_correctDefinitionOfTestRun(t *testing.T) {
199202
cloudFieldsTestRun.Spec.Parallelism = int32(someInstances)
200203

201204
cloudEnvVarsTestRun = cloudFieldsTestRun // build up on top of cloud fields case
202-
cloudEnvVarsTestRun.Spec.Arguments += " -e ENV=VALUE -e foo=bar"
205+
cloudEnvVarsTestRun.Spec.Arguments = strings.Replace(cloudEnvVarsTestRun.Spec.Arguments,
206+
`--block-hostnames="" --no-thresholds`,
207+
`--block-hostnames="" --tag load_zone=some-label --no-thresholds`,
208+
1)
209+
cloudEnvVarsTestRun.Spec.Arguments += " -e ENV=VALUE -e K6_CLOUDRUN_DISTRIBUTION=some-label -e K6_CLOUDRUN_LOAD_ZONE=some-zone -e K6_CLOUDRUN_TEST_RUN_ID=6543 -e foo=bar"
203210

204211
podTemplateTolerationsTestRun = requiredFieldsTestRun
205212
podTemplateTolerationsTestRun.Spec.Runner.Tolerations = someTolerations
@@ -321,7 +328,11 @@ func Test_complete_correctDefinitionOfTestRun(t *testing.T) {
321328
InstanceCount: someInstances,
322329
ArchiveURL: someArchiveURL,
323330
Environment: someEnvVars,
331+
CLIArgs: cloud.CLIArgs{
332+
Tags: map[string]string{},
333+
},
324334
},
335+
LZDistribution: someLZDistribution,
325336
},
326337
ingestUrl: mainIngest,
327338
expected: &cloudEnvVarsTestRun,
@@ -448,6 +459,7 @@ func Test_complete_correctDefinitionOfTestRun(t *testing.T) {
448459
worker := NewPLZWorker(testCase.plz, "token", c, logr.Logger{})
449460

450461
tr := worker.template.Create()
462+
_ = testCase.cloudData.Build()
451463
worker.complete(tr, testCase.cloudData)
452464

453465
if diff := deep.Equal(tr, testCase.expected); diff != nil {

0 commit comments

Comments
 (0)