-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
251 lines (208 loc) · 4.83 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package mount
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/proact-de/vcloud-csi-driver/pkg/model"
"github.com/rs/zerolog/log"
"k8s.io/mount-utils"
"k8s.io/utils/exec"
)
const (
// DefaultFilesystem defines the standard filesystem if nothing else is provided.
DefaultFilesystem = "ext4"
)
// Service defines the service for the mount component.
type Service struct {
options Options
mounter *mount.SafeFormatAndMount
}
// NewService simply initializes a new mount service.
func NewService(opts ...Option) *Service {
options := newOptions(opts...)
return &Service{
options: options,
mounter: &mount.SafeFormatAndMount{
Interface: mount.New(""),
Exec: exec.New(),
},
}
}
// PublishOpts defines the available options for the publish handler.
type PublishOpts struct {
Volume *model.Volume
Target string
Staging string
IsBlock bool
Readonly bool
FSType string
MountFlags []string
}
// Publish is publishing the request volume.
func (s *Service) Publish(ctx context.Context, opts PublishOpts) error {
if opts.IsBlock {
if err := os.MkdirAll(
filepath.Dir(opts.Target),
os.FileMode(0750),
); err != nil {
return err
}
mountFile, err := os.OpenFile(
opts.Target,
os.O_CREATE,
os.FileMode(0660),
)
if err != nil {
return err
}
mountFile.Close()
} else {
if opts.FSType == "" {
opts.FSType = DefaultFilesystem
}
if err := os.MkdirAll(
opts.Target,
os.FileMode(0750),
); err != nil {
return err
}
}
options := []string{
"bind",
}
if opts.Readonly {
options = append(options, "ro")
}
options = append(options, opts.MountFlags...)
log.Info().
Str("name", opts.Volume.Name).
Str("target", opts.Target).
Str("staging", opts.Staging).
Str("fs", opts.FSType).
Bool("block", opts.IsBlock).
Str("options", strings.Join(options, ", ")).
Msg("Publishing volume")
if err := s.mounter.Interface.Mount(
opts.Staging,
opts.Target,
opts.FSType,
options,
); err != nil {
log.Error().
Err(err).
Str("name", opts.Volume.Name).
Str("target", opts.Target).
Str("staging", opts.Staging).
Str("fs", opts.FSType).
Bool("block", opts.IsBlock).
Str("options", strings.Join(options, ", ")).
Msg("Failed to publish volume")
return err
}
return nil
}
// UnpublishOpts defines the available options for the unpublish handler.
type UnpublishOpts struct {
Volume *model.Volume
Target string
}
// Unpublish is unpublishing the request volume.
func (s *Service) Unpublish(ctx context.Context, opts UnpublishOpts) error {
log.Info().
Str("name", opts.Volume.Name).
Str("target", opts.Target).
Msg("Unpublishing volume")
if err := s.mounter.Interface.Unmount(opts.Target); err != nil {
log.Error().
Err(err).
Str("name", opts.Volume.Name).
Str("target", opts.Target).
Msg("Failed to unpublish volume")
return err
}
return nil
}
// StageOpts defines the available options for the stage handler.
type StageOpts struct {
Volume *model.Volume
Target string
FSType string
MountFlags []string
}
// Stage is staging the request volume.
func (s *Service) Stage(ctx context.Context, opts StageOpts) error {
if opts.FSType == "" {
opts.FSType = DefaultFilesystem
}
log.Info().
Str("name", opts.Volume.Name).
Str("target", opts.Target).
Str("fs", opts.FSType).
Msg("Staging volume")
isNotMountPoint, err := s.mounter.Interface.IsLikelyNotMountPoint(opts.Target)
if err != nil {
if os.IsNotExist(err) {
if err := os.Mkdir(
opts.Target,
os.FileMode(0750),
); err != nil {
return err
}
isNotMountPoint = true
} else {
return err
}
}
if !isNotMountPoint {
return fmt.Errorf("%q is not a valid mount point", opts.Target)
}
if err := s.mounter.FormatAndMount(
opts.Volume.Device,
opts.Target,
opts.FSType,
nil,
); err != nil {
log.Error().
Err(err).
Str("name", opts.Volume.Name).
Str("target", opts.Target).
Str("fs", opts.FSType).
Msg("Failed to stage volume")
return err
}
return nil
}
// UnstageOpts defines the available options for the unstage handler.
type UnstageOpts struct {
Volume *model.Volume
Target string
}
// Unstage is unstaging the request volume.
func (s *Service) Unstage(ctx context.Context, opts UnstageOpts) error {
log.Info().
Str("name", opts.Volume.Name).
Str("target", opts.Target).
Msg("Unstaging volume")
if err := s.mounter.Interface.Unmount(opts.Target); err != nil {
log.Error().
Err(err).
Str("name", opts.Volume.Name).
Str("target", opts.Target).
Msg("Failed to unstage volume")
return err
}
return nil
}
// Exists is checking if volume path exists.
func (s *Service) Exists(ctx context.Context, target string) (bool, error) {
_, err := os.Stat(target)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}