diff --git a/pkg/library/container/mountSources.go b/pkg/library/container/mountSources.go index 1827baa9e..7afe963ae 100644 --- a/pkg/library/container/mountSources.go +++ b/pkg/library/container/mountSources.go @@ -81,13 +81,17 @@ func handleMountSources(k8sContainer *corev1.Container, devfileContainer *dw.Con return err } - k8sContainer.Env = append(k8sContainer.Env, corev1.EnvVar{ - Name: devfileConstants.ProjectsRootEnvVar, - Value: sourceMapping, - }, corev1.EnvVar{ - Name: devfileConstants.ProjectsSourceEnvVar, - Value: path.Join(sourceMapping, projectsSourcePath), - }) + // The $PROJECT_ROOT and $PROJECT_SOURCE environment variables must be the first + // environment variables defined in the container so that other environment variables can reference them. + k8sContainer.Env = append([]corev1.EnvVar{ + { + Name: devfileConstants.ProjectsRootEnvVar, + Value: sourceMapping, + }, + { + Name: devfileConstants.ProjectsSourceEnvVar, + Value: path.Join(sourceMapping, projectsSourcePath), + }}, k8sContainer.Env...) return nil } diff --git a/pkg/library/container/testdata/component/converts-all-fields.yaml b/pkg/library/container/testdata/component/converts-all-fields.yaml index 2260202b2..b2e15e5a7 100644 --- a/pkg/library/container/testdata/component/converts-all-fields.yaml +++ b/pkg/library/container/testdata/component/converts-all-fields.yaml @@ -51,14 +51,14 @@ output: memory: "-1" cpu: "-1" env: - - name: "DEVWORKSPACE_COMPONENT_NAME" - value: "testing-container-1" - - name: "TEST_ENVVAR" - value: "TEST_VALUE" - name: "PROJECTS_ROOT" value: "/source-mapping" - name: "PROJECT_SOURCE" value: "/source-mapping" + - name: "DEVWORKSPACE_COMPONENT_NAME" + value: "testing-container-1" + - name: "TEST_ENVVAR" + value: "TEST_VALUE" command: - "test" - "command" diff --git a/pkg/library/container/testdata/mountsources/projects-source-and-root-first-env-vars.yaml.yaml b/pkg/library/container/testdata/mountsources/projects-source-and-root-first-env-vars.yaml.yaml new file mode 100644 index 000000000..9084ed25a --- /dev/null +++ b/pkg/library/container/testdata/mountsources/projects-source-and-root-first-env-vars.yaml.yaml @@ -0,0 +1,116 @@ +name: "Ensures PROJECT_ROOT and PROJECT_SOURCE env vars are first in list of env vars" + +input: + projects: + - name: test_project + clonePath: clone-path + components: + - name: testing-container-1 + container: + image: testing-image-1 + memoryRequest: "-1" # isolate test to not include this field + memoryLimit: "-1" # isolate test to not include this field + cpuRequest: "-1" # isolate test to not include this field + cpuLimit: "-1" # isolate test to not include this field + # no mountSources defined -> should mount sources + env: + - name: user-env-var-1 + value: value1 + - name: user-env-var-1 + value: value1 + - name: testing-container-2 + container: + image: testing-image-2 + memoryRequest: "-1" # isolate test to not include this field + memoryLimit: "-1" # isolate test to not include this field + cpuRequest: "-1" # isolate test to not include this field + cpuLimit: "-1" # isolate test to not include this field + # No sourceMapping -> defaults to "/projects" + mountSources: true # mountSources: true -> should mount sources + env: + - name: user-env-var-2 + value: value1 + - name: user-env-var-3 + value: value1 + - name: testing-container-3 + container: + image: testing-image-3 + memoryRequest: "-1" # isolate test to not include this field + memoryLimit: "-1" # isolate test to not include this field + cpuRequest: "-1" # isolate test to not include this field + cpuLimit: "-1" # isolate test to not include this field + sourceMapping: "/projects" + mountSources: false # mountSources: false -> should not mount sources + env: + - name: user-env-var-4 + value: value1 + - name: user-env-var-5 + value: value1 +output: + podAdditions: + containers: + - name: testing-container-1 + image: testing-image-1 + imagePullPolicy: Always + resources: + requests: + memory: "-1" + cpu: "-1" + limits: + memory: "-1" + cpu: "-1" + volumeMounts: + - name: "projects" + mountPath: "/projects" + env: + - name: "PROJECTS_ROOT" + value: "/projects" + - name: "PROJECT_SOURCE" + value: "/projects/clone-path" + - name: "DEVWORKSPACE_COMPONENT_NAME" + value: "testing-container-1" + - name: user-env-var-1 + value: value1 + - name: user-env-var-1 + value: value1 + - name: testing-container-2 + image: testing-image-2 + imagePullPolicy: Always + resources: + requests: + memory: "-1" + cpu: "-1" + limits: + memory: "-1" + cpu: "-1" + volumeMounts: + - name: "projects" + mountPath: "/projects" + env: + - name: "PROJECTS_ROOT" + value: "/projects" + - name: "PROJECT_SOURCE" + value: "/projects/clone-path" + - name: "DEVWORKSPACE_COMPONENT_NAME" + value: "testing-container-2" + - name: user-env-var-2 + value: value1 + - name: user-env-var-3 + value: value1 + - name: testing-container-3 + image: testing-image-3 + imagePullPolicy: Always + env: + - name: "DEVWORKSPACE_COMPONENT_NAME" + value: "testing-container-3" + - name: user-env-var-4 + value: value1 + - name: user-env-var-5 + value: value1 + resources: + requests: + memory: "-1" + cpu: "-1" + limits: + memory: "-1" + cpu: "-1" diff --git a/pkg/library/container/testdata/mountsources/projects-source-is-set-correctly-from-clonepath.yaml b/pkg/library/container/testdata/mountsources/projects-source-is-set-correctly-from-clonepath.yaml index 1da0d2253..8f3780d98 100644 --- a/pkg/library/container/testdata/mountsources/projects-source-is-set-correctly-from-clonepath.yaml +++ b/pkg/library/container/testdata/mountsources/projects-source-is-set-correctly-from-clonepath.yaml @@ -51,12 +51,12 @@ output: - name: "projects" mountPath: "/testdir1" env: - - name: "DEVWORKSPACE_COMPONENT_NAME" - value: "testing-container-1" - name: "PROJECTS_ROOT" value: "/testdir1" - name: "PROJECT_SOURCE" value: "/testdir1/clone-path" + - name: "DEVWORKSPACE_COMPONENT_NAME" + value: "testing-container-1" - name: testing-container-2 image: testing-image-2 imagePullPolicy: Always @@ -71,12 +71,12 @@ output: - name: "projects" mountPath: "/projects" env: - - name: "DEVWORKSPACE_COMPONENT_NAME" - value: "testing-container-2" - name: "PROJECTS_ROOT" value: "/projects" - name: "PROJECT_SOURCE" value: "/projects/clone-path" + - name: "DEVWORKSPACE_COMPONENT_NAME" + value: "testing-container-2" - name: testing-container-3 image: testing-image-3 imagePullPolicy: Always diff --git a/pkg/library/container/testdata/mountsources/projects-source-is-set-correctly-from-project-name.yaml b/pkg/library/container/testdata/mountsources/projects-source-is-set-correctly-from-project-name.yaml index b9e1bc3da..cc9109585 100644 --- a/pkg/library/container/testdata/mountsources/projects-source-is-set-correctly-from-project-name.yaml +++ b/pkg/library/container/testdata/mountsources/projects-source-is-set-correctly-from-project-name.yaml @@ -50,12 +50,12 @@ output: - name: "projects" mountPath: "/testdir1" env: - - name: "DEVWORKSPACE_COMPONENT_NAME" - value: "testing-container-1" - name: "PROJECTS_ROOT" value: "/testdir1" - name: "PROJECT_SOURCE" value: "/testdir1/test_project" + - name: "DEVWORKSPACE_COMPONENT_NAME" + value: "testing-container-1" - name: testing-container-2 image: testing-image-2 imagePullPolicy: Always @@ -70,12 +70,12 @@ output: - name: "projects" mountPath: "/projects" env: - - name: "DEVWORKSPACE_COMPONENT_NAME" - value: "testing-container-2" - name: "PROJECTS_ROOT" value: "/projects" - name: "PROJECT_SOURCE" value: "/projects/test_project" + - name: "DEVWORKSPACE_COMPONENT_NAME" + value: "testing-container-2" - name: testing-container-3 image: testing-image-3 imagePullPolicy: Always diff --git a/pkg/library/container/testdata/volume/container-that-mounts-projects-directly.yaml b/pkg/library/container/testdata/volume/container-that-mounts-projects-directly.yaml index e77e3b75b..d0a25af70 100644 --- a/pkg/library/container/testdata/volume/container-that-mounts-projects-directly.yaml +++ b/pkg/library/container/testdata/volume/container-that-mounts-projects-directly.yaml @@ -35,9 +35,9 @@ output: - name: "projects" mountPath: "/not-source-mapping" env: - - name: "DEVWORKSPACE_COMPONENT_NAME" - value: "testing-container-1" - name: "PROJECTS_ROOT" value: "/not-source-mapping" - name: "PROJECT_SOURCE" value: "/not-source-mapping" + - name: "DEVWORKSPACE_COMPONENT_NAME" + value: "testing-container-1" diff --git a/pkg/library/container/testdata/volume/mountSources.yaml b/pkg/library/container/testdata/volume/mountSources.yaml index 5b54c2597..7e4772981 100644 --- a/pkg/library/container/testdata/volume/mountSources.yaml +++ b/pkg/library/container/testdata/volume/mountSources.yaml @@ -46,12 +46,12 @@ output: - name: "projects" mountPath: "/testdir1" env: - - name: "DEVWORKSPACE_COMPONENT_NAME" - value: "testing-container-1" - name: "PROJECTS_ROOT" value: "/testdir1" - name: "PROJECT_SOURCE" value: "/testdir1" + - name: "DEVWORKSPACE_COMPONENT_NAME" + value: "testing-container-1" - name: testing-container-2 image: testing-image-2 imagePullPolicy: Always @@ -66,12 +66,12 @@ output: - name: "projects" mountPath: "/projects" env: - - name: "DEVWORKSPACE_COMPONENT_NAME" - value: "testing-container-2" - name: "PROJECTS_ROOT" value: "/projects" - name: "PROJECT_SOURCE" value: "/projects" + - name: "DEVWORKSPACE_COMPONENT_NAME" + value: "testing-container-2" - name: testing-container-3 image: testing-image-3 imagePullPolicy: Always