K6: colocated data file

My k6 test script uses a rather long list of domains and picks in it at random.
This list is in a file which should be colocated to the script.

How to achieve this with a custom resource? The script is (following the documentation example) in a config map under ‘script’ – which is not suitable for the data file.


I assume you are using the k6 operator and your dataset is too large for the config map? If not could you expand on what you mean by unsuitable for the data file.

What you could do is build an image from the default runner image and copy in the dataset. You can then reference this image in spec.runner.image in the K6 custom resource.

1 Like

Actually, there was no issue of size… My ‘unsuitable’ only came from errors about the syntax of the data file not matching the ‘script’ expectation, as it was added to the same configmap:

msg="could not initialize '/test/opendns-top-domains.txt': 
     could not load JS test 'file:///test/opendns-top-domains.txt':
     SyntaxError: file:///test/opendns-top-domains.txt:
     Identifier directly after number (18:1)

I can try the spec.runner structure… but at least this fails:

  parallelism: 2
    file: opendns-top-domains.txt


error: error validating "/home/mgirod@azdsthq.net/git/titanhq/dev/cat/vade/k6/custom-resource.yml": error validating data: ValidationError(K6.spec.runner): unknown field "file" in io.k6.v1alpha1.K6.spec.runner; if you choose to ignore these errors, turn validation off with --validate=false

Can I insert just my text file into an image?

~> docker build -t domains .
Sending build context to Docker daemon  138.2kB
Step 1/2 : FROM scratch
Step 2/2 : ADD opendns-top-domains.txt /test
 ---> c2cb9f371412
Successfully built c2cb9f371412
Successfully tagged domains:latest

Not sure what version of the operator you are using, but I don’t think that K6.spec.runner.file is a valid field looking at the code: https://github.com/grafana/k6-operator/blob/main/api/v1alpha1/k6_types.go

I think what you need to do is something like this:

  1. Create a configmap containing your script and data
kubectl create cm \
    --from-file script.js=script.js \
    --from-file opendns-top-domains.txt=opendns-top-domains.txt \
  1. Specify the script file in K6.spec.script
    file: script.js 
    name: load-test-data
# both files in the configmap will get mounted in the runner pods at /test
# K6.spec.script.configMap.file specifies the JS entrypoint to the runner
  1. Open the file in the load test script like so:
var data = open("./opendns-top-domains.txt")
1 Like

Many Thanks! It worked beautifully.