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

add projectsrc to env #220

Merged
merged 5 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
49 changes: 40 additions & 9 deletions pkg/devfile/generator/generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func init() {
}

func TestGetContainers(t *testing.T) {

containerNames := []string{"testcontainer1", "testcontainer2", "testcontainer3"}
containerImages := []string{"image1", "image2", "image3"}

Expand Down Expand Up @@ -124,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 @@ -132,14 +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 @@ -160,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 @@ -168,14 +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/test-project",
Name: "testVar1",
Value: "testVal1",
},
{
Name: "testVar2",
Value: "testVal2",
},
},
wantContainerVolMount: []corev1.VolumeMount{
Expand Down Expand Up @@ -309,7 +342,6 @@ func TestGetContainers(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockDevfileData := data.NewMockDevfileData(ctrl)
Expand Down Expand Up @@ -372,7 +404,6 @@ func TestGetContainers(t *testing.T) {
}
})
}

}

func TestGetVolumesAndVolumeMounts(t *testing.T) {
Expand Down
24 changes: 12 additions & 12 deletions pkg/devfile/generator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (

v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/api/v2/pkg/attributes"
"github.com/hashicorp/go-multierror"

"github.com/devfile/library/v2/pkg/devfile/parser"
"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
"github.com/hashicorp/go-multierror"
buildv1 "github.com/openshift/api/build/v1"
routev1 "github.com/openshift/api/route/v1"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -153,11 +153,12 @@ func addSyncRootFolder(container *corev1.Container, sourceMapping string) string

// Note: PROJECTS_ROOT & PROJECT_SOURCE are validated at the devfile parser level
// Add PROJECTS_ROOT to the container
container.Env = append(container.Env,
corev1.EnvVar{
container.Env = append([]corev1.EnvVar{
{
Name: EnvProjectsRoot,
Value: syncRootFolder,
})
},
}, container.Env...)
thepetk marked this conversation as resolved.
Show resolved Hide resolved

return syncRootFolder
}
Expand All @@ -168,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 = ""
thepetk marked this conversation as resolved.
Show resolved Hide resolved
} 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 @@ -184,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 @@ -719,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