Skip to content

create volume with emptyDir if ephemeral is true #121

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

Merged
merged 2 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 22 additions & 1 deletion pkg/devfile/generator/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
77 changes: 68 additions & 9 deletions pkg/devfile/generator/generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand All @@ -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{
Expand All @@ -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))

}

Expand All @@ -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 {
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
}
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/devfile/generator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down