@@ -8,99 +8,89 @@ 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
+ // AddVolumeMounts adds the volume mounts to the specified container component
12
+ func (d * DevfileV2 ) AddVolumeMounts (componentName string , volumeMounts []v1.VolumeMount ) error {
14
13
var pathErrorContainers []string
14
+ found := false
15
15
for _ , component := range d .Components {
16
- if component .Container != nil {
17
- for _ , volumeMount := range component .Container .VolumeMounts {
18
- 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
- pathErrorContainers = append (pathErrorContainers , err .Error ())
16
+ if component .Container != nil && component .Name == componentName {
17
+ found = true
18
+ for _ , devfileVolumeMount := range component .Container .VolumeMounts {
19
+ for _ , volumeMount := range volumeMounts {
20
+ if devfileVolumeMount .Path == volumeMount .Path {
21
+ pathErrorContainers = append (pathErrorContainers , fmt .Sprintf ("unable to mount volume %s, as another volume %s is mounted to the same path %s in the container %s" , volumeMount .Name , devfileVolumeMount .Name , volumeMount .Path , component .Name ))
22
+ }
21
23
}
22
24
}
23
- component .Container .VolumeMounts = append (component .Container .VolumeMounts , v1.VolumeMount {
24
- Name : volumeComponent .Name ,
25
- Path : path ,
26
- })
27
- } else if component .Volume != nil && component .Name == volumeComponent .Name {
28
- volumeExists = true
29
- break
25
+ if len (pathErrorContainers ) == 0 {
26
+ component .Container .VolumeMounts = append (component .Container .VolumeMounts , volumeMounts ... )
27
+ }
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 {
51
47
found := false
52
- for i := len (d .Components ) - 1 ; i >= 0 ; i -- {
53
- if d .Components [i ].Container != nil {
54
- var tmp []v1.VolumeMount
55
- for _ , volumeMount := range d .Components [i ].Container .VolumeMounts {
56
- if volumeMount .Name != name {
57
- tmp = append (tmp , volumeMount )
48
+ for i := range d .Components {
49
+ if d .Components [i ].Container != nil && d .Components [i ].Name != name {
50
+ // Volume Mounts can have multiple instances of a volume mounted at different paths
51
+ // As arrays are rearraged/shifted for deletion, we lose one element every time there is a match
52
+ // Looping backward is efficient, otherwise we would have to manually decrement counter
53
+ // if we looped forward
54
+ for j := len (d .Components [i ].Container .VolumeMounts ) - 1 ; j >= 0 ; j -- {
55
+ if d .Components [i ].Container .VolumeMounts [j ].Name == name {
56
+ found = true
57
+ d .Components [i ].Container .VolumeMounts = append (d .Components [i ].Container .VolumeMounts [:j ], d .Components [i ].Container .VolumeMounts [j + 1 :]... )
58
58
}
59
59
}
60
- d .Components [i ].Container .VolumeMounts = tmp
61
- } else if d .Components [i ].Volume != nil {
62
- if d .Components [i ].Name == name {
63
- found = true
64
- d .Components = append (d .Components [:i ], d .Components [i + 1 :]... )
65
- }
66
60
}
67
61
}
68
62
69
63
if ! found {
70
64
return & common.FieldNotFoundError {
71
- Field : "volume" ,
65
+ Field : "volume mount " ,
72
66
Name : name ,
73
67
}
74
68
}
75
69
76
70
return nil
77
71
}
78
72
79
- // GetVolumeMountPath gets the mount path of the required volume
80
- func (d * DevfileV2 ) GetVolumeMountPath (name string ) (string , error ) {
81
- volumeFound := false
82
- mountFound := false
83
- path := ""
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 ) {
75
+ componentFound := false
84
76
85
77
for _ , component := range d .Components {
86
- if component .Container != nil {
78
+ if component .Container != nil && component .Name == componentName {
79
+ componentFound = true
87
80
for _ , volumeMount := range component .Container .VolumeMounts {
88
- if volumeMount .Name == name {
89
- mountFound = true
90
- path = volumeMount .Path
81
+ if volumeMount .Name == mountName {
82
+ return volumeMount .Path , nil
91
83
}
92
84
}
93
- } else if component .Volume != nil {
94
- volumeFound = true
95
85
}
96
86
}
97
- if volumeFound && mountFound {
98
- return path , nil
99
- } else if ! mountFound && volumeFound {
100
- return "" , fmt .Errorf ("volume not mounted to any component" )
101
- }
102
- return "" , & common.FieldNotFoundError {
103
- Field : "volume" ,
104
- Name : "name" ,
87
+
88
+ if ! componentFound {
89
+ return "" , & common.FieldNotFoundError {
90
+ Field : "container component" ,
91
+ Name : componentName ,
92
+ }
105
93
}
94
+
95
+ return "" , fmt .Errorf ("volume %s not mounted to component %s" , mountName , componentName )
106
96
}
0 commit comments