Skip to content

Commit ee6ffb6

Browse files
committed
Update volume mount funcs and delete funcs
Signed-off-by: Maysun J Faisal <[email protected]>
1 parent 329c600 commit ee6ffb6

File tree

7 files changed

+170
-363
lines changed

7 files changed

+170
-363
lines changed

pkg/devfile/parser/data/interface.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ type DevfileData interface {
4646
UpdateCommand(command v1.Command)
4747
DeleteCommand(id string) error
4848

49-
// volume related methods
50-
AddVolume(volume v1.Component, path string) error
51-
DeleteVolume(name string) error
52-
GetVolumeMountPath(name string) (string, error)
49+
// volume mount related methods
50+
AddVolumeMount(componentName, name, path string) error
51+
DeleteVolumeMount(name string) error
52+
GetVolumeMountPath(mountName, componentName string) (string, error)
5353

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

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

+1-9
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,7 @@ func (d *DevfileV2) DeleteCommand(id string) error {
5959

6060
found := false
6161
for i := len(d.Commands) - 1; i >= 0; i-- {
62-
if d.Commands[i].Composite != nil && d.Commands[i].Id != id {
63-
var subCmd []string
64-
for _, command := range d.Commands[i].Composite.Commands {
65-
if command != id {
66-
subCmd = append(subCmd, command)
67-
}
68-
}
69-
d.Commands[i].Composite.Commands = subCmd
70-
} else if d.Commands[i].Id == id {
62+
if d.Commands[i].Id == id {
7163
found = true
7264
d.Commands = append(d.Commands[:i], d.Commands[i+1:]...)
7365
}

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

+1-46
Original file line numberDiff line numberDiff line change
@@ -347,52 +347,7 @@ func TestDeleteCommands(t *testing.T) {
347347
Id: "command3",
348348
CommandUnion: v1.CommandUnion{
349349
Composite: &v1.CompositeCommand{
350-
Commands: []string{"command2"},
351-
},
352-
},
353-
},
354-
},
355-
wantErr: false,
356-
},
357-
{
358-
name: "Commands that do not belong to Composite Command",
359-
commandToDelete: "command3",
360-
commands: []v1.Command{
361-
{
362-
Id: "command1",
363-
CommandUnion: v1.CommandUnion{
364-
Exec: &v1.ExecCommand{},
365-
},
366-
},
367-
{
368-
Id: "command2",
369-
CommandUnion: v1.CommandUnion{
370-
Composite: &v1.CompositeCommand{
371-
Commands: []string{"command1"},
372-
},
373-
},
374-
},
375-
{
376-
Id: "command3",
377-
CommandUnion: v1.CommandUnion{
378-
Composite: &v1.CompositeCommand{
379-
Commands: []string{"command1"},
380-
},
381-
},
382-
},
383-
},
384-
wantCommands: []v1.Command{
385-
{
386-
Id: "command1",
387-
CommandUnion: v1.CommandUnion{
388-
Exec: &v1.ExecCommand{},
389-
},
390-
},
391-
{
392-
Id: "command2",
393-
CommandUnion: v1.CommandUnion{
394-
Composite: &v1.CompositeCommand{
395-
Commands: []string{"command1"},
350+
Commands: []string{"command1", "command2", "command1"},
396351
},
397352
},
398353
},

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

+1-9
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,7 @@ func (d *DevfileV2) DeleteComponent(name string) error {
9090

9191
found := false
9292
for i := len(d.Components) - 1; i >= 0; i-- {
93-
if d.Components[i].Container != nil && d.Components[i].Name != name {
94-
var tmp []v1.VolumeMount
95-
for _, volumeMount := range d.Components[i].Container.VolumeMounts {
96-
if volumeMount.Name != name {
97-
tmp = append(tmp, volumeMount)
98-
}
99-
}
100-
d.Components[i].Container.VolumeMounts = tmp
101-
} else if d.Components[i].Name == name {
93+
if d.Components[i].Name == name {
10294
found = true
10395
d.Components = append(d.Components[:i], d.Components[i+1:]...)
10496
}

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

+1-45
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ func TestDeleteComponents(t *testing.T) {
454454
VolumeMounts: []v1.VolumeMount{
455455
testingutil.GetFakeVolumeMount("comp2", "/path"),
456456
testingutil.GetFakeVolumeMount("comp2", "/path2"),
457+
testingutil.GetFakeVolumeMount("comp3", "/path"),
457458
},
458459
},
459460
},
@@ -468,51 +469,6 @@ func TestDeleteComponents(t *testing.T) {
468469
},
469470
wantErr: false,
470471
},
471-
{
472-
name: "Non Volume Component",
473-
componentToDelete: "comp1",
474-
components: []v1.Component{
475-
{
476-
Name: "comp1",
477-
ComponentUnion: v1.ComponentUnion{
478-
Container: &v1.ContainerComponent{
479-
Container: v1.Container{
480-
VolumeMounts: []v1.VolumeMount{
481-
testingutil.GetFakeVolumeMount("comp2", "/path"),
482-
},
483-
},
484-
},
485-
},
486-
},
487-
{
488-
Name: "comp2",
489-
ComponentUnion: v1.ComponentUnion{
490-
Volume: &v1.VolumeComponent{},
491-
},
492-
},
493-
{
494-
Name: "comp3",
495-
ComponentUnion: v1.ComponentUnion{
496-
Kubernetes: &v1.KubernetesComponent{},
497-
},
498-
},
499-
},
500-
wantComponents: []v1.Component{
501-
{
502-
Name: "comp2",
503-
ComponentUnion: v1.ComponentUnion{
504-
Volume: &v1.VolumeComponent{},
505-
},
506-
},
507-
{
508-
Name: "comp3",
509-
ComponentUnion: v1.ComponentUnion{
510-
Kubernetes: &v1.KubernetesComponent{},
511-
},
512-
},
513-
},
514-
wantErr: false,
515-
},
516472
{
517473
name: "Missing Component",
518474
componentToDelete: "comp12",

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

+50-35
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,90 @@ import (
88
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
99
)
1010

11-
// AddVolume adds the volume to the devFile and mounts it to all the container components
12-
func (d *DevfileV2) AddVolume(volumeComponent v1.Component, path string) error {
13-
volumeExists := false
11+
// AddVolumeMount adds the volume mount to the specified container component
12+
func (d *DevfileV2) AddVolumeMount(componentName, name, path string) error {
1413
var pathErrorContainers []string
14+
found := false
1515
for _, component := range d.Components {
16-
if component.Container != nil {
16+
if component.Container != nil && component.Name == componentName {
17+
found = true
1718
for _, volumeMount := range component.Container.VolumeMounts {
1819
if volumeMount.Path == path {
19-
var err = fmt.Errorf("another volume, %s, is mounted to the same path: %s, on the container: %s", volumeMount.Name, path, component.Name)
20+
var err = fmt.Errorf("another volume, %s, is mounted to the same path: %s, in the container: %s", volumeMount.Name, path, component.Name)
2021
pathErrorContainers = append(pathErrorContainers, err.Error())
2122
}
2223
}
2324
component.Container.VolumeMounts = append(component.Container.VolumeMounts, v1.VolumeMount{
24-
Name: volumeComponent.Name,
25+
Name: name,
2526
Path: path,
2627
})
27-
} else if component.Volume != nil && component.Name == volumeComponent.Name {
28-
volumeExists = true
29-
break
3028
}
3129
}
3230

33-
if volumeExists {
34-
return &common.FieldAlreadyExistError{
35-
Field: "volume",
36-
Name: volumeComponent.Name,
31+
if !found {
32+
return &common.FieldNotFoundError{
33+
Field: "container component",
34+
Name: componentName,
3735
}
3836
}
3937

4038
if len(pathErrorContainers) > 0 {
41-
return fmt.Errorf("errors while creating volume:\n%s", strings.Join(pathErrorContainers, "\n"))
39+
return fmt.Errorf("errors while adding volume mounts:\n%s", strings.Join(pathErrorContainers, "\n"))
4240
}
4341

44-
d.Components = append(d.Components, volumeComponent)
45-
4642
return nil
4743
}
4844

49-
// DeleteVolume removes the volume from the devFile and removes all the related volume mounts
50-
func (d *DevfileV2) DeleteVolume(name string) error {
45+
// DeleteVolumeMount deletes the volume mount from container components
46+
func (d *DevfileV2) DeleteVolumeMount(name string) error {
47+
found := false
48+
for i := range d.Components {
49+
if d.Components[i].Container != nil && d.Components[i].Name != name {
50+
for j := len(d.Components[i].Container.VolumeMounts) - 1; j >= 0; j-- {
51+
if d.Components[i].Container.VolumeMounts[j].Name == name {
52+
found = true
53+
d.Components[i].Container.VolumeMounts = append(d.Components[i].Container.VolumeMounts[:j], d.Components[i].Container.VolumeMounts[j+1:]...)
54+
}
55+
}
56+
}
57+
}
5158

52-
return d.DeleteComponent(name)
59+
if !found {
60+
return &common.FieldNotFoundError{
61+
Field: "volume mount",
62+
Name: name,
63+
}
64+
}
65+
66+
return nil
5367
}
5468

55-
// GetVolumeMountPath gets the mount path of the required volume
56-
func (d *DevfileV2) GetVolumeMountPath(name string) (string, error) {
57-
volumeFound := false
69+
// GetVolumeMountPath gets the mount path of the specified volume mount from the specified container component
70+
func (d *DevfileV2) GetVolumeMountPath(mountName, componentName string) (string, error) {
5871
mountFound := false
59-
path := ""
72+
componentFound := false
73+
var path string
6074

6175
for _, component := range d.Components {
62-
if component.Container != nil {
76+
if component.Container != nil && component.Name == componentName {
77+
componentFound = true
6378
for _, volumeMount := range component.Container.VolumeMounts {
64-
if volumeMount.Name == name {
79+
if volumeMount.Name == mountName {
6580
mountFound = true
6681
path = volumeMount.Path
6782
}
6883
}
69-
} else if component.Volume != nil {
70-
volumeFound = true
7184
}
7285
}
73-
if volumeFound && mountFound {
74-
return path, nil
75-
} else if !mountFound && volumeFound {
76-
return "", fmt.Errorf("volume not mounted to any component")
77-
}
78-
return "", &common.FieldNotFoundError{
79-
Field: "volume",
80-
Name: "name",
86+
87+
if !componentFound {
88+
return "", &common.FieldNotFoundError{
89+
Field: "container component",
90+
Name: componentName,
91+
}
92+
} else if !mountFound {
93+
return "", fmt.Errorf("volume %s not mounted to component %s", mountName, componentName)
8194
}
95+
96+
return path, nil
8297
}

0 commit comments

Comments
 (0)