@@ -8,75 +8,90 @@ import (
8
8
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
9
9
)
10
10
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 {
14
13
var pathErrorContainers []string
14
+ found := false
15
15
for _ , component := range d .Components {
16
- if component .Container != nil {
16
+ if component .Container != nil && component .Name == componentName {
17
+ found = true
17
18
for _ , volumeMount := range component .Container .VolumeMounts {
18
19
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 )
20
21
pathErrorContainers = append (pathErrorContainers , err .Error ())
21
22
}
22
23
}
23
24
component .Container .VolumeMounts = append (component .Container .VolumeMounts , v1.VolumeMount {
24
- Name : volumeComponent . Name ,
25
+ Name : name ,
25
26
Path : path ,
26
27
})
27
- } else if component .Volume != nil && component .Name == volumeComponent .Name {
28
- volumeExists = true
29
- break
30
28
}
31
29
}
32
30
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 ,
37
35
}
38
36
}
39
37
40
38
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 " ))
42
40
}
43
41
44
- d .Components = append (d .Components , volumeComponent )
45
-
46
42
return nil
47
43
}
48
44
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
+ }
51
58
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
53
67
}
54
68
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 ) {
58
71
mountFound := false
59
- path := ""
72
+ componentFound := false
73
+ var path string
60
74
61
75
for _ , component := range d .Components {
62
- if component .Container != nil {
76
+ if component .Container != nil && component .Name == componentName {
77
+ componentFound = true
63
78
for _ , volumeMount := range component .Container .VolumeMounts {
64
- if volumeMount .Name == name {
79
+ if volumeMount .Name == mountName {
65
80
mountFound = true
66
81
path = volumeMount .Path
67
82
}
68
83
}
69
- } else if component .Volume != nil {
70
- volumeFound = true
71
84
}
72
85
}
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 )
81
94
}
95
+
96
+ return path , nil
82
97
}
0 commit comments