From c06005c6d45892622fb322048232e1a851b0af58 Mon Sep 17 00:00:00 2001 From: Praful Khanduri <99384392+Horiodino@users.noreply.github.com> Date: Wed, 21 Aug 2024 19:28:16 +0530 Subject: [PATCH] add projectsrc to env (#220) * add projectsrc to env Signed-off-by: Horiodino * use sourcevalue Signed-off-by: Horiodino * fix after review Signed-off-by: Horiodino * fix after review Signed-off-by: Horiodino * fix test cases Signed-off-by: Horiodino --------- Signed-off-by: Horiodino --- pkg/devfile/generator/generators_test.go | 70 +++++++++++++++++------- pkg/devfile/generator/utils.go | 17 +++--- 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/pkg/devfile/generator/generators_test.go b/pkg/devfile/generator/generators_test.go index e5c13868..36356965 100644 --- a/pkg/devfile/generator/generators_test.go +++ b/pkg/devfile/generator/generators_test.go @@ -52,7 +52,6 @@ func init() { } func TestGetContainers(t *testing.T) { - containerNames := []string{"testcontainer1", "testcontainer2", "testcontainer3"} containerImages := []string{"image1", "image2", "image3"} @@ -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", + }, + }, }, }, }, @@ -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{ @@ -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", + }, + }, }, }, }, @@ -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{ @@ -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) @@ -372,7 +404,6 @@ func TestGetContainers(t *testing.T) { } }) } - } func TestGetVolumesAndVolumeMounts(t *testing.T) { @@ -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{ @@ -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{}, @@ -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{ @@ -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{ @@ -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{}, @@ -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{ @@ -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, @@ -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{}, @@ -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{}, @@ -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{}, @@ -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{}, diff --git a/pkg/devfile/generator/utils.go b/pkg/devfile/generator/utils.go index edfc4c66..2aed14a5 100644 --- a/pkg/devfile/generator/utils.go +++ b/pkg/devfile/generator/utils.go @@ -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" @@ -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 } @@ -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 } @@ -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