Skip to content

Commit

Permalink
Merge pull request #121 from yangcao77/642-emphemeral-true-vol
Browse files Browse the repository at this point in the history
create volume with emptyDir if ephemeral is true
  • Loading branch information
yangcao77 authored Oct 19, 2021
2 parents ab7bd9b + edf6e75 commit 0a4f50f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 10 deletions.
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

0 comments on commit 0a4f50f

Please sign in to comment.