Skip to content

Commit 3d70885

Browse files
authored
Merge pull request #75 from maysunfaisal/updateVolMount-1
Container can mount same vol at multiple places
2 parents 64d0bf9 + b72a7ab commit 3d70885

File tree

3 files changed

+49
-29
lines changed

3 files changed

+49
-29
lines changed

pkg/devfile/parser/data/interface.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ type DevfileData interface {
4747
DeleteCommand(id string) error
4848

4949
// volume mount related methods
50-
AddVolumeMounts(componentName string, volumeMounts []v1.VolumeMount) error
50+
AddVolumeMounts(containerName string, volumeMounts []v1.VolumeMount) error
5151
DeleteVolumeMount(name string) error
52-
GetVolumeMountPath(mountName, componentName string) (string, error)
52+
GetVolumeMountPaths(mountName, containerName string) ([]string, error)
5353

5454
// workspace related methods
5555
GetDevfileWorkspace() *v1.DevWorkspaceTemplateSpecContent

pkg/devfile/parser/data/v2/volumes.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
)
1010

1111
// AddVolumeMounts adds the volume mounts to the specified container component
12-
func (d *DevfileV2) AddVolumeMounts(componentName string, volumeMounts []v1.VolumeMount) error {
12+
func (d *DevfileV2) AddVolumeMounts(containerName string, volumeMounts []v1.VolumeMount) error {
1313
var pathErrorContainers []string
1414
found := false
1515
for _, component := range d.Components {
16-
if component.Container != nil && component.Name == componentName {
16+
if component.Container != nil && component.Name == containerName {
1717
found = true
1818
for _, devfileVolumeMount := range component.Container.VolumeMounts {
1919
for _, volumeMount := range volumeMounts {
@@ -31,7 +31,7 @@ func (d *DevfileV2) AddVolumeMounts(componentName string, volumeMounts []v1.Volu
3131
if !found {
3232
return &common.FieldNotFoundError{
3333
Field: "container component",
34-
Name: componentName,
34+
Name: containerName,
3535
}
3636
}
3737

@@ -70,27 +70,33 @@ func (d *DevfileV2) DeleteVolumeMount(name string) error {
7070
return nil
7171
}
7272

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

7779
for _, component := range d.Components {
78-
if component.Container != nil && component.Name == componentName {
80+
if component.Container != nil && component.Name == containerName {
7981
componentFound = true
8082
for _, volumeMount := range component.Container.VolumeMounts {
8183
if volumeMount.Name == mountName {
82-
return volumeMount.Path, nil
84+
mountPaths = append(mountPaths, volumeMount.Path)
8385
}
8486
}
8587
}
8688
}
8789

8890
if !componentFound {
89-
return "", &common.FieldNotFoundError{
91+
return mountPaths, &common.FieldNotFoundError{
9092
Field: "container component",
91-
Name: componentName,
93+
Name: containerName,
9294
}
9395
}
9496

95-
return "", fmt.Errorf("volume %s not mounted to component %s", mountName, componentName)
97+
if len(mountPaths) == 0 {
98+
return mountPaths, fmt.Errorf("volume %s not mounted to component %s", mountName, containerName)
99+
}
100+
101+
return mountPaths, nil
96102
}

pkg/devfile/parser/data/v2/volumes_test.go

+31-17
Original file line numberDiff line numberDiff line change
@@ -263,16 +263,14 @@ func TestDevfile200_DeleteVolumeMounts(t *testing.T) {
263263

264264
}
265265

266-
func TestDevfile200_GetVolumeMountPath(t *testing.T) {
267-
volume1 := "volume1"
268-
component1 := "component1"
266+
func TestDevfile200_GetVolumeMountPaths(t *testing.T) {
269267

270268
tests := []struct {
271269
name string
272270
currentComponents []v1.Component
273271
mountName string
274272
componentName string
275-
wantPath string
273+
wantPaths []string
276274
wantErr bool
277275
}{
278276
{
@@ -283,17 +281,18 @@ func TestDevfile200_GetVolumeMountPath(t *testing.T) {
283281
Container: &v1.ContainerComponent{
284282
Container: v1.Container{
285283
VolumeMounts: []v1.VolumeMount{
286-
testingutil.GetFakeVolumeMount(volume1, "/path"),
284+
testingutil.GetFakeVolumeMount("volume1", "/path"),
285+
testingutil.GetFakeVolumeMount("volume1", "/path2"),
287286
},
288287
},
289288
},
290289
},
291-
Name: component1,
290+
Name: "component1",
292291
},
293292
},
294-
wantPath: "/path",
295-
mountName: volume1,
296-
componentName: component1,
293+
wantPaths: []string{"/path", "/path2"},
294+
mountName: "volume1",
295+
componentName: "component1",
297296
wantErr: false,
298297
},
299298
{
@@ -304,16 +303,16 @@ func TestDevfile200_GetVolumeMountPath(t *testing.T) {
304303
Container: &v1.ContainerComponent{
305304
Container: v1.Container{
306305
VolumeMounts: []v1.VolumeMount{
307-
testingutil.GetFakeVolumeMount(volume1, "/path"),
306+
testingutil.GetFakeVolumeMount("volume1", "/path"),
308307
},
309308
},
310309
},
311310
},
312-
Name: component1,
311+
Name: "component1",
313312
},
314313
},
315314
mountName: "volume2",
316-
componentName: component1,
315+
componentName: "component1",
317316
wantErr: true,
318317
},
319318
{
@@ -324,15 +323,15 @@ func TestDevfile200_GetVolumeMountPath(t *testing.T) {
324323
Container: &v1.ContainerComponent{
325324
Container: v1.Container{
326325
VolumeMounts: []v1.VolumeMount{
327-
testingutil.GetFakeVolumeMount(volume1, "/path"),
326+
testingutil.GetFakeVolumeMount("volume1", "/path"),
328327
},
329328
},
330329
},
331330
},
332-
Name: component1,
331+
Name: "component1",
333332
},
334333
},
335-
mountName: volume1,
334+
mountName: "volume1",
336335
componentName: "component2",
337336
wantErr: true,
338337
},
@@ -348,11 +347,26 @@ func TestDevfile200_GetVolumeMountPath(t *testing.T) {
348347
},
349348
},
350349
}
351-
gotPath, err := d.GetVolumeMountPath(tt.mountName, tt.componentName)
350+
gotPaths, err := d.GetVolumeMountPaths(tt.mountName, tt.componentName)
352351
if (err != nil) != tt.wantErr {
353352
t.Errorf("GetVolumeMountPath() error = %v, wantErr %v", err, tt.wantErr)
354353
} else if err == nil {
355-
assert.Equal(t, tt.wantPath, gotPath, "The two values should be the same.")
354+
if len(gotPaths) != len(tt.wantPaths) {
355+
t.Error("expected mount paths length not the same as actual mount paths length")
356+
}
357+
358+
for _, wantPath := range tt.wantPaths {
359+
matched := false
360+
for _, gotPath := range gotPaths {
361+
if wantPath == gotPath {
362+
matched = true
363+
}
364+
}
365+
366+
if !matched {
367+
t.Errorf("unable to find the wanted mount path %s in the actual mount paths slice", wantPath)
368+
}
369+
}
356370
}
357371
})
358372
}

0 commit comments

Comments
 (0)