-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Suleyman Akbas <[email protected]>
- Loading branch information
1 parent
c54c93e
commit 2529f3b
Showing
4 changed files
with
504 additions
and
396 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
/* | ||
Copyright © 2023 Red Hat, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package vgmanager | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" | ||
"github.com/openshift/lvm-operator/pkg/internal" | ||
) | ||
|
||
func (r *VGReconciler) addDevicesToVG(vgName string, devices []internal.BlockDevice) error { | ||
if len(devices) < 1 { | ||
return fmt.Errorf("can't create vg %q with 0 devices", vgName) | ||
} | ||
|
||
// check if volume group is already present | ||
vgs, err := ListVolumeGroups(r.executor) | ||
if err != nil { | ||
return fmt.Errorf("failed to list volume groups. %v", err) | ||
} | ||
|
||
vgFound := false | ||
for _, vg := range vgs { | ||
if vg.Name == vgName { | ||
vgFound = true | ||
} | ||
} | ||
|
||
args := []string{vgName} | ||
for _, device := range devices { | ||
if device.DevicePath != "" { | ||
args = append(args, device.DevicePath) | ||
} else { | ||
args = append(args, device.KName) | ||
} | ||
} | ||
|
||
// TODO: Check if we can use functions from lvm.go here | ||
var cmd string | ||
if vgFound { | ||
r.Log.Info("extending an existing volume group", "VGName", vgName) | ||
cmd = "/usr/sbin/vgextend" | ||
} else { | ||
r.Log.Info("creating a new volume group", "VGName", vgName) | ||
cmd = "/usr/sbin/vgcreate" | ||
} | ||
|
||
_, err = r.executor.ExecuteCommandWithOutputAsHost(cmd, args...) | ||
if err != nil { | ||
return fmt.Errorf("failed to create or extend volume group %q. %v", vgName, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *VGReconciler) getMatchingDevicesForVG(volumeGroup *lvmv1alpha1.LVMVolumeGroup) ([]internal.BlockDevice, []internal.BlockDevice, error) { | ||
// The LVMVolumeGroup was created/modified | ||
r.Log.Info("getting block devices for volumegroup", "VGName", volumeGroup.Name) | ||
|
||
// list block devices | ||
blockDevices, err := internal.ListBlockDevices(r.executor) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to list block devices: %v", err) | ||
} | ||
|
||
// filter out block devices | ||
remainingValidDevices, delayedDevices := r.filterAvailableDevices(blockDevices) | ||
|
||
var matchingDevices []internal.BlockDevice | ||
matchingDevices, err = r.filterMatchingDevices(remainingValidDevices, volumeGroup) | ||
if err != nil { | ||
r.Log.Error(err, "could not filter matching devices", "VGName", volumeGroup.Name) | ||
return nil, nil, err | ||
} | ||
|
||
return matchingDevices, delayedDevices, nil | ||
} | ||
|
||
// filterAvailableDevices returns: | ||
// validDevices: the list of blockdevices considered available | ||
// delayedDevices: the list of blockdevices considered available, but first observed less than 'minDeviceAge' time ago | ||
func (r *VGReconciler) filterAvailableDevices(blockDevices []internal.BlockDevice) ([]internal.BlockDevice, []internal.BlockDevice) { | ||
var availableDevices, delayedDevices []internal.BlockDevice | ||
// using a label so `continue DeviceLoop` can be used to skip devices | ||
DeviceLoop: | ||
for _, blockDevice := range blockDevices { | ||
|
||
// store device in deviceAgeMap | ||
r.deviceAgeMap.storeDeviceAge(blockDevice.KName) | ||
|
||
// check for partitions recursively | ||
if blockDevice.HasChildren() { | ||
childAvailableDevices, childDelayedDevices := r.filterAvailableDevices(blockDevice.Children) | ||
availableDevices = append(availableDevices, childAvailableDevices...) | ||
delayedDevices = append(delayedDevices, childDelayedDevices...) | ||
} | ||
|
||
devLogger := r.Log.WithValues("Device.Name", blockDevice.Name) | ||
for name, filter := range FilterMap { | ||
filterLogger := devLogger.WithValues("filter.Name", name) | ||
valid, err := filter(blockDevice, r.executor) | ||
if err != nil { | ||
filterLogger.Error(err, "filter error") | ||
valid = false | ||
continue DeviceLoop | ||
} else if !valid { | ||
filterLogger.Info("does not match filter") | ||
continue DeviceLoop | ||
} | ||
} | ||
// check if the device is older than deviceMinAge | ||
isOldEnough := r.deviceAgeMap.isOlderThan(blockDevice.KName) | ||
if isOldEnough { | ||
availableDevices = append(availableDevices, blockDevice) | ||
} else { | ||
delayedDevices = append(delayedDevices, blockDevice) | ||
} | ||
} | ||
return availableDevices, delayedDevices | ||
} | ||
|
||
// filterMatchingDevices returns matched blockdevices | ||
func (r *VGReconciler) filterMatchingDevices(blockDevices []internal.BlockDevice, volumeGroup *lvmv1alpha1.LVMVolumeGroup) ([]internal.BlockDevice, error) { | ||
var filteredBlockDevices []internal.BlockDevice | ||
if volumeGroup.Spec.DeviceSelector != nil && len(volumeGroup.Spec.DeviceSelector.Paths) > 0 { | ||
for _, path := range volumeGroup.Spec.DeviceSelector.Paths { | ||
diskName, err := filepath.EvalSymlinks(path) | ||
if err != nil { | ||
err = fmt.Errorf("unable to find symlink for disk path %s: %v", path, err) | ||
return []internal.BlockDevice{}, err | ||
} | ||
|
||
blockDevice, ok := hasExactDisk(blockDevices, diskName) | ||
if !ok { | ||
err := fmt.Errorf("can not find device name %s in the available block devices", path) | ||
return []internal.BlockDevice{}, err | ||
} | ||
|
||
blockDevice.DevicePath = path | ||
filteredBlockDevices = append(filteredBlockDevices, blockDevice) | ||
} | ||
|
||
return filteredBlockDevices, nil | ||
} | ||
|
||
// return all available block devices if none is specified in the CR | ||
return blockDevices, nil | ||
} | ||
|
||
// filterOutAttachedDevices filters out already attached devices to the volume group | ||
func (r *VGReconciler) filterOutAttachedDevices(blockDevices []internal.BlockDevice, volumeGroup *lvmv1alpha1.LVMVolumeGroup) ([]internal.BlockDevice, error) { | ||
var filteredBlockDevices []internal.BlockDevice | ||
|
||
vgs, err := ListVolumeGroups(r.executor) | ||
if err != nil { | ||
return []internal.BlockDevice{}, fmt.Errorf("failed to list volume groups. %v", err) | ||
} | ||
|
||
for _, device := range blockDevices { | ||
if !isDeviceAlreadyPartOfVG(vgs, device.Name, volumeGroup) { | ||
filteredBlockDevices = append(filteredBlockDevices, device) | ||
} | ||
} | ||
|
||
return filteredBlockDevices, nil | ||
} | ||
|
||
func isDeviceAlreadyPartOfVG(vgs []VolumeGroup, diskName string, volumeGroup *lvmv1alpha1.LVMVolumeGroup) bool { | ||
for _, vg := range vgs { | ||
if vg.Name == volumeGroup.Name { | ||
for _, pv := range vg.PVs { | ||
if pv == diskName { | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func hasExactDisk(blockDevices []internal.BlockDevice, deviceName string) (internal.BlockDevice, bool) { | ||
for _, blockDevice := range blockDevices { | ||
if blockDevice.KName == deviceName { | ||
return blockDevice, true | ||
} | ||
} | ||
return internal.BlockDevice{}, false | ||
} |
Oops, something went wrong.