Skip to content

Commit 34cde26

Browse files
committed
PR comments
Signed-off-by: Lukas Frank <lukas.frank@sap.com> Signed-off-by: Lukas Frank <lukas.frank@sap.com>
1 parent 53c90cc commit 34cde26

5 files changed

Lines changed: 66 additions & 40 deletions

File tree

‎internal/controllers/machine_controller.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func (r *MachineReconciler) deleteMachine(ctx context.Context, log logr.Logger,
240240
}
241241

242242
log.V(2).Info("Delete volume", "name", vol.Name, "plugin", plugin.Name())
243-
if err := plugin.Delete(ctx, vol.Name, machine.ID); err != nil {
243+
if err := plugin.Delete(ctx, vol, machine.ID); err != nil {
244244
return fmt.Errorf("failed to delete volume %s: %w", vol.Name, err)
245245
}
246246
}
@@ -285,7 +285,7 @@ func (r *MachineReconciler) reconcileVolumes(ctx context.Context, log logr.Logge
285285
if vol.DeletedAt != nil {
286286
if status.State != api.VolumeStateAttached {
287287
log.V(2).Info("Delete not attached volume", "name", vol.Name)
288-
if err := plugin.Delete(ctx, vol.Name, machine.ID); err != nil {
288+
if err := plugin.Delete(ctx, vol, machine.ID); err != nil {
289289
return fmt.Errorf("failed to delete volume %s: %w", vol.Name, err)
290290
}
291291
continue
@@ -397,7 +397,7 @@ func (r *MachineReconciler) attachDetachDisks(
397397
if err != nil {
398398
return fmt.Errorf("failed to find plugin for volume %s: %w", vol.Name, err)
399399
}
400-
if err := plugin.Delete(ctx, vol.Name, machine.ID); err != nil {
400+
if err := plugin.Delete(ctx, vol, machine.ID); err != nil {
401401
return fmt.Errorf("failed to cleanup volume %s: %w", vol.Name, err)
402402
}
403403
log.V(1).Info("Cleaned up volume", "disk", vol.Name)

‎internal/plugins/volume/ceph/ceph.go‎

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type validatedVolume struct {
4545
type backend interface {
4646
VolumeType() api.VolumeType
4747
Mount(ctx context.Context, machineID string, volume *validatedVolume) (string, error)
48-
Unmount(ctx context.Context, machineID string, volumeID string) error
48+
Unmount(ctx context.Context, machineID string, volume *validatedVolume) error
4949
}
5050

5151
// NewRBDPlugin creates a volume.Plugin backed by host-level rbd map/unmap.
@@ -212,10 +212,43 @@ func (p *plugin) validateVolume(spec *api.VolumeSpec) (vData *validatedVolume, e
212212
return vData, nil
213213
}
214214

215-
func (p *plugin) Delete(ctx context.Context, computeVolumeName string, machineID string) error {
216-
if err := p.be.Unmount(ctx, machineID, computeVolumeName); err != nil {
217-
return fmt.Errorf("failed to unmount volume %q: %w", computeVolumeName, err)
215+
func (p *plugin) Delete(ctx context.Context, spec *api.VolumeSpec, machineID string) error {
216+
volume, err := p.volumeForUnmount(spec)
217+
if err != nil {
218+
return fmt.Errorf("failed to get volume data for deletion: %w", err)
219+
}
220+
221+
if err := p.be.Unmount(ctx, machineID, volume); err != nil {
222+
return fmt.Errorf("failed to unmount volume %q: %w", spec.Name, err)
218223
}
219224

220-
return os.RemoveAll(p.host.MachineVolumeDir(machineID, cephDriverName, computeVolumeName))
225+
return os.RemoveAll(p.host.MachineVolumeDir(machineID, cephDriverName, spec.Name))
226+
}
227+
228+
func (p *plugin) volumeForUnmount(spec *api.VolumeSpec) (*validatedVolume, error) {
229+
if spec == nil {
230+
return nil, fmt.Errorf("volume is nil")
231+
}
232+
connection := spec.Connection
233+
if connection == nil {
234+
return nil, fmt.Errorf("volume does not specify connection")
235+
}
236+
if connection.Driver != cephDriverName {
237+
return nil, fmt.Errorf("volume connection specifies invalid driver %q", connection.Driver)
238+
}
239+
if connection.Attributes == nil {
240+
return nil, fmt.Errorf("volume connection does not specify attributes")
241+
}
242+
if connection.Handle == "" {
243+
return nil, fmt.Errorf("volume connection does not specify handle")
244+
}
245+
246+
volume := &validatedVolume{
247+
name: spec.Name,
248+
handle: connection.Handle,
249+
}
250+
if err := readVolumeAttributes(connection.Attributes, volume); err != nil {
251+
return nil, fmt.Errorf("error reading volume attributes: %w", err)
252+
}
253+
return volume, nil
221254
}

‎internal/plugins/volume/ceph/rbd.go‎

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (r *rbdProvider) VolumeType() api.VolumeType {
3636

3737
func (r *rbdProvider) Mount(ctx context.Context, machineID string, volume *validatedVolume) (string, error) {
3838
volumeDir := r.volumeDir(machineID, volume.name)
39-
if err := os.MkdirAll(volumeDir, os.ModePerm); err != nil {
39+
if err := os.MkdirAll(volumeDir, 0o700); err != nil {
4040
return "", err
4141
}
4242

@@ -55,7 +55,6 @@ func (r *rbdProvider) Mount(ctx context.Context, machineID string, volume *valid
5555

5656
if devicePath != "" {
5757
log.V(1).Info("RBD image already mapped", "device", devicePath)
58-
r.persistDevicePath(volumeDir, devicePath)
5958
return devicePath, nil
6059
}
6160

@@ -65,39 +64,27 @@ func (r *rbdProvider) Mount(ctx context.Context, machineID string, volume *valid
6564
}
6665

6766
log.V(1).Info("Mapped RBD image to device", "device", devicePath)
68-
r.persistDevicePath(volumeDir, devicePath)
6967
return devicePath, nil
7068
}
7169

72-
func (r *rbdProvider) Unmount(ctx context.Context, machineID string, volumeName string) error {
73-
log := r.log.WithValues("machineID", machineID, "volumeName", volumeName)
70+
func (r *rbdProvider) Unmount(ctx context.Context, machineID string, volume *validatedVolume) error {
71+
log := r.log.WithValues("machineID", machineID, "volumeName", volume.name)
7472

75-
volumeDir := r.paths.MachineVolumeDir(machineID, cephDriverName, volumeName)
76-
if devicePath, err := r.readDevicePath(volumeDir); err == nil && devicePath != "" {
77-
log.V(1).Info("Unmapping RBD device from persisted path", "device", devicePath)
78-
if err := r.unmapDevice(ctx, devicePath); err != nil {
79-
return fmt.Errorf("error unmapping rbd device %s: %w", devicePath, err)
80-
}
81-
_ = os.Remove(filepath.Join(volumeDir, "device"))
82-
return nil
73+
device, err := r.findMappedDevice(volume.pool, volume.image)
74+
if err != nil {
75+
return fmt.Errorf("error querying rbd mappings: %w", err)
8376
}
8477

85-
log.V(1).Info("No persisted device path found, nothing to unmap")
86-
return nil
87-
}
88-
89-
const deviceFileName = "device"
90-
91-
func (r *rbdProvider) persistDevicePath(volumeDir, devicePath string) {
92-
_ = os.WriteFile(filepath.Join(volumeDir, deviceFileName), []byte(devicePath), 0o644)
93-
}
78+
if device == "" {
79+
log.V(1).Info("RBD image not currently mapped, nothing to unmap", "pool", volume.pool, "image", volume.image)
80+
return nil
81+
}
9482

95-
func (r *rbdProvider) readDevicePath(volumeDir string) (string, error) {
96-
data, err := os.ReadFile(filepath.Join(volumeDir, deviceFileName))
97-
if err != nil {
98-
return "", err
83+
log.V(1).Info("Unmapping RBD device", "device", device)
84+
if err := r.unmapDevice(ctx, device); err != nil {
85+
return fmt.Errorf("error unmapping rbd device %s: %w", device, err)
9986
}
100-
return strings.TrimSpace(string(data)), nil
87+
return nil
10188
}
10289

10390
func (r *rbdProvider) volumeDir(machineID string, volumeHandle string) string {
@@ -121,15 +108,21 @@ func (r *rbdProvider) createCephConf(log logr.Logger, machineID string, volume *
121108
volume.userID,
122109
keyPath,
123110
)
124-
if err := os.WriteFile(confPath, []byte(confData), os.ModePerm); err != nil {
111+
if err := os.WriteFile(confPath, []byte(confData), 0o600); err != nil {
125112
return "", fmt.Errorf("error writing conf file %s: %w", confPath, err)
126113
}
114+
if err := os.Chmod(confPath, 0o600); err != nil {
115+
return "", fmt.Errorf("error setting permissions on conf file %s: %w", confPath, err)
116+
}
127117

128118
log.V(1).Info("Creating ceph key", "keyPath", keyPath)
129119
keyData := fmt.Sprintf("[client.%s]\nkey = %s\n", volume.userID, volume.userKey)
130-
if err := os.WriteFile(keyPath, []byte(keyData), os.ModePerm); err != nil {
120+
if err := os.WriteFile(keyPath, []byte(keyData), 0o600); err != nil {
131121
return "", fmt.Errorf("error writing key file %s: %w", keyPath, err)
132122
}
123+
if err := os.Chmod(keyPath, 0o600); err != nil {
124+
return "", fmt.Errorf("error setting permissions on key file %s: %w", keyPath, err)
125+
}
133126

134127
return confPath, nil
135128
}

‎internal/plugins/volume/localdisk/localdisk.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ func (p *plugin) Apply(ctx context.Context, spec *api.VolumeSpec, machineID stri
122122
}, nil
123123
}
124124

125-
func (p *plugin) Delete(_ context.Context, computeVolumeName string, machineID string) error {
126-
return os.RemoveAll(p.host.MachineVolumeDir(machineID, utilstrings.EscapeQualifiedName(pluginName), computeVolumeName))
125+
func (p *plugin) Delete(_ context.Context, spec *api.VolumeSpec, machineID string) error {
126+
return os.RemoveAll(p.host.MachineVolumeDir(machineID, utilstrings.EscapeQualifiedName(pluginName), spec.Name))
127127
}
128128

129129
func generateWWN(machineID, diskName string) string {

‎internal/plugins/volume/plugins.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Plugin interface {
2525
CanSupport(spec *api.VolumeSpec) bool
2626

2727
Apply(ctx context.Context, spec *api.VolumeSpec, machineID string) (*api.VolumeStatus, error)
28-
Delete(ctx context.Context, computeVolumeName string, machineID string) error
28+
Delete(ctx context.Context, spec *api.VolumeSpec, machineID string) error
2929
}
3030

3131
type PluginManager struct {

0 commit comments

Comments
 (0)