devfile / devworkspace-operator

Apache License 2.0
59 stars 49 forks source link

User-provided environment variables can't reference $PROJECT_ROOT or $PROJECT_SOURCE #1274

Open AObuchow opened 1 week ago

AObuchow commented 1 week ago

Description

User-provided devworkspace environment variables (provided with spec.template.components[].container.env) can't reference $PROJECT_ROOT or $PROJECT_SOURCE (and probably other "built-in" environment variables that DevWorkspace Operator automatically provisions).

The issue lies in the ordering of environment variables when the devworkspace deployment is created: Kubernetes container environment variables can only reference environment variables that are declared earlier in the list of environment variables. Thus, all user-provided environment variables should be appended to the end of the list of environment variables (after the DWO-provisioned environment variables such as $PROJECT_SOURCE) when creating a devworkspace deployment.

How To Reproduce

  1. Create the following devworkspace on a cluster with DWO installed:
kind: DevWorkspace
apiVersion: workspace.devfile.io/v1alpha2
metadata:
  name: plain-devworkspace-env-test
spec:
  started: true
  routingClass: 'basic'
  template:
    projects:
      - name: web-nodejs-sample
        git:
          remotes:
            origin: "https://github.com/che-samples/web-nodejs-sample.git"
    components:
      - name: web-terminal
        container:
          image: quay.io/wto/web-terminal-tooling:next
          memoryRequest: 256Mi
          memoryLimit: 512Mi
          mountSources: true
          command:
           - "tail"
           - "-f"
           - "/dev/null"
          env:
           - name: ENV_TEST
             value: $(PROJECT_SOURCE)/hey-there
  1. Once the devworkspace starts, exec into it and run echo $ENV_TEST. The output will be $(PROJECT_SOURCE)/hey-there

Expected behavior

The output of echo $ENV_TEST should be /projects/web-nodejs-sample/hey-there.

Additional context

Che issue demonstrating the error in environment variable ordering: https://github.com/eclipse-che/che/issues/23008#issuecomment-2168627363