K6-operator - pass local environment variable as test-wide tag

When running k6 using the K6 operator, I want to pass some test wide tags using the CLI.
The tag value is an environment variable in the pod.

How would I do that?

What I’ve tried:

add to the k6 archive command --tag pod=$HOSTNAME
add to the spec.arguments command --tag pod=$HOSTNAME didnt play out as well.

how’d you suggest doing this?

Hi @nocgod,
Welcome to the forum! :wave:
In case of k6-operator’s usage, test-wide tags can be passed as part of arguments field. Options struct in JS generally works with k6 too but has tricks described in this issue.

I’m not sure what you meant by “adding to k6 archive command” but your second attempt should have worked, so long as env var substitution happened correctly:

spec.arguments command --tag pod=$HOSTNAME didnt play out as well.

What do you mean by “didn’t play out well”?

IIRC it wasn’t interpolated into the command string, I had the $HOSTNAME as value for the pod tag :slight_smile:

I needed it to implement some logic (see metrics per pod) which was not available in 0.0.7, but I’ve since updated to 0.0.8-rc3 which has the tags I needed instance_id and job_name.

Got it, that makes sense. Glad to hear that you resolved it with built-in tags :slightly_smiling_face:

Your question made me double-check what Kubernetes is supposed to support in such cases and it seems the “standard” way to pass env vars to Pod is with specific format:

So something like this:

arguments: --tag pod="$(HOSTNAME)"
runner:
  env:
      - name: HOSTNAME
        value: ...

And this format isn’t possible in k6-operator, due to escaped double quotes causing a k6 error :thinking: This is not easy to fix at the moment and I’m inclined to treat this as a potential feature request - will open an issue. Thanks for your question and for helping to locate this!

awesome! glad to have helped, I’m really happy with k6 and k6-operator, running load tests on our (somewhat crappy) gateway trying to make it better.

I’m closing this question as resolved :slight_smile:

1 Like