Skip to content

Container can mount same vol at multiple places #75

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
Mar 23, 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
4 changes: 2 additions & 2 deletions pkg/devfile/parser/data/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ type DevfileData interface {
DeleteCommand(id string) error

// volume mount related methods
AddVolumeMounts(componentName string, volumeMounts []v1.VolumeMount) error
AddVolumeMounts(containerName string, volumeMounts []v1.VolumeMount) error
DeleteVolumeMount(name string) error
GetVolumeMountPath(mountName, componentName string) (string, error)
GetVolumeMountPaths(mountName, containerName string) ([]string, error)

// workspace related methods
GetDevfileWorkspace() *v1.DevWorkspaceTemplateSpecContent
Expand Down
26 changes: 16 additions & 10 deletions pkg/devfile/parser/data/v2/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

// AddVolumeMounts adds the volume mounts to the specified container component
func (d *DevfileV2) AddVolumeMounts(componentName string, volumeMounts []v1.VolumeMount) error {
func (d *DevfileV2) AddVolumeMounts(containerName string, volumeMounts []v1.VolumeMount) error {
var pathErrorContainers []string
found := false
for _, component := range d.Components {
if component.Container != nil && component.Name == componentName {
if component.Container != nil && component.Name == containerName {
found = true
for _, devfileVolumeMount := range component.Container.VolumeMounts {
for _, volumeMount := range volumeMounts {
Expand All @@ -31,7 +31,7 @@ func (d *DevfileV2) AddVolumeMounts(componentName string, volumeMounts []v1.Volu
if !found {
return &common.FieldNotFoundError{
Field: "container component",
Name: componentName,
Name: containerName,
}
}

Expand Down Expand Up @@ -70,27 +70,33 @@ func (d *DevfileV2) DeleteVolumeMount(name string) error {
return nil
}

// GetVolumeMountPath gets the mount path of the specified volume mount from the specified container component
func (d *DevfileV2) GetVolumeMountPath(mountName, componentName string) (string, error) {
// GetVolumeMountPaths gets all the mount paths of the specified volume mount from the specified container component.
// A container can mount at different paths for a given volume.
func (d *DevfileV2) GetVolumeMountPaths(mountName, containerName string) ([]string, error) {
componentFound := false
var mountPaths []string

for _, component := range d.Components {
if component.Container != nil && component.Name == componentName {
if component.Container != nil && component.Name == containerName {
componentFound = true
for _, volumeMount := range component.Container.VolumeMounts {
if volumeMount.Name == mountName {
return volumeMount.Path, nil
mountPaths = append(mountPaths, volumeMount.Path)
}
}
}
}

if !componentFound {
return "", &common.FieldNotFoundError{
return mountPaths, &common.FieldNotFoundError{
Field: "container component",
Name: componentName,
Name: containerName,
}
}

return "", fmt.Errorf("volume %s not mounted to component %s", mountName, componentName)
if len(mountPaths) == 0 {
return mountPaths, fmt.Errorf("volume %s not mounted to component %s", mountName, containerName)
}

return mountPaths, nil
}
48 changes: 31 additions & 17 deletions pkg/devfile/parser/data/v2/volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,14 @@ func TestDevfile200_DeleteVolumeMounts(t *testing.T) {

}

func TestDevfile200_GetVolumeMountPath(t *testing.T) {
volume1 := "volume1"
component1 := "component1"
func TestDevfile200_GetVolumeMountPaths(t *testing.T) {

tests := []struct {
name string
currentComponents []v1.Component
mountName string
componentName string
wantPath string
wantPaths []string
wantErr bool
}{
{
Expand All @@ -283,17 +281,18 @@ func TestDevfile200_GetVolumeMountPath(t *testing.T) {
Container: &v1.ContainerComponent{
Container: v1.Container{
VolumeMounts: []v1.VolumeMount{
testingutil.GetFakeVolumeMount(volume1, "/path"),
testingutil.GetFakeVolumeMount("volume1", "/path"),
testingutil.GetFakeVolumeMount("volume1", "/path2"),
},
},
},
},
Name: component1,
Name: "component1",
},
},
wantPath: "/path",
mountName: volume1,
componentName: component1,
wantPaths: []string{"/path", "/path2"},
mountName: "volume1",
componentName: "component1",
wantErr: false,
},
{
Expand All @@ -304,16 +303,16 @@ func TestDevfile200_GetVolumeMountPath(t *testing.T) {
Container: &v1.ContainerComponent{
Container: v1.Container{
VolumeMounts: []v1.VolumeMount{
testingutil.GetFakeVolumeMount(volume1, "/path"),
testingutil.GetFakeVolumeMount("volume1", "/path"),
},
},
},
},
Name: component1,
Name: "component1",
},
},
mountName: "volume2",
componentName: component1,
componentName: "component1",
wantErr: true,
},
{
Expand All @@ -324,15 +323,15 @@ func TestDevfile200_GetVolumeMountPath(t *testing.T) {
Container: &v1.ContainerComponent{
Container: v1.Container{
VolumeMounts: []v1.VolumeMount{
testingutil.GetFakeVolumeMount(volume1, "/path"),
testingutil.GetFakeVolumeMount("volume1", "/path"),
},
},
},
},
Name: component1,
Name: "component1",
},
},
mountName: volume1,
mountName: "volume1",
componentName: "component2",
wantErr: true,
},
Expand All @@ -348,11 +347,26 @@ func TestDevfile200_GetVolumeMountPath(t *testing.T) {
},
},
}
gotPath, err := d.GetVolumeMountPath(tt.mountName, tt.componentName)
gotPaths, err := d.GetVolumeMountPaths(tt.mountName, tt.componentName)
if (err != nil) != tt.wantErr {
t.Errorf("GetVolumeMountPath() error = %v, wantErr %v", err, tt.wantErr)
} else if err == nil {
assert.Equal(t, tt.wantPath, gotPath, "The two values should be the same.")
if len(gotPaths) != len(tt.wantPaths) {
t.Error("expected mount paths length not the same as actual mount paths length")
}

for _, wantPath := range tt.wantPaths {
matched := false
for _, gotPath := range gotPaths {
if wantPath == gotPath {
matched = true
}
}

if !matched {
t.Errorf("unable to find the wanted mount path %s in the actual mount paths slice", wantPath)
}
}
}
})
}
Expand Down