From b50d309472b7e7f70e8775246952c9d338a08cce Mon Sep 17 00:00:00 2001 From: Stephanie Date: Tue, 19 Oct 2021 16:16:12 -0400 Subject: [PATCH 1/2] create volume with emptyDir with ephemeral is true Signed-off-by: Stephanie --- pkg/devfile/generator/generators.go | 23 ++++++- pkg/devfile/generator/generators_test.go | 77 +++++++++++++++++++++--- pkg/devfile/generator/utils.go | 10 +++ 3 files changed, 100 insertions(+), 10 deletions(-) diff --git a/pkg/devfile/generator/generators.go b/pkg/devfile/generator/generators.go index 8a077d95..913c4fdf 100644 --- a/pkg/devfile/generator/generators.go +++ b/pkg/devfile/generator/generators.go @@ -384,9 +384,30 @@ func GetVolumesAndVolumeMounts(devfileObj parser.DevfileObj, volumeParams Volume return nil, err } + options.ComponentOptions = common.ComponentOptions{ + ComponentType: v1.VolumeComponentType, + } + volumeComponent, err := devfileObj.Data.GetComponents(options) + if err != nil { + return nil, err + } + var pvcVols []corev1.Volume for volName, volInfo := range volumeParams.VolumeNameToVolumeInfo { - pvcVols = append(pvcVols, getPVC(volInfo.VolumeName, volInfo.PVCName)) + emptyDirVolume := false + for _, volumeComp := range volumeComponent { + if volumeComp.Name == volName && *volumeComp.Volume.Ephemeral { + emptyDirVolume = true + break + } + } + + // if `ephemeral=true`, a volume with emptyDir should be created + if emptyDirVolume { + pvcVols = append(pvcVols, getEmptyDirVol(volInfo.VolumeName)) + } else { + pvcVols = append(pvcVols, getPVC(volInfo.VolumeName, volInfo.PVCName)) + } // containerNameToMountPaths is a map of the Devfile container name to their Devfile Volume Mount Paths for a given Volume Name containerNameToMountPaths := make(map[string][]string) diff --git a/pkg/devfile/generator/generators_test.go b/pkg/devfile/generator/generators_test.go index ec25bf32..b121ff75 100644 --- a/pkg/devfile/generator/generators_test.go +++ b/pkg/devfile/generator/generators_test.go @@ -352,17 +352,20 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) { } errMatches := "an expected error" + trueEphemeral := true tests := []struct { name string - components []v1.Component + containerComponents []v1.Component + volumeComponents []v1.Component volumeNameToVolInfo map[string]VolumeInfo wantContainerToVol map[string][]testVolumeMountInfo + ephemeralVol bool wantErr *string }{ { - name: "One volume mounted", - components: []v1.Component{testingutil.GetFakeContainerComponent("comp1"), testingutil.GetFakeContainerComponent("comp2")}, + name: "One volume mounted", + containerComponents: []v1.Component{testingutil.GetFakeContainerComponent("comp1"), testingutil.GetFakeContainerComponent("comp2")}, volumeNameToVolInfo: map[string]VolumeInfo{ "myvolume1": { PVCName: "volume1-pvc", @@ -386,7 +389,7 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) { }, { name: "One volume mounted at diff locations", - components: []v1.Component{ + containerComponents: []v1.Component{ { Name: "container1", ComponentUnion: v1.ComponentUnion{ @@ -428,7 +431,7 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) { }, { name: "One volume mounted at diff container components", - components: []v1.Component{ + containerComponents: []v1.Component{ { Name: "container1", ComponentUnion: v1.ComponentUnion{ @@ -481,6 +484,53 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) { }, }, }, + { + name: "Ephemeral volume", + containerComponents: []v1.Component{ + { + Name: "container1", + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{ + Container: v1.Container{ + VolumeMounts: []v1.VolumeMount{ + { + Name: "volume1", + Path: "/path1", + }, + }, + }, + }, + }, + }, + }, + volumeComponents: []v1.Component{ + { + Name: "volume1", + ComponentUnion: v1.ComponentUnion{ + Volume: &v1.VolumeComponent{ + Volume: v1.Volume{ + Ephemeral: &trueEphemeral, + }, + }, + }, + }, + }, + volumeNameToVolInfo: map[string]VolumeInfo{ + "volume1": { + PVCName: "volume1-pvc", + VolumeName: "volume1-pvc-vol", + }, + }, + ephemeralVol: true, + wantContainerToVol: map[string][]testVolumeMountInfo{ + "container1": { + { + mountPath: "/path1", + volumeName: "volume1-pvc-vol", + }, + }, + }, + }, { name: "Simulating error case, check if error matches", wantErr: &errMatches, @@ -494,14 +544,21 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) { defer ctrl.Finish() mockDevfileData := data.NewMockDevfileData(ctrl) - mockGetComponents := mockDevfileData.EXPECT().GetComponents(common.DevfileOptions{ + mockGetContainerComponents := mockDevfileData.EXPECT().GetComponents(common.DevfileOptions{ ComponentOptions: common.ComponentOptions{ ComponentType: v1.ContainerComponentType, }, }) + mockGetVolumeComponents := mockDevfileData.EXPECT().GetComponents(common.DevfileOptions{ + ComponentOptions: common.ComponentOptions{ + ComponentType: v1.VolumeComponentType, + }, + }) + // set up the mock data - mockGetComponents.Return(tt.components, nil).AnyTimes() + mockGetContainerComponents.Return(tt.containerComponents, nil).AnyTimes() + mockGetVolumeComponents.Return(tt.volumeComponents, nil).AnyTimes() mockDevfileData.EXPECT().GetProjects(common.DevfileOptions{}).Return(nil, nil).AnyTimes() devObj := parser.DevfileObj{ @@ -516,7 +573,7 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) { if tt.wantErr != nil { // simulate error condition - mockGetComponents.Return(nil, fmt.Errorf(*tt.wantErr)) + mockGetContainerComponents.Return(nil, fmt.Errorf(*tt.wantErr)) } @@ -533,7 +590,9 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) { for _, volInfo := range tt.volumeNameToVolInfo { matched := false for _, pvcVol := range pvcVols { - if volInfo.VolumeName == pvcVol.Name && pvcVol.PersistentVolumeClaim != nil && volInfo.PVCName == pvcVol.PersistentVolumeClaim.ClaimName { + if tt.ephemeralVol && volInfo.VolumeName == pvcVol.Name && reflect.DeepEqual(pvcVol.EmptyDir, &corev1.EmptyDirVolumeSource{}) { + matched = true + } else if volInfo.VolumeName == pvcVol.Name && pvcVol.PersistentVolumeClaim != nil && volInfo.PVCName == pvcVol.PersistentVolumeClaim.ClaimName { matched = true } } diff --git a/pkg/devfile/generator/utils.go b/pkg/devfile/generator/utils.go index 8487a2cf..452cb052 100644 --- a/pkg/devfile/generator/utils.go +++ b/pkg/devfile/generator/utils.go @@ -526,6 +526,16 @@ func getPVC(volumeName, pvcName string) corev1.Volume { } } +// getEmptyDirVol gets a volume with emptyDir +func getEmptyDirVol(volumeName string) corev1.Volume { + return corev1.Volume{ + Name: volumeName, + VolumeSource: corev1.VolumeSource{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + }, + } +} + // addVolumeMountToContainers adds the Volume Mounts in containerNameToMountPaths to the containers for a given volumeName. // containerNameToMountPaths is a map of a container name to an array of its Mount Paths. func addVolumeMountToContainers(containers []corev1.Container, volumeName string, containerNameToMountPaths map[string][]string) { From edf6e75496d96dde1858b11fb49fc79f7c56727b Mon Sep 17 00:00:00 2001 From: Stephanie Date: Tue, 19 Oct 2021 16:25:10 -0400 Subject: [PATCH 2/2] update condition Signed-off-by: Stephanie --- pkg/devfile/generator/generators_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/devfile/generator/generators_test.go b/pkg/devfile/generator/generators_test.go index b121ff75..1a199bb5 100644 --- a/pkg/devfile/generator/generators_test.go +++ b/pkg/devfile/generator/generators_test.go @@ -590,9 +590,9 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) { for _, volInfo := range tt.volumeNameToVolInfo { matched := false for _, pvcVol := range pvcVols { - if tt.ephemeralVol && volInfo.VolumeName == pvcVol.Name && reflect.DeepEqual(pvcVol.EmptyDir, &corev1.EmptyDirVolumeSource{}) { - matched = true - } else if volInfo.VolumeName == pvcVol.Name && pvcVol.PersistentVolumeClaim != nil && volInfo.PVCName == pvcVol.PersistentVolumeClaim.ClaimName { + emptyDirVolCondition := tt.ephemeralVol && reflect.DeepEqual(pvcVol.EmptyDir, &corev1.EmptyDirVolumeSource{}) + pvcCondition := pvcVol.PersistentVolumeClaim != nil && volInfo.PVCName == pvcVol.PersistentVolumeClaim.ClaimName + if volInfo.VolumeName == pvcVol.Name && (emptyDirVolCondition || pvcCondition) { matched = true } }