Passing environment variables in init code on the k6 operator

I’m using the k6-operator on a Kubernetes cluster. My script uses environment variables to know how many VUs to use.

const VUS = __ENV.TEST_VUS || 10000;

export const options = {
    vus: VUS,
    duration: __ENV.TEST_DURATION || '300s',
    setupTimeout: '600s'
};

const sharedData = new SharedArray("user IDs and event IDs", function () {
    const arr = new Array(VUS);
    // ...

My config is this:

apiVersion: k6.io/v1alpha1
kind: K6
metadata:
  name: <...>
spec:
  parallelism: 4
  runner:
    # ...
  script:
    configMap:
      name: <...>
      file: archive.tar
  arguments: --include-system-env-vars --env TEST_VUS=10000 --env TEST_DURATION=300s

I generated a k8s configmap with

k6 archive --include-system-env-vars <my-file>

So, whatever I do, I cannot get the __ENV filled in during the init stage on k8s. It works fine locally.

Any suggestions? :upside_down_face:

Hi @whatyouhide

Welcome to the community forum :wave:

I haven’t tested this concrete case. However, have you tried passing environment variables the Kubernetes way? E.g.

# k6-resource-with-extensions.yml

apiVersion: k6.io/v1alpha1
kind: K6
metadata:
  name: k6-sample-with-extensions
spec:
  parallelism: 4
  runner:
    env:
      - name: TEST_VUS
        value: 10000

Let me know if that does not work and I’ll dig into it.

Cheers!

Hiya @eyeveebe :wave: Yep, sorry, I should have mentioned that. I tried the env: approach (a la k8s) as well, but no luck there either.

1 Like

Hi @whatyouhide

Thanks for clarifying. The person I want to talk to will be back this week, and we’ll get back to you.

Cheers!

Hi @whatyouhide,

The example posted by @eyeveebe above should work actually, with minor adjustments. I can access env vars in init context with the following:

apiVersion: k6.io/v1alpha1
kind: K6
metadata:
  name: k6-sample
spec:
  parallelism: 1
  script:
    configMap:
      name: "env-test"
      file: "archive.tar"
  runner:
    env:
      - name: TEST_VUS
        value: "4"

The script is:

import http from 'k6/http';
import { check } from 'k6';

const VUS = __ENV.TEST_VUS || 10000;

export let options = {
  stages: [
    { target: 200, duration: '30s' },
    { target: 0, duration: '30s' },
  ],
};

export default function () {
  console.log("VUS is", VUS)
  const result = http.get('https://test-api.k6.io/public/crocodiles/');
  check(result, {
    'http response status code is 200': result.status === 200,
  });
}

And ConfigMap was generated with k6 archive --include-system-env-vars test.js. Could you please try the same and describe the result?

1 Like