Skip to content

Commit

Permalink
fix after review
Browse files Browse the repository at this point in the history
Signed-off-by: Horiodino <[email protected]>
  • Loading branch information
Horiodino committed Aug 15, 2024
1 parent 64f49c7 commit 3084543
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
44 changes: 38 additions & 6 deletions pkg/devfile/generator/generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ func TestGetContainers(t *testing.T) {
Container: v1.Container{
Image: containerImages[0],
MountSources: &trueMountSources,
Env: []v1.EnvVar{
{
Name: "testVar1",
Value: "testVal1",
},
{
Name: "testVar2",
Value: "testVal2",
},
},
},
},
},
Expand All @@ -131,13 +141,21 @@ func TestGetContainers(t *testing.T) {
wantContainerName: containerNames[0],
wantContainerImage: containerImages[0],
wantContainerEnv: []corev1.EnvVar{
{
Name: "PROJECT_SOURCE",
Value: "/projects/test-project",
},
{
Name: "PROJECTS_ROOT",
Value: "/projects",
},
{
Name: "PROJECT_SOURCE",
Value: "/projects/test-project",
Name: "testVar1",
Value: "testVal1",
},
{
Name: "testVar2",
Value: "testVal2",
},
},
wantContainerVolMount: []corev1.VolumeMount{
Expand All @@ -158,6 +176,16 @@ func TestGetContainers(t *testing.T) {
Image: containerImages[0],
MountSources: &trueMountSources,
SourceMapping: "/myroot",
Env: []v1.EnvVar{
{
Name: "testVar1",
Value: "testVal1",
},
{
Name: "testVar2",
Value: "testVal2",
},
},
},
},
},
Expand All @@ -166,17 +194,21 @@ func TestGetContainers(t *testing.T) {
wantContainerName: containerNames[0],
wantContainerImage: containerImages[0],
wantContainerEnv: []corev1.EnvVar{
{
Name: "PROJECT_SOURCE",
Value: "/myroot/test-project",
},
{
Name: "PROJECTS_ROOT",
Value: "/myroot",
},
{
Name: "PROJECT_SOURCE",
Value: "/myroot",
Name: "testVar1",
Value: "testVal1",
},
{
Name: "PROJECT_SOURCE",
Value: "/myroot/test-project",
Name: "testVar2",
Value: "testVal2",
},
},
wantContainerVolMount: []corev1.VolumeMount{
Expand Down
23 changes: 7 additions & 16 deletions pkg/devfile/generator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,6 @@ func addSyncRootFolder(container *corev1.Container, sourceMapping string) string
},
}, container.Env...)

if sourceMapping != "" {
container.Env = append(container.Env,
corev1.EnvVar{
Name: EnvProjectsSrc,
Value: sourceMapping,
})
}

return syncRootFolder
}

Expand All @@ -177,13 +169,11 @@ func addSyncRootFolder(container *corev1.Container, sourceMapping string) string
func addSyncFolder(container *corev1.Container, sourceVolumePath string, projects []v1.Project) error {
var syncFolder string

// if there are no projects in the devfile, source would be synced to $PROJECTS_ROOT
if len(projects) == 0 {
syncFolder = sourceVolumePath
// No projects found, set PROJECT_SOURCE to empty
syncFolder = ""
} else {
// if there is one or more projects in the devfile, get the first project and check its clonepath
project := projects[0]
// If clonepath does not exist source would be synced to $PROJECTS_ROOT/projectName
syncFolder = filepath.ToSlash(filepath.Join(sourceVolumePath, project.Name))

if project.ClonePath != "" {
Expand All @@ -193,16 +183,16 @@ func addSyncFolder(container *corev1.Container, sourceVolumePath string, project
if strings.Contains(project.ClonePath, "..") {
return fmt.Errorf("the clonePath %s in the devfile project %s cannot escape the value defined by $PROJECTS_ROOT. Please avoid using \"..\" in clonePath", project.ClonePath, project.Name)
}
// If clonepath exist source would be synced to $PROJECTS_ROOT/clonePath
syncFolder = filepath.ToSlash(filepath.Join(sourceVolumePath, project.ClonePath))
}
}

container.Env = append(container.Env,
corev1.EnvVar{
container.Env = append([]corev1.EnvVar{
{
Name: EnvProjectsSrc,
Value: syncFolder,
})
},
}, container.Env...)

return nil
}
Expand Down Expand Up @@ -728,6 +718,7 @@ func getAllContainers(devfileObj parser.DevfileObj, options common.DevfileOption
if comp.Container.MountSources == nil || *comp.Container.MountSources {
syncRootFolder := addSyncRootFolder(container, comp.Container.SourceMapping)

// Always set PROJECT_SOURCE, regardless of project presence
projects, err := devfileObj.Data.GetProjects(common.DevfileOptions{})
if err != nil {
return nil, err
Expand Down

0 comments on commit 3084543

Please sign in to comment.