Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set PROJECT_SOURCE env var correctly in DevWorkspaces #697

Merged
merged 2 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/library/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ package constants

const (
ProjectsRootEnvVar = "PROJECTS_ROOT"
ProjectsSourceEnvVar = "PROJECTS_SOURCE"
ProjectsSourceEnvVar = "PROJECT_SOURCE"
ProjectsVolumeName = "projects"
)
4 changes: 2 additions & 2 deletions pkg/library/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func GetKubeContainersFromDevfile(workspace *dw.DevWorkspaceTemplateSpec) (*v1al
if err != nil {
return nil, err
}
handleMountSources(k8sContainer, component.Container)
handleMountSources(k8sContainer, component.Container, workspace.Projects)
podAdditions.Containers = append(podAdditions.Containers, *k8sContainer)
}

Expand All @@ -75,7 +75,7 @@ func GetKubeContainersFromDevfile(workspace *dw.DevWorkspaceTemplateSpec) (*v1al
if err != nil {
return nil, err
}
handleMountSources(k8sContainer, container.Container)
handleMountSources(k8sContainer, container.Container, workspace.Projects)
podAdditions.InitContainers = append(podAdditions.InitContainers, *k8sContainer)
}

Expand Down
23 changes: 21 additions & 2 deletions pkg/library/container/mountSources.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package container

import (
"path"

dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
corev1 "k8s.io/api/core/v1"

Expand Down Expand Up @@ -51,7 +53,7 @@ func AnyMountSources(devfileComponents []dw.Component) bool {

// handleMountSources adds a volumeMount to a container if the corresponding devfile container has
// mountSources enabled.
func handleMountSources(k8sContainer *corev1.Container, devfileContainer *dw.ContainerComponent) {
func handleMountSources(k8sContainer *corev1.Container, devfileContainer *dw.ContainerComponent, projects []dw.Project) {
if !HasMountSources(devfileContainer) {
return
}
Expand All @@ -72,15 +74,32 @@ func handleMountSources(k8sContainer *corev1.Container, devfileContainer *dw.Con
MountPath: sourceMapping,
})
}

projectsSourcePath := getProjectSourcePath(projects)

k8sContainer.Env = append(k8sContainer.Env, corev1.EnvVar{
Name: devfileConstants.ProjectsRootEnvVar,
Value: sourceMapping,
}, corev1.EnvVar{
Name: devfileConstants.ProjectsSourceEnvVar,
Value: sourceMapping, // TODO: Unclear how this should be handled in case of multiple projects
Value: path.Join(sourceMapping, projectsSourcePath),
})
}

// getProjectSourcePath gets the path, relative to PROJECTS_ROOT, that should be used for the PROJECT_SOURCE env var
func getProjectSourcePath(projects []dw.Project) string {
projectPath := ""
if len(projects) > 0 {
firstProject := projects[0]
if firstProject.ClonePath != "" {
projectPath = firstProject.ClonePath
} else {
projectPath = firstProject.Name
}
}
return projectPath
}

// getProjectsVolumeMount returns the projects volumeMount in a container, if it is defined; if it does not exist,
// returns nil.
func getProjectsVolumeMount(k8sContainer *corev1.Container) *corev1.VolumeMount {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ output:
value: "TEST_VALUE"
- name: "PROJECTS_ROOT"
value: "/source-mapping"
- name: "PROJECTS_SOURCE"
value: "/source-mapping" # Temp value until projects is figured out
- name: "PROJECT_SOURCE"
value: "/source-mapping"
command:
- "test"
- "command"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: "Sets PROJECT_SOURCE env var based on project's clonePath"

input:
projects:
- name: test_project
clonePath: clone-path
- name: test_project_unused
clonePath: ignored/since/not/first
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
sourceMapping: "/testdir1"
# no mountSources defined -> should mount sources
- 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
- 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
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: "/testdir1"
env:
- name: "DEVWORKSPACE_COMPONENT_NAME"
value: "testing-container-1"
- name: "PROJECTS_ROOT"
value: "/testdir1"
- name: "PROJECT_SOURCE"
value: "/testdir1/clone-path"
- 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: "DEVWORKSPACE_COMPONENT_NAME"
value: "testing-container-2"
- name: "PROJECTS_ROOT"
value: "/projects"
- name: "PROJECT_SOURCE"
value: "/projects/clone-path"
- name: testing-container-3
image: testing-image-3
imagePullPolicy: Always
env:
- name: "DEVWORKSPACE_COMPONENT_NAME"
value: "testing-container-3"
resources:
requests:
memory: "-1"
cpu: "-1"
limits:
memory: "-1"
cpu: "-1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: "Sets PROJECT_SOURCE env var based on project's name when no clonePath"

input:
projects:
- name: test_project
- name: test_project_unused
clonePath: ignored/since/not/first
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
sourceMapping: "/testdir1"
# no mountSources defined -> should mount sources
- 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
- 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
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: "/testdir1"
env:
- name: "DEVWORKSPACE_COMPONENT_NAME"
value: "testing-container-1"
- name: "PROJECTS_ROOT"
value: "/testdir1"
- name: "PROJECT_SOURCE"
value: "/testdir1/test_project"
- 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: "DEVWORKSPACE_COMPONENT_NAME"
value: "testing-container-2"
- name: "PROJECTS_ROOT"
value: "/projects"
- name: "PROJECT_SOURCE"
value: "/projects/test_project"
- name: testing-container-3
image: testing-image-3
imagePullPolicy: Always
env:
- name: "DEVWORKSPACE_COMPONENT_NAME"
value: "testing-container-3"
resources:
requests:
memory: "-1"
cpu: "-1"
limits:
memory: "-1"
cpu: "-1"
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ output:
value: "testing-container-1"
- name: "PROJECTS_ROOT"
value: "/not-source-mapping"
- name: "PROJECTS_SOURCE"
value: "/not-source-mapping" # Temp value until projects is figured out
- name: "PROJECT_SOURCE"
value: "/not-source-mapping"
9 changes: 4 additions & 5 deletions pkg/library/container/testdata/volume/mountSources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ output:
value: "testing-container-1"
- name: "PROJECTS_ROOT"
value: "/testdir1"
- name: "PROJECTS_SOURCE"
value: "/testdir1" # Temp value until projects is figured out
- name: "PROJECT_SOURCE"
value: "/testdir1"
- name: testing-container-2
image: testing-image-2
imagePullPolicy: Always
Expand All @@ -70,9 +70,8 @@ output:
value: "testing-container-2"
- name: "PROJECTS_ROOT"
value: "/projects"
- name: "PROJECTS_SOURCE"
value: "/projects" # Temp value until projects is figured out

- name: "PROJECT_SOURCE"
value: "/projects"
- name: testing-container-3
image: testing-image-3
imagePullPolicy: Always
Expand Down