Cannot access __ENV inside initializer pod itself and testing fails

Hi,
I have installed the k6 operator on our GKE cluster using the helm chart.
I am facing an issue where I am unable to use variables from ConfigMap inside the TestRun.
While I can console.log the env vars inside my function, I cannot access them inside export const options hence the test fails at initializer stage itself.

Here is the error:

level=error msg="could not initialize '/test/api.js': could not load JS test 'file:///test/api.js': strconv.ParseInt: parsing \"undefined\": invalid syntax"

Here is my script which is saved as the ConfigMap for TestRun:

apiVersion: v1
data:
  api.js: |
    import http from 'k6/http';
    import { check, fail } from 'k6';
    import { sleep } from 'k6';

    const url = 'https://url-to-test';
    let rt = `${__ENV.RATE}`;

    export const options = {
      scenarios: {
        constant_request_rate: {
          executor: 'constant-arrival-rate',
          rate: rt, // setting rate: `${__ENV.RATE}` does not work either 
          timeUnit: '1s', // 1000 iterations per second, i.e. 1000 RPS
          duration: '5s',
          preAllocatedVUs: 5, // how large the initial pool of VUs would be
          maxVUs: 10, // if the preAllocatedVUs are not enough, we can initialize more
        },
      }
    };

    export default function() {
      let res = http.get(url);
      console.log(res.json(), rt);

    }
kind: ConfigMap
metadata:
  name: api-cm-test
  namespace: podinfo

My ConfigMap with the Rate data:

apiVersion: v1
kind: ConfigMap
metadata:
  name: api-data-cm
  namespace: podinfo
data:
  RATE: '1'
  DURATION: 5s

And here is my TestRun config:

apiVersion: k6.io/v1alpha1
kind: TestRun
metadata:
  name: api-test
  namespace: podinfo
spec:
  parallelism: 4
  script:
    configMap:
      name: api-cm-test
      file: api-eve.js
  runner:
    # env: 
    #   - name: "RATE"
    #     value: '1'
    envFrom: 
      - secretRef:
          name: api-secret
      - configMapRef: 
          name: api-data-cm

Even tried the Kubernetes way as you can see from env: but this stlll does not work.

Hi @himanshu312001, welcome to the community forum!

There are multiple problems, but fortunately there is a solution also!

Your TestRun fails in the initializer stage, therefore the runner: section of the yaml has no effect for it. If you see the output of the kubectl explain TestRun.spec command, you can see, that there is an initializer section also.

So adding this initializer section to the TestRun should work, but it is NOT working for multiple reasons

  initializer:
    envFrom: 
      - configMapRef: 
          name: api-data-cm

The reasons, it is not working is the following:

  1. I don’t know why, but envFrom is not added to the PodSpec by the k6-operator when it creates the initializer pod: source code (note: It is added when it creates the Runner: source code) so we have to use the env instead of envFrom
  2. In the initializer pod, k6-operator runs a k6 inspect command (source code). And inspect command doesn’t get the environment variables, just the variables which were passed to the k6 inspect command with the -e flag : source code (note: run command gets all the environment variables: source code)

So the workaround for your problem could be the following modification in the TestRun yaml:

apiVersion: k6.io/v1alpha1
kind: TestRun
metadata:
  name: api-test
  namespace: podinfo
spec:
  parallelism: 4
  script:
    configMap:
      name: api-cm-test
      file: api-eve.js
  runner:
    envFrom: 
      - secretRef:
          name: api-secret
      - configMapRef: 
          name: api-data-cm
  initializer:
    env: 
      - name: "RATE"
        value: '1'
      - name: "K6_INCLUDE_SYSTEM_ENV_VARS"  # this enables environment variables for the initializer
        value: "1"
1 Like

Hi @bandorko
Thanks for the response. This worked for me! I have not seen this initializer block anywhere, will need to look more into this but this solved the error for me!

1 Like