Skip to content

Commit

Permalink
Fix: include selected starter project for $PROJECT_SOURCE path selection
Browse files Browse the repository at this point in the history
Fix #1189

Signed-off-by: Andrew Obuchowicz <[email protected]>
  • Loading branch information
AObuchow committed Oct 19, 2023
1 parent 3ed1eba commit 3f91f30
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
11 changes: 7 additions & 4 deletions pkg/library/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ package container
import (
"fmt"

"github.com/devfile/devworkspace-operator/pkg/library/overrides"

dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
"github.com/devfile/devworkspace-operator/pkg/library/flatten"
"github.com/devfile/devworkspace-operator/pkg/library/lifecycle"
"github.com/devfile/devworkspace-operator/pkg/library/overrides"
corev1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -65,7 +64,9 @@ func GetKubeContainersFromDevfile(workspace *dw.DevWorkspaceTemplateSpec, securi
if err != nil {
return nil, err
}
handleMountSources(k8sContainer, component.Container, workspace.Projects)
if err := handleMountSources(k8sContainer, component.Container, workspace); err != nil {
return nil, err
}
if overrides.NeedsContainerOverride(&component) {
patchedContainer, err := overrides.ApplyContainerOverrides(&component, k8sContainer)
if err != nil {
Expand All @@ -89,7 +90,9 @@ func GetKubeContainersFromDevfile(workspace *dw.DevWorkspaceTemplateSpec, securi
if err != nil {
return nil, err
}
handleMountSources(k8sContainer, initComponent.Container, workspace.Projects)
if err := handleMountSources(k8sContainer, initComponent.Container, workspace); err != nil {
return nil, err
}
if overrides.NeedsContainerOverride(&initComponent) {
patchedContainer, err := overrides.ApplyContainerOverrides(&initComponent, k8sContainer)
if err != nil {
Expand Down
47 changes: 34 additions & 13 deletions pkg/library/container/mountSources.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/devfile/devworkspace-operator/pkg/constants"
devfileConstants "github.com/devfile/devworkspace-operator/pkg/library/constants"
projectslib "github.com/devfile/devworkspace-operator/pkg/library/projects"
)

// HasMountSources evaluates whether project sources should be mounted in the given container component.
Expand Down Expand Up @@ -53,9 +54,9 @@ 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, projects []dw.Project) {
func handleMountSources(k8sContainer *corev1.Container, devfileContainer *dw.ContainerComponent, workspace *dw.DevWorkspaceTemplateSpec) error {
if !HasMountSources(devfileContainer) {
return
return nil
}
var sourceMapping string
if vm := getProjectsVolumeMount(k8sContainer); vm != nil {
Expand All @@ -75,7 +76,10 @@ func handleMountSources(k8sContainer *corev1.Container, devfileContainer *dw.Con
})
}

projectsSourcePath := getProjectSourcePath(projects)
projectsSourcePath, err := getProjectSourcePath(workspace)
if err != nil {
return err
}

k8sContainer.Env = append(k8sContainer.Env, corev1.EnvVar{
Name: devfileConstants.ProjectsRootEnvVar,
Expand All @@ -84,20 +88,37 @@ func handleMountSources(k8sContainer *corev1.Container, devfileContainer *dw.Con
Name: devfileConstants.ProjectsSourceEnvVar,
Value: path.Join(sourceMapping, projectsSourcePath),
})

return nil
}

// 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 := ""
// getProjectSourcePath gets the path, relative to PROJECTS_ROOT, that should be used for the PROJECT_SOURCE env var.
// Returns an error if there was a problem retrieving the selected starter project.
//
// The project source path is determined based on the following priorities:
//
// 1. If the workspace has at least one regular project, the first one will be selected.
// If the first project has a clone path, it will be used, otherwise the project's name will be used as the project source path.
//
// 2. If the workspace has a starter project that is selected, its name will be used as the project source path.
//
// 3. Otherwise, the returned project source path will be an empty string.
func getProjectSourcePath(workspace *dw.DevWorkspaceTemplateSpec) (string, error) {
projects := workspace.Projects
// If there are any projects, return the first one's clone path
if len(projects) > 0 {
firstProject := projects[0]
if firstProject.ClonePath != "" {
projectPath = firstProject.ClonePath
} else {
projectPath = firstProject.Name
}
return projectslib.GetClonePath(&projects[0]), nil
}

// No projects, check if we have a selected starter project
selectedStarterProject, err := projectslib.GetStarterProject(workspace)
if err != nil {
return "", err
} else if selectedStarterProject != nil {
// Starter projects do not allow specifying a clone path, so use the name
return selectedStarterProject.Name, nil
}
return projectPath
return "", nil
}

// getProjectsVolumeMount returns the projects volumeMount in a container, if it is defined; if it does not exist,
Expand Down

0 comments on commit 3f91f30

Please sign in to comment.