Skip to content

Commit

Permalink
add projectsrc to env (#220)
Browse files Browse the repository at this point in the history
* add projectsrc to env

Signed-off-by: Horiodino <[email protected]>

* use sourcevalue

Signed-off-by: Horiodino <[email protected]>

* fix after review

Signed-off-by: Horiodino <[email protected]>

* fix after review

Signed-off-by: Horiodino <[email protected]>

* fix test cases

Signed-off-by: Horiodino <[email protected]>

---------

Signed-off-by: Horiodino <[email protected]>
  • Loading branch information
Horiodino authored Aug 21, 2024
1 parent cd486bd commit c06005c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 27 deletions.
70 changes: 50 additions & 20 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 Expand Up @@ -1447,8 +1478,8 @@ func TestGetPodTemplateSpec(t *testing.T) {
Name: "main",
Image: "an-image",
Env: []corev1.EnvVar{
{Name: "PROJECTS_ROOT", Value: "/projects"},
{Name: "PROJECT_SOURCE", Value: "/projects"},
{Name: "PROJECTS_ROOT", Value: "/projects"},
},
ImagePullPolicy: corev1.PullAlways,
SecurityContext: &corev1.SecurityContext{
Expand All @@ -1459,8 +1490,8 @@ func TestGetPodTemplateSpec(t *testing.T) {
Name: "tools",
Image: "a-tool-image",
Env: []corev1.EnvVar{
{Name: "PROJECTS_ROOT", Value: "/projects"},
{Name: "PROJECT_SOURCE", Value: "/projects"},
{Name: "PROJECTS_ROOT", Value: "/projects"},
},
ImagePullPolicy: corev1.PullAlways,
Ports: []corev1.ContainerPort{},
Expand Down Expand Up @@ -1511,8 +1542,8 @@ func TestGetPodTemplateSpec(t *testing.T) {
Name: "main",
Image: "an-image",
Env: []corev1.EnvVar{
{Name: "PROJECTS_ROOT", Value: "/projects"},
{Name: "PROJECT_SOURCE", Value: "/projects"},
{Name: "PROJECTS_ROOT", Value: "/projects"},
},
ImagePullPolicy: corev1.PullAlways,
SecurityContext: &corev1.SecurityContext{
Expand Down Expand Up @@ -1565,8 +1596,8 @@ func TestGetPodTemplateSpec(t *testing.T) {
Name: "main",
Image: "an-image",
Env: []corev1.EnvVar{
{Name: "PROJECTS_ROOT", Value: "/projects"},
{Name: "PROJECT_SOURCE", Value: "/projects"},
{Name: "PROJECTS_ROOT", Value: "/projects"},
},
ImagePullPolicy: corev1.PullAlways,
SecurityContext: &corev1.SecurityContext{
Expand Down Expand Up @@ -1621,8 +1652,8 @@ func TestGetPodTemplateSpec(t *testing.T) {
Name: "main",
Image: "an-image",
Env: []corev1.EnvVar{
{Name: "PROJECTS_ROOT", Value: "/projects"},
{Name: "PROJECT_SOURCE", Value: "/projects"},
{Name: "PROJECTS_ROOT", Value: "/projects"},
},
ImagePullPolicy: corev1.PullAlways,
Ports: []corev1.ContainerPort{},
Expand Down Expand Up @@ -1675,8 +1706,8 @@ func TestGetPodTemplateSpec(t *testing.T) {
Name: "main",
Image: "an-image",
Env: []corev1.EnvVar{
{Name: "PROJECTS_ROOT", Value: "/projects"},
{Name: "PROJECT_SOURCE", Value: "/projects"},
{Name: "PROJECTS_ROOT", Value: "/projects"},
},
ImagePullPolicy: corev1.PullAlways,
SecurityContext: &corev1.SecurityContext{
Expand Down Expand Up @@ -1713,7 +1744,6 @@ func TestGetPodTemplateSpec(t *testing.T) {
mockDevfileData.EXPECT().GetAttributes().Return(attributes.Attributes{
PodOverridesAttribute: apiext.JSON{Raw: []byte("{\"spec\": {\"securityContext\": {\"seccompProfile\": {\"type\": \"Localhost\"}}}}")},
}, nil)

mockDevfileData.EXPECT().GetSchemaVersion().Return("2.1.0").AnyTimes()
return parser.DevfileObj{
Data: mockDevfileData,
Expand Down Expand Up @@ -1741,8 +1771,8 @@ func TestGetPodTemplateSpec(t *testing.T) {
Name: "main",
Image: "an-image",
Env: []corev1.EnvVar{
{Name: "PROJECTS_ROOT", Value: "/projects"},
{Name: "PROJECT_SOURCE", Value: "/projects"},
{Name: "PROJECTS_ROOT", Value: "/projects"},
},
ImagePullPolicy: corev1.PullAlways,
Ports: []corev1.ContainerPort{},
Expand Down Expand Up @@ -1810,8 +1840,8 @@ func TestGetPodTemplateSpec(t *testing.T) {
Name: "main",
Image: "an-image",
Env: []corev1.EnvVar{
{Name: "PROJECTS_ROOT", Value: "/projects"},
{Name: "PROJECT_SOURCE", Value: "/projects"},
{Name: "PROJECTS_ROOT", Value: "/projects"},
},
ImagePullPolicy: corev1.PullAlways,
Ports: []corev1.ContainerPort{},
Expand Down Expand Up @@ -1873,8 +1903,8 @@ func TestGetPodTemplateSpec(t *testing.T) {
Name: "main",
Image: "an-image",
Env: []corev1.EnvVar{
{Name: "PROJECTS_ROOT", Value: "/projects"},
{Name: "PROJECT_SOURCE", Value: "/projects"},
{Name: "PROJECTS_ROOT", Value: "/projects"},
},
ImagePullPolicy: corev1.PullAlways,
Ports: []corev1.ContainerPort{},
Expand Down Expand Up @@ -1935,8 +1965,8 @@ func TestGetPodTemplateSpec(t *testing.T) {
Name: "tools",
Image: "a-tool-image",
Env: []corev1.EnvVar{
{Name: "PROJECTS_ROOT", Value: "/projects"},
{Name: "PROJECT_SOURCE", Value: "/projects"},
{Name: "PROJECTS_ROOT", Value: "/projects"},
},
ImagePullPolicy: corev1.PullAlways,
Ports: []corev1.ContainerPort{},
Expand Down
17 changes: 10 additions & 7 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...)

return syncRootFolder
}
Expand Down Expand Up @@ -189,11 +190,12 @@ func addSyncFolder(container *corev1.Container, sourceVolumePath string, project
}
}

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 +721,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 c06005c

Please sign in to comment.