From 30845433e7c04c55e02850291e8b8ba51b3b78f8 Mon Sep 17 00:00:00 2001 From: Horiodino Date: Thu, 15 Aug 2024 22:17:13 +0530 Subject: [PATCH] fix after review Signed-off-by: Horiodino --- pkg/devfile/generator/generators_test.go | 44 ++++++++++++++++++++---- pkg/devfile/generator/utils.go | 23 ++++--------- 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/pkg/devfile/generator/generators_test.go b/pkg/devfile/generator/generators_test.go index c287e015..de35fce2 100644 --- a/pkg/devfile/generator/generators_test.go +++ b/pkg/devfile/generator/generators_test.go @@ -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", + }, + }, }, }, }, @@ -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{ @@ -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", + }, + }, }, }, }, @@ -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{ diff --git a/pkg/devfile/generator/utils.go b/pkg/devfile/generator/utils.go index 59db9102..8be87b3f 100644 --- a/pkg/devfile/generator/utils.go +++ b/pkg/devfile/generator/utils.go @@ -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 } @@ -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 != "" { @@ -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 } @@ -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