From db462129c58e32ddc85cf3b8c890320eb860ef61 Mon Sep 17 00:00:00 2001 From: Viktor Kram <92625690+ViktorKram@users.noreply.github.com> Date: Tue, 23 Apr 2024 18:55:31 +0300 Subject: [PATCH] [controller] Change size type from string to quantity (#46) Signed-off-by: Viktor Kramarenko --- images/agent/api/v1alpha1/block_device.go | 35 +- images/agent/api/v1alpha1/lvm_volume_group.go | 8 +- images/agent/go.mod | 2 +- images/agent/internal/type.go | 2 +- images/agent/pkg/controller/block_device.go | 8 +- .../agent/pkg/controller/block_device_test.go | 2 +- .../controller/controller_reconcile_test.go | 2 +- .../controller/lvm_logical_volume_watcher.go | 34 +- .../lvm_logical_volume_watcher_test.go | 10 +- .../controller/lvm_volume_group_discover.go | 46 +-- .../lvm_volume_group_discover_test.go | 74 ++-- .../pkg/controller/lvm_volume_group_test.go | 26 +- .../controller/lvm_volume_group_watcher.go | 29 +- .../lvm_volume_group_watcher_func.go | 19 +- .../lvm_volume_group_watcher_test.go | 327 +----------------- 15 files changed, 128 insertions(+), 496 deletions(-) diff --git a/images/agent/api/v1alpha1/block_device.go b/images/agent/api/v1alpha1/block_device.go index a30d6a4c..5d332d8e 100644 --- a/images/agent/api/v1alpha1/block_device.go +++ b/images/agent/api/v1alpha1/block_device.go @@ -18,6 +18,7 @@ package v1alpha1 import ( "k8s.io/api/policy/v1beta1" + "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -37,21 +38,21 @@ type BlockDeviceList struct { } type BlockDeviceStatus struct { - Type string `json:"type"` - FsType v1beta1.FSType `json:"fsType"` - NodeName string `json:"nodeName"` - Consumable bool `json:"consumable"` - PVUuid string `json:"pvUUID"` - VGUuid string `json:"vgUUID"` - PartUUID string `json:"partUUID"` - LvmVolumeGroupName string `json:"lvmVolumeGroupName"` - ActualVGNameOnTheNode string `json:"actualVGNameOnTheNode"` - Wwn string `json:"wwn"` - Serial string `json:"serial"` - Path string `json:"path"` - Size string `json:"size"` - Model string `json:"model"` - Rota bool `json:"rota"` - HotPlug bool `json:"hotPlug"` - MachineID string `json:"machineId"` + Type string `json:"type"` + FsType v1beta1.FSType `json:"fsType"` + NodeName string `json:"nodeName"` + Consumable bool `json:"consumable"` + PVUuid string `json:"pvUUID"` + VGUuid string `json:"vgUUID"` + PartUUID string `json:"partUUID"` + LvmVolumeGroupName string `json:"lvmVolumeGroupName"` + ActualVGNameOnTheNode string `json:"actualVGNameOnTheNode"` + Wwn string `json:"wwn"` + Serial string `json:"serial"` + Path string `json:"path"` + Size resource.Quantity `json:"size"` + Model string `json:"model"` + Rota bool `json:"rota"` + HotPlug bool `json:"hotPlug"` + MachineID string `json:"machineId"` } diff --git a/images/agent/api/v1alpha1/lvm_volume_group.go b/images/agent/api/v1alpha1/lvm_volume_group.go index 62699e68..6061fb50 100644 --- a/images/agent/api/v1alpha1/lvm_volume_group.go +++ b/images/agent/api/v1alpha1/lvm_volume_group.go @@ -51,7 +51,7 @@ type LvmVolumeGroupSpec struct { type LvmVolumeGroupDevice struct { BlockDevice string `json:"blockDevice"` DevSize resource.Quantity `json:"devSize"` - PVSize string `json:"pvSize"` + PVSize resource.Quantity `json:"pvSize"` PVUuid string `json:"pvUUID"` Path string `json:"path"` } @@ -64,15 +64,15 @@ type LvmVolumeGroupNode struct { type StatusThinPool struct { Name string `json:"name"` ActualSize resource.Quantity `json:"actualSize"` - UsedSize string `json:"usedSize"` + UsedSize resource.Quantity `json:"usedSize"` } type LvmVolumeGroupStatus struct { - AllocatedSize string `json:"allocatedSize"` + AllocatedSize resource.Quantity `json:"allocatedSize"` Health string `json:"health"` Message string `json:"message"` Nodes []LvmVolumeGroupNode `json:"nodes"` ThinPools []StatusThinPool `json:"thinPools"` - VGSize string `json:"vgSize"` + VGSize resource.Quantity `json:"vgSize"` VGUuid string `json:"vgUUID"` } diff --git a/images/agent/go.mod b/images/agent/go.mod index 497e58ea..f37d2e95 100644 --- a/images/agent/go.mod +++ b/images/agent/go.mod @@ -4,6 +4,7 @@ go 1.20 require ( github.com/go-logr/logr v1.3.0 + github.com/google/go-cmp v0.6.0 github.com/onsi/ginkgo/v2 v2.11.0 github.com/onsi/gomega v1.27.10 github.com/prometheus/client_golang v1.17.0 @@ -33,7 +34,6 @@ require ( github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/gnostic-models v0.6.8 // indirect - github.com/google/go-cmp v0.6.0 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect github.com/google/uuid v1.4.0 // indirect diff --git a/images/agent/internal/type.go b/images/agent/internal/type.go index 8a2e52f3..3862ab3a 100644 --- a/images/agent/internal/type.go +++ b/images/agent/internal/type.go @@ -60,7 +60,7 @@ type LVMVolumeGroupCandidate struct { type LVMVGStatusThinPool struct { Name string ActualSize resource.Quantity - UsedSize string + UsedSize resource.Quantity } type LVMVGDevice struct { diff --git a/images/agent/pkg/controller/block_device.go b/images/agent/pkg/controller/block_device.go index 59ba6ac7..420fc803 100644 --- a/images/agent/pkg/controller/block_device.go +++ b/images/agent/pkg/controller/block_device.go @@ -133,7 +133,6 @@ func RunBlockDeviceController( } func hasBlockDeviceDiff(res v1alpha1.BlockDeviceStatus, candidate internal.BlockDeviceCandidate) bool { - candSizeTmp := resource.NewQuantity(candidate.Size.Value(), resource.BinarySI) return candidate.NodeName != res.NodeName || candidate.Consumable != res.Consumable || candidate.PVUuid != res.PVUuid || @@ -144,7 +143,7 @@ func hasBlockDeviceDiff(res v1alpha1.BlockDeviceStatus, candidate internal.Block candidate.Wwn != res.Wwn || candidate.Serial != res.Serial || candidate.Path != res.Path || - candSizeTmp.String() != res.Size || + candidate.Size.Value() != res.Size.Value() || candidate.Rota != res.Rota || candidate.Model != res.Model || candidate.HotPlug != res.HotPlug || @@ -491,7 +490,6 @@ func readSerialBlockDevice(deviceName string, isMdRaid bool) (string, error) { } func UpdateAPIBlockDevice(ctx context.Context, kc kclient.Client, metrics monitoring.Metrics, res v1alpha1.BlockDevice, candidate internal.BlockDeviceCandidate) error { - candidateSizeTmp := resource.NewQuantity(candidate.Size.Value(), resource.BinarySI) device := &v1alpha1.BlockDevice{ TypeMeta: metav1.TypeMeta{ Kind: v1alpha1.BlockDeviceKind, @@ -515,7 +513,7 @@ func UpdateAPIBlockDevice(ctx context.Context, kc kclient.Client, metrics monito Wwn: candidate.Wwn, Serial: candidate.Serial, Path: candidate.Path, - Size: candidateSizeTmp.String(), + Size: *resource.NewQuantity(candidate.Size.Value(), resource.BinarySI), Model: candidate.Model, Rota: candidate.Rota, HotPlug: candidate.HotPlug, @@ -559,7 +557,7 @@ func CreateAPIBlockDevice(ctx context.Context, kc kclient.Client, metrics monito Wwn: candidate.Wwn, Serial: candidate.Serial, Path: candidate.Path, - Size: candidateSizeTmp.String(), + Size: *resource.NewQuantity(candidateSizeTmp.Value(), resource.BinarySI), Model: candidate.Model, Rota: candidate.Rota, MachineID: candidate.MachineId, diff --git a/images/agent/pkg/controller/block_device_test.go b/images/agent/pkg/controller/block_device_test.go index 54532f8d..c58773ab 100644 --- a/images/agent/pkg/controller/block_device_test.go +++ b/images/agent/pkg/controller/block_device_test.go @@ -189,7 +189,7 @@ func TestBlockDeviceCtrl(t *testing.T) { Wwn: "testWWN", Serial: "testSERIAL", Path: "testPATH", - Size: "0", + Size: resource.MustParse("0"), Model: "testMODEL", Rota: false, HotPlug: false, diff --git a/images/agent/pkg/controller/controller_reconcile_test.go b/images/agent/pkg/controller/controller_reconcile_test.go index 3ba06a2e..69c0034b 100644 --- a/images/agent/pkg/controller/controller_reconcile_test.go +++ b/images/agent/pkg/controller/controller_reconcile_test.go @@ -71,7 +71,7 @@ var _ = Describe("Storage Controller", func() { Expect(blockDevice.Status.Wwn).To(Equal(candidate.Wwn)) Expect(blockDevice.Status.Serial).To(Equal(candidate.Serial)) Expect(blockDevice.Status.Path).To(Equal(candidate.Path)) - Expect(blockDevice.Status.Size).To(Equal(candidate.Size.String())) + Expect(blockDevice.Status.Size.Value()).To(Equal(candidate.Size.Value())) Expect(blockDevice.Status.Rota).To(Equal(candidate.Rota)) Expect(blockDevice.Status.Model).To(Equal(candidate.Model)) Expect(blockDevice.Status.Type).To(Equal(candidate.Type)) diff --git a/images/agent/pkg/controller/lvm_logical_volume_watcher.go b/images/agent/pkg/controller/lvm_logical_volume_watcher.go index 0f027835..c85ebe13 100644 --- a/images/agent/pkg/controller/lvm_logical_volume_watcher.go +++ b/images/agent/pkg/controller/lvm_logical_volume_watcher.go @@ -345,15 +345,7 @@ func reconcileLLVUpdateFunc( switch llv.Spec.Type { case Thick: - freeSpace, err := getFreeVGSpace(lvg) - if err != nil { - log.Error(err, fmt.Sprintf("[reconcileLLVUpdateFunc] unable to count free space in VG, name: %s", vgName)) - err = updateLVMLogicalVolumePhase(ctx, cl, log, metrics, llv, failedStatusPhase, fmt.Sprintf("Unable to count free VG space, VG name %s, err: %s", vgName, err.Error())) - if err != nil { - log.Error(err, fmt.Sprintf("[reconcileLLVUpdateFunc] unable to update the LVMLogicalVolume %s", llv.Name)) - } - return - } + freeSpace := getFreeVGSpace(lvg) log.Trace(fmt.Sprintf("[reconcileLLVUpdateFunc] the LVMLogicalVolume %s, LV: %s, VG: %s Thick extending size: %d, free size: %d", llv.Name, lvName, vgName, extendingSize.Value(), freeSpace.Value())) if freeSpace.Value() < extendingSize.Value()+delta.Value() { @@ -509,15 +501,7 @@ func reconcileLLVCreateFunc( switch llv.Spec.Type { case Thick: - freeSpace, err := getFreeVGSpace(lvg) - if err != nil { - log.Error(err, fmt.Sprintf("[reconcileLLVCreateFunc] unable to count free space in VG, name: %s", vgName)) - err = updateLVMLogicalVolumePhase(ctx, cl, log, metrics, llv, failedStatusPhase, fmt.Sprintf("Unable to get free VG space, err: %s", err.Error())) - if err != nil { - log.Error(err, fmt.Sprintf("[reconcileLLVCreateFunc] unable to updateLVMLogicalVolumePhase for LVMLogicalVolume %s", llv.Name)) - } - return - } + freeSpace := getFreeVGSpace(lvg) log.Trace(fmt.Sprintf("[reconcileLLVCreateFunc] the LVMLogicalVolume %s, LV: %s, VG: %s type: %s requested size: %d, free size: %d", llv.Name, lvName, vgName, llv.Spec.Type, llv.Spec.Size.Value(), freeSpace.Value())) if freeSpace.Value() < llv.Spec.Size.Value() { @@ -706,18 +690,8 @@ func subtractQuantity(currentQuantity, quantityToSubtract resource.Quantity) res return resultingQuantity } -func getFreeVGSpace(lvg *v1alpha1.LvmVolumeGroup) (resource.Quantity, error) { - total, err := resource.ParseQuantity(lvg.Status.VGSize) - if err != nil { - return resource.Quantity{}, err - } - - allocated, err := resource.ParseQuantity(lvg.Status.AllocatedSize) - if err != nil { - return resource.Quantity{}, err - } - - return subtractQuantity(total, allocated), nil +func getFreeVGSpace(lvg *v1alpha1.LvmVolumeGroup) resource.Quantity { + return subtractQuantity(lvg.Status.VGSize, lvg.Status.AllocatedSize) } func belongsToNode(lvg *v1alpha1.LvmVolumeGroup, nodeName string) bool { diff --git a/images/agent/pkg/controller/lvm_logical_volume_watcher_test.go b/images/agent/pkg/controller/lvm_logical_volume_watcher_test.go index d0940075..24fcf78c 100644 --- a/images/agent/pkg/controller/lvm_logical_volume_watcher_test.go +++ b/images/agent/pkg/controller/lvm_logical_volume_watcher_test.go @@ -443,15 +443,13 @@ func TestLVMLogicaVolumeWatcher(t *testing.T) { t.Run("getFreeVGSpace", func(t *testing.T) { lvg := &v1alpha1.LvmVolumeGroup{ Status: v1alpha1.LvmVolumeGroupStatus{ - VGSize: "2G", - AllocatedSize: "1G", + VGSize: resource.MustParse("2G"), + AllocatedSize: resource.MustParse("1G"), }, } - free, err := getFreeVGSpace(lvg) - if assert.NoError(t, err) { - assert.Equal(t, int64(1000000000), free.Value()) - } + free := getFreeVGSpace(lvg) + assert.Equal(t, int64(1000000000), free.Value()) }) t.Run("updateLVMLogicalVolume", func(t *testing.T) { diff --git a/images/agent/pkg/controller/lvm_volume_group_discover.go b/images/agent/pkg/controller/lvm_volume_group_discover.go index 13f39529..a50edcb2 100644 --- a/images/agent/pkg/controller/lvm_volume_group_discover.go +++ b/images/agent/pkg/controller/lvm_volume_group_discover.go @@ -134,7 +134,6 @@ func RunLVMVolumeGroupDiscoverController( } log.Info(fmt.Sprintf(`[RunLVMVolumeGroupDiscoverController] updated LvmVolumeGroup, name: "%s"`, lvmVolumeGroup.Name)) - //TODO: release lock } else { lvm, err := CreateLVMVolumeGroup(ctx, log, metrics, cl, candidate) @@ -230,24 +229,21 @@ func turnLVMVGHealthToNonOperational(ctx context.Context, cl kclient.Client, lvg } func hasLVMVolumeGroupDiff(log logger.Logger, res v1alpha1.LvmVolumeGroup, candidate internal.LVMVolumeGroupCandidate) bool { - candidateASTmp := resource.NewQuantity(candidate.AllocatedSize.Value(), resource.BinarySI) - candidateVGSizeTmp := resource.NewQuantity(candidate.VGSize.Value(), resource.BinarySI) - - log.Trace(fmt.Sprintf(`AllocatedSize, candidate: %s, res: %s`, candidateASTmp.String(), res.Status.AllocatedSize)) + log.Trace(fmt.Sprintf(`AllocatedSize, candidate: %s, res: %s`, candidate.AllocatedSize.String(), res.Status.AllocatedSize.String())) log.Trace(fmt.Sprintf(`Health, candidate: %s, res: %s`, candidate.Health, res.Status.Health)) log.Trace(fmt.Sprintf(`Message, candidate: %s, res: %s`, candidate.Message, res.Status.Message)) log.Trace(fmt.Sprintf(`ThinPools, candidate: %v, res: %v`, convertStatusThinPools(candidate.StatusThinPools), res.Status.ThinPools)) - log.Trace(fmt.Sprintf(`VGSize, candidate: %s, res: %s`, candidateVGSizeTmp.String(), res.Status.VGSize)) + log.Trace(fmt.Sprintf(`VGSize, candidate: %s, res: %s`, candidate.VGSize.String(), res.Status.VGSize.String())) log.Trace(fmt.Sprintf(`VGUuid, candidate: %s, res: %s`, candidate.VGUuid, res.Status.VGUuid)) log.Trace(fmt.Sprintf(`Nodes, candidate: %v, res: %v`, convertLVMVGNodes(candidate.Nodes), res.Status.Nodes)) //TODO: Uncomment this //return strings.Join(candidate.Finalizers, "") == strings.Join(res.Finalizers, "") || - return candidateASTmp.String() != res.Status.AllocatedSize || + return candidate.AllocatedSize.Value() != res.Status.AllocatedSize.Value() || candidate.Health != res.Status.Health || candidate.Message != res.Status.Message || !reflect.DeepEqual(convertStatusThinPools(candidate.StatusThinPools), res.Status.ThinPools) || - candidateVGSizeTmp.String() != res.Status.VGSize || + candidate.VGSize.Value() != res.Status.VGSize.Value() || candidate.VGUuid != res.Status.VGUuid || !reflect.DeepEqual(convertLVMVGNodes(candidate.Nodes), res.Status.Nodes) } @@ -421,11 +417,11 @@ func GetLVMVolumeGroupCandidates(log logger.Logger, metrics monitoring.Metrics, BlockDevicesNames: getBlockDevicesNames(sortedBDs, vg), SpecThinPools: getSpecThinPools(sortedThinPools, vg), Type: getVgType(vg), - AllocatedSize: allocateSize, + AllocatedSize: *resource.NewQuantity(allocateSize.Value(), resource.BinarySI), Health: health, Message: message, StatusThinPools: getStatusThinPools(log, sortedThinPools, vg), - VGSize: vg.VGSize, + VGSize: *resource.NewQuantity(vg.VGSize.Value(), resource.BinarySI), VGUuid: vg.VGUuid, Nodes: configureCandidateNodeDevices(sortedPVs, sortedBDs, vg, currentNode), } @@ -608,12 +604,12 @@ func configureCandidateNodeDevices(pvs map[string][]internal.PVData, bds map[str for _, pv := range filteredPV { device := internal.LVMVGDevice{ Path: pv.PVName, - PVSize: pv.PVSize, + PVSize: *resource.NewQuantity(pv.PVSize.Value(), resource.BinarySI), PVUuid: pv.PVUuid, } if bd, exist := bdPathStatus[pv.PVName]; exist { - device.DevSize = resource.MustParse(bd.Status.Size) + device.DevSize = *resource.NewQuantity(bd.Status.Size.Value(), resource.BinarySI) device.BlockDevice = bd.Name } @@ -665,8 +661,8 @@ func getStatusThinPools(log logger.Logger, thinPools map[string][]internal.LVDat } tps = append(tps, internal.LVMVGStatusThinPool{ Name: lv.LVName, - ActualSize: lv.LVSize, - UsedSize: usedSize.String(), + ActualSize: *resource.NewQuantity(lv.LVSize.Value(), resource.BinarySI), + UsedSize: *resource.NewQuantity(usedSize.Value(), resource.BinarySI), }) } return tps @@ -714,8 +710,6 @@ func CreateLVMVolumeGroup( kc kclient.Client, candidate internal.LVMVolumeGroupCandidate, ) (*v1alpha1.LvmVolumeGroup, error) { - candidateVGSizeTmp := resource.NewQuantity(candidate.VGSize.Value(), resource.BinarySI) - candidateAllocatedSizeTmp := resource.NewQuantity(candidate.AllocatedSize.Value(), resource.BinarySI) lvmVolumeGroup := &v1alpha1.LvmVolumeGroup{ TypeMeta: metav1.TypeMeta{ Kind: v1alpha1.LVMVolumeGroupKind, @@ -734,12 +728,12 @@ func CreateLVMVolumeGroup( Type: candidate.Type, }, Status: v1alpha1.LvmVolumeGroupStatus{ - AllocatedSize: candidateAllocatedSizeTmp.String(), + AllocatedSize: candidate.AllocatedSize, Health: candidate.Health, Message: candidate.Message, Nodes: convertLVMVGNodes(candidate.Nodes), ThinPools: convertStatusThinPools(candidate.StatusThinPools), - VGSize: candidateVGSizeTmp.String(), + VGSize: candidate.VGSize, VGUuid: candidate.VGUuid, }, } @@ -760,7 +754,7 @@ func CreateLVMVolumeGroup( metrics.ApiMethodsExecutionCount(LVMVolumeGroupDiscoverCtrlName, "create").Inc() if err != nil { metrics.ApiMethodsErrors(LVMVolumeGroupDiscoverCtrlName, "create").Inc() - return nil, fmt.Errorf("unable to CreateLVMVolumeGroup, err: %w", err) + return nil, fmt.Errorf("unable to сreate LVMVolumeGroup, err: %w", err) } return lvmVolumeGroup, nil @@ -795,10 +789,6 @@ func UpdateLVMVolumeGroupByCandidate( } // Update status. - - candidateVGSizeTmp := resource.NewQuantity(candidate.VGSize.Value(), resource.BinarySI) - candidateAllocatedSizeTmp := resource.NewQuantity(candidate.AllocatedSize.Value(), resource.BinarySI) - lvmvg := &v1alpha1.LvmVolumeGroup{ TypeMeta: metav1.TypeMeta{ Kind: v1alpha1.LVMVolumeGroupKind, @@ -809,6 +799,7 @@ func UpdateLVMVolumeGroupByCandidate( OwnerReferences: res.OwnerReferences, ResourceVersion: res.ResourceVersion, Annotations: res.Annotations, + Labels: res.Labels, }, Spec: v1alpha1.LvmVolumeGroupSpec{ ActualVGNameOnTheNode: res.Spec.ActualVGNameOnTheNode, @@ -817,12 +808,12 @@ func UpdateLVMVolumeGroupByCandidate( Type: res.Spec.Type, }, Status: v1alpha1.LvmVolumeGroupStatus{ - AllocatedSize: candidateAllocatedSizeTmp.String(), + AllocatedSize: candidate.AllocatedSize, Health: candidate.Health, Message: candidate.Message, Nodes: convertLVMVGNodes(candidate.Nodes), ThinPools: convertStatusThinPools(candidate.StatusThinPools), - VGSize: candidateVGSizeTmp.String(), + VGSize: candidate.VGSize, VGUuid: candidate.VGUuid, }, } @@ -858,13 +849,10 @@ func convertLVMVGDevices(devices []internal.LVMVGDevice) []v1alpha1.LvmVolumeGro convertedDevices := make([]v1alpha1.LvmVolumeGroupDevice, 0, len(devices)) for _, dev := range devices { - - devPVSizeTmp := resource.NewQuantity(dev.PVSize.Value(), resource.BinarySI) - convertedDevices = append(convertedDevices, v1alpha1.LvmVolumeGroupDevice{ BlockDevice: dev.BlockDevice, DevSize: dev.DevSize, - PVSize: devPVSizeTmp.String(), + PVSize: dev.PVSize, PVUuid: dev.PVUuid, Path: dev.Path, }) diff --git a/images/agent/pkg/controller/lvm_volume_group_discover_test.go b/images/agent/pkg/controller/lvm_volume_group_discover_test.go index 58ce78c8..4972dd6b 100644 --- a/images/agent/pkg/controller/lvm_volume_group_discover_test.go +++ b/images/agent/pkg/controller/lvm_volume_group_discover_test.go @@ -281,7 +281,7 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { ObjectMeta: metav1.ObjectMeta{Name: "block_device1"}, Status: v1alpha1.BlockDeviceStatus{ Path: "test_pv1", - Size: "10G", + Size: resource.MustParse("10G"), VGUuid: vgUuid, ActualVGNameOnTheNode: vgName, }, @@ -290,7 +290,7 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { ObjectMeta: metav1.ObjectMeta{Name: "block_device2"}, Status: v1alpha1.BlockDeviceStatus{ Path: "test_pv2", - Size: "1G", + Size: resource.MustParse("1G"), VGUuid: vgUuid, ActualVGNameOnTheNode: vgName, }, @@ -301,15 +301,15 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { nodeName: { { Path: "test_pv1", - PVSize: size10G, - DevSize: size10G, + PVSize: *resource.NewQuantity(size10G.Value(), resource.BinarySI), + DevSize: *resource.NewQuantity(size10G.Value(), resource.BinarySI), PVUuid: "pv_uuid1", BlockDevice: "block_device1", }, { Path: "test_pv2", - PVSize: size1G, - DevSize: size1G, + PVSize: *resource.NewQuantity(size1G.Value(), resource.BinarySI), + DevSize: *resource.NewQuantity(size1G.Value(), resource.BinarySI), PVUuid: "pv_uuid2", BlockDevice: "block_device2", }, @@ -414,10 +414,8 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { LVMVGName = "test_lvm" ActualVGNameOnTheNode = "test-vg" Type = "local" - AllocatedSize = "9765625Ki" Health = internal.LVMVGHealthOperational Message = "No problems detected" - VGSize = "9765625Ki" VGUuid = "test_uuid" ) @@ -438,7 +436,7 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { { Name: "first_status_pool", ActualSize: size10G, - UsedSize: "4G", + UsedSize: resource.MustParse("4G"), }, } nodes = map[string][]internal.LVMVGDevice{ @@ -486,12 +484,12 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { Type: Type, }, Status: v1alpha1.LvmVolumeGroupStatus{ - AllocatedSize: AllocatedSize, + AllocatedSize: size10G, Health: Health, Message: Message, Nodes: convertLVMVGNodes(nodes), ThinPools: convertStatusThinPools(statusThinPools), - VGSize: VGSize, + VGSize: size10G, VGUuid: VGUuid, }, } @@ -507,10 +505,8 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { LVMVGName = "test_lvm" ActualVGNameOnTheNode = "test-vg" Type = "local" - AllocatedSize = "9765625Ki" Health = internal.LVMVGHealthOperational Message = "No problems detected" - VGSize = "9765625Ki" VGUuid = "test_uuid" ) @@ -531,7 +527,7 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { { Name: "first_status_pool", ActualSize: size10G, - UsedSize: "4G", + UsedSize: resource.MustParse("4G"), }, } nodes = map[string][]internal.LVMVGDevice{ @@ -580,12 +576,12 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { Type: Type, }, Status: v1alpha1.LvmVolumeGroupStatus{ - AllocatedSize: AllocatedSize, + AllocatedSize: size10G, Health: Health, Message: Message, Nodes: convertLVMVGNodes(nodes), ThinPools: convertStatusThinPools(statusThinPools), - VGSize: VGSize, + VGSize: size10G, VGUuid: VGUuid, }, }, @@ -627,7 +623,7 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { { Name: "first_status_pool", ActualSize: size10G, - UsedSize: "4G", + UsedSize: resource.MustParse("4G"), }, } nodes = map[string][]internal.LVMVGDevice{ @@ -678,10 +674,8 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { LVMVGName = "test_lvm" ActualVGNameOnTheNode = "test-vg" Type = "local" - AllocatedSize = "9765625Ki" Health = internal.LVMVGHealthOperational Message = "No problems detected" - VGSize = "9765625Ki" VGUuid = "test_uuid" ) @@ -702,7 +696,7 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { { Name: "first_status_pool", ActualSize: size10G, - UsedSize: "4G", + UsedSize: resource.MustParse("4G"), }, } oldNodes = map[string][]internal.LVMVGDevice{ @@ -783,12 +777,12 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { Type: Type, }, Status: v1alpha1.LvmVolumeGroupStatus{ - AllocatedSize: AllocatedSize, + AllocatedSize: size10G, Health: Health, Message: Message, Nodes: convertLVMVGNodes(newNodes), ThinPools: convertStatusThinPools(StatusThinPools), - VGSize: VGSize, + VGSize: size10G, VGUuid: VGUuid, }, } @@ -868,15 +862,15 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { t.Run("filterResourcesByNode_returns_current_node_resources", func(t *testing.T) { var ( - ctx = context.Background() - cl = NewFakeClient() - testLogger, _ = logger.NewLogger(logger.InfoLevel) - currentNode = "test_node" - firstBDName = "first_device" - secondBDName = "second_device" - firstLVName = "first_lv" - secondLVName = "second_lv" - blockDevices = map[string]v1alpha1.BlockDevice{ + ctx = context.Background() + cl = NewFakeClient() + testLogger = logger.Logger{} + currentNode = "test_node" + firstBDName = "first_device" + secondBDName = "second_device" + firstLVName = "first_lv" + secondLVName = "second_lv" + blockDevices = map[string]v1alpha1.BlockDevice{ firstBDName: { ObjectMeta: metav1.ObjectMeta{ Name: firstBDName, @@ -923,7 +917,7 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { }, } - actual := filterResourcesByNode(ctx, cl, *testLogger, lvs, blockDevices, currentNode) + actual := filterResourcesByNode(ctx, cl, testLogger, lvs, blockDevices, currentNode) assert.Equal(t, expected, actual) }) @@ -1007,12 +1001,12 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { { Name: "first", ActualSize: size10G, - UsedSize: "2G", + UsedSize: resource.MustParse("2G"), }, { Name: "second", ActualSize: size10G, - UsedSize: "2G", + UsedSize: resource.MustParse("2G"), }, } nodes = map[string][]internal.LVMVGDevice{ @@ -1046,12 +1040,12 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { Type: specType, }, Status: v1alpha1.LvmVolumeGroupStatus{ - AllocatedSize: "9765625Ki", + AllocatedSize: resource.MustParse("9765625Ki"), Health: health, Message: message, Nodes: convertLVMVGNodes(nodes), ThinPools: convertStatusThinPools(statusThinPools), - VGSize: "9765625Ki", + VGSize: resource.MustParse("9765625Ki"), }, } @@ -1076,22 +1070,22 @@ func TestLVMVolumeGroupDiscover(t *testing.T) { "second": size1G, } specType = "type" - allocatedSize = "10G" + allocatedSize = resource.MustParse("10G") health = internal.LVMVGHealthOperational message = "all good" statusThinPools = []internal.LVMVGStatusThinPool{ { Name: "first", ActualSize: size10G, - UsedSize: "2G", + UsedSize: resource.MustParse("2G"), }, { Name: "second", ActualSize: size10G, - UsedSize: "2G", + UsedSize: resource.MustParse("2G"), }, } - vgSize = "10G" + vgSize = resource.MustParse("10G") nodes = map[string][]internal.LVMVGDevice{ "test_node": { { diff --git a/images/agent/pkg/controller/lvm_volume_group_test.go b/images/agent/pkg/controller/lvm_volume_group_test.go index 9331ccf1..2abe70e0 100644 --- a/images/agent/pkg/controller/lvm_volume_group_test.go +++ b/images/agent/pkg/controller/lvm_volume_group_test.go @@ -95,7 +95,7 @@ func TestLvmVolumeGroupAPIObjects(t *testing.T) { "actualSize": "1G" } ], - "vgSize": "test-vg-size", + "vgSize": "30G", "vgUUID": "test-vg-uuid" } }` @@ -127,8 +127,8 @@ func TestLvmVolumeGroupAPIObjects(t *testing.T) { Health: "operational", Message: "all-good", VGUuid: "test-vg-uuid", - VGSize: "test-vg-size", - AllocatedSize: "20G", + VGSize: resource.MustParse("30G"), + AllocatedSize: resource.MustParse("20G"), ThinPools: []v1alpha1.StatusThinPool{ { Name: "test-name", @@ -141,14 +141,14 @@ func TestLvmVolumeGroupAPIObjects(t *testing.T) { Devices: []v1alpha1.LvmVolumeGroupDevice{ { Path: "test/path1", - PVSize: "1G", + PVSize: resource.MustParse("1G"), DevSize: *convertSize("1G", t), PVUuid: "testPV1", BlockDevice: "test/BD", }, { Path: "test/path2", - PVSize: "2G", + PVSize: resource.MustParse("2G"), DevSize: *convertSize("1G", t), PVUuid: "testPV2", BlockDevice: "test/BD2", @@ -160,7 +160,7 @@ func TestLvmVolumeGroupAPIObjects(t *testing.T) { Devices: []v1alpha1.LvmVolumeGroupDevice{ { Path: "test/path3", - PVSize: "3G", + PVSize: resource.MustParse("3G"), DevSize: *convertSize("2G", t), PVUuid: "testPV3", BlockDevice: "test/DB3", @@ -249,7 +249,7 @@ func TestLvmVolumeGroupAPIObjects(t *testing.T) { "usedSize": "500M" } ], - "vgSize": "test-vg-size", + "vgSize": "30G", "vgUUID": "test-vg-uuid" } }` @@ -278,7 +278,7 @@ func TestLvmVolumeGroupAPIObjects(t *testing.T) { Type: "local", }, Status: v1alpha1.LvmVolumeGroupStatus{ - AllocatedSize: "20G", + AllocatedSize: resource.MustParse("20G"), Health: "operational", Message: "all-good", Nodes: []v1alpha1.LvmVolumeGroupNode{ @@ -287,14 +287,14 @@ func TestLvmVolumeGroupAPIObjects(t *testing.T) { { BlockDevice: "test/BD", DevSize: *convertSize("1G", t), - PVSize: "1G", + PVSize: resource.MustParse("1G"), PVUuid: "testPV1", Path: "test/path1", }, { BlockDevice: "test/BD2", DevSize: *convertSize("1G", t), - PVSize: "2G", + PVSize: resource.MustParse("2G"), PVUuid: "testPV2", Path: "test/path2", }, @@ -306,7 +306,7 @@ func TestLvmVolumeGroupAPIObjects(t *testing.T) { { BlockDevice: "test/DB3", DevSize: *convertSize("2G", t), - PVSize: "3G", + PVSize: resource.MustParse("3G"), PVUuid: "testPV3", Path: "test/path3", }, @@ -318,10 +318,10 @@ func TestLvmVolumeGroupAPIObjects(t *testing.T) { { Name: "test-name", ActualSize: *convertSize("1G", t), - UsedSize: "500M", + UsedSize: resource.MustParse("500M"), }, }, - VGSize: "test-vg-size", + VGSize: resource.MustParse("30G"), VGUuid: "test-vg-uuid", }, } diff --git a/images/agent/pkg/controller/lvm_volume_group_watcher.go b/images/agent/pkg/controller/lvm_volume_group_watcher.go index c50c2305..c74f48a1 100644 --- a/images/agent/pkg/controller/lvm_volume_group_watcher.go +++ b/images/agent/pkg/controller/lvm_volume_group_watcher.go @@ -148,19 +148,18 @@ func RunLVMVolumeGroupWatcherController( return } - log.Debug(fmt.Sprintf("[RunLVMVolumeGroupWatcherController] update spec check resize device.PVSize = %s", device.PVSize)) - dPVSizeTmp := resource.MustParse(device.PVSize) + log.Debug(fmt.Sprintf("[RunLVMVolumeGroupWatcherController] update spec check resize device.PVSize = %s", device.PVSize.String())) - if dPVSizeTmp.Value() == 0 { - log.Warning(fmt.Sprintf("[RunLVMVolumeGroupWatcherController] check dev PV size device.PVSize = %s", device.PVSize)) + if device.PVSize.Value() == 0 { + log.Warning(fmt.Sprintf("[RunLVMVolumeGroupWatcherController] check dev PV size device.PVSize = %s", device.PVSize.String())) return } delta, _ := utils.QuantityToBytes(internal.ResizeDelta) - log.Debug(fmt.Sprintf("[RunLVMVolumeGroupWatcherController] resize flag = %t", device.DevSize.Value()-dPVSizeTmp.Value() > delta)) + log.Debug(fmt.Sprintf("[RunLVMVolumeGroupWatcherController] resize flag = %t", device.DevSize.Value()-device.PVSize.Value() > delta)) - if device.DevSize.Value()-dPVSizeTmp.Value() > delta { + if device.DevSize.Value()-device.PVSize.Value() > delta { log.Info("[RunLVMVolumeGroupWatcherController] lvg status device and PV changed") request := reconcile.Request{NamespacedName: types.NamespacedName{Namespace: e.ObjectNew.GetNamespace(), Name: e.ObjectNew.GetName()}} shouldRequeue, err := ReconcileLVMVG(ctx, metrics, e.ObjectNew.GetName(), e.ObjectNew.GetNamespace(), cfg.NodeName, log, cl) @@ -332,24 +331,22 @@ func ReconcileLVMVG( return false, err } - log.Debug(fmt.Sprintf("[ReconcileLVMVG] check Resize d.PVSize = %s", d.PVSize)) + log.Debug(fmt.Sprintf("[ReconcileLVMVG] check Resize d.PVSize = %s", d.PVSize.String())) - dPVSizeTmp := resource.MustParse(d.PVSize) - - if dPVSizeTmp.Value() == 0 { - log.Warning(fmt.Sprintf("[ReconcileLVMVG] check dev PV size device.PVSize = %s", d.PVSize)) + if d.PVSize.Value() == 0 { + log.Warning(fmt.Sprintf("[ReconcileLVMVG] check dev PV size device.PVSize = %s", d.PVSize.String())) return false, nil } delta, _ := utils.QuantityToBytes(internal.ResizeDelta) log.Debug("---------- Reconcile ---------------") - log.Debug(fmt.Sprintf("[ReconcileLVMVG] PVSize = %s", d.PVSize)) - log.Debug(fmt.Sprintf("[ReconcileLVMVG] DevSize = %s %s", d.DevSize.String(), d.PVSize)) - log.Debug(fmt.Sprintf("[ReconcileLVMVG] Resize flag = %t", d.DevSize.Value()-dPVSizeTmp.Value() > delta)) + log.Debug(fmt.Sprintf("[ReconcileLVMVG] PVSize = %s", d.PVSize.String())) + log.Debug(fmt.Sprintf("[ReconcileLVMVG] DevSize = %s %s", d.DevSize.String(), d.PVSize.String())) + log.Debug(fmt.Sprintf("[ReconcileLVMVG] Resize flag = %t", d.DevSize.Value()-d.PVSize.Value() > delta)) log.Debug("---------- Reconcile ---------------") - if d.DevSize.Value() < dPVSizeTmp.Value() { + if d.DevSize.Value() < d.PVSize.Value() { return false, nil } @@ -357,7 +354,7 @@ func ReconcileLVMVG( log.Info(fmt.Sprintf("[ReconcileLVMVG] devSize %s ", d.DevSize.String())) log.Info(fmt.Sprintf("[ReconcileLVMVG] pvSize %s ", d.DevSize.String())) - if d.DevSize.Value()-dPVSizeTmp.Value() > delta { + if d.DevSize.Value()-d.PVSize.Value() > delta { log.Info(fmt.Sprintf("[ReconcileLVMVG] create event: %s", EventActionResizing)) err = CreateEventLVMVolumeGroup(ctx, cl, metrics, EventReasonResizing, EventActionResizing, nodeName, lvg) if err != nil { diff --git a/images/agent/pkg/controller/lvm_volume_group_watcher_func.go b/images/agent/pkg/controller/lvm_volume_group_watcher_func.go index 1a306dba..6f555286 100644 --- a/images/agent/pkg/controller/lvm_volume_group_watcher_func.go +++ b/images/agent/pkg/controller/lvm_volume_group_watcher_func.go @@ -489,26 +489,15 @@ func UpdateLVMVolumeGroupTagsName(log logger.Logger, metrics monitoring.Metrics, } func ResizeThinPool(ctx context.Context, cl client.Client, log logger.Logger, metrics monitoring.Metrics, lvg *v1alpha1.LvmVolumeGroup, specThinPool v1alpha1.SpecThinPool, statusThinPool v1alpha1.StatusThinPool, nodeName string, resizeDelta resource.Quantity) (bool, error) { - volumeGroupSize, err := resource.ParseQuantity(lvg.Status.VGSize) - if err != nil { - log.Error(err, fmt.Sprintf("[ResizeThinPool] error ParseQuantity, resource name: %s", lvg.Name)) - return true, err - } - - volumeGroupAllocatedSize, err := resource.ParseQuantity(lvg.Status.VGSize) - if err != nil { - log.Error(err, fmt.Sprintf("[ResizeThinPool] error ParseQuantity, resource name: %s", lvg.Name)) - return true, err - } - - volumeGroupFreeSpaceBytes := volumeGroupSize.Value() - volumeGroupAllocatedSize.Value() + volumeGroupFreeSpaceBytes := lvg.Status.VGSize.Value() - lvg.Status.AllocatedSize.Value() addSizeBytes := specThinPool.Size.Value() - statusThinPool.ActualSize.Value() - log.Debug(fmt.Sprintf("[ResizeThinPool] volumeGroupSize = %s", volumeGroupSize.String())) - log.Debug(fmt.Sprintf("[ResizeThinPool] volumeGroupAllocatedSize = %s", volumeGroupAllocatedSize.String())) + log.Debug(fmt.Sprintf("[ResizeThinPool] volumeGroupSize = %s", lvg.Status.VGSize.String())) + log.Debug(fmt.Sprintf("[ResizeThinPool] volumeGroupAllocatedSize = %s", lvg.Status.AllocatedSize.String())) log.Debug(fmt.Sprintf("[ResizeThinPool] volumeGroupFreeSpaceBytes = %d", volumeGroupFreeSpaceBytes)) log.Debug(fmt.Sprintf("[ResizeThinPool] addSizeBytes = %d", addSizeBytes)) + var err error if addSizeBytes <= 0 { err = fmt.Errorf("thin pool name: %s; add size value <= 0, specThinPool.Size: %s, statusThinPool.ActualSize: %s", specThinPool.Name, specThinPool.Size.String(), statusThinPool.ActualSize.String()) log.Error(err, "[ResizeThinPool]: ") diff --git a/images/agent/pkg/controller/lvm_volume_group_watcher_test.go b/images/agent/pkg/controller/lvm_volume_group_watcher_test.go index fce7c7fe..a14fd588 100644 --- a/images/agent/pkg/controller/lvm_volume_group_watcher_test.go +++ b/images/agent/pkg/controller/lvm_volume_group_watcher_test.go @@ -326,11 +326,11 @@ func TestLVMVolumeGroupWatcherCtrl(t *testing.T) { }) t.Run("ValidateLVMGroup_type_local_selected_absent_bds_validation_fails", func(t *testing.T) { - const name = "test_name" + const lvgName = "test_name" - testObj := &v1alpha1.LvmVolumeGroup{ + lvg := &v1alpha1.LvmVolumeGroup{ ObjectMeta: metav1.ObjectMeta{ - Name: name, + Name: lvgName, Namespace: namespace, }, Spec: v1alpha1.LvmVolumeGroupSpec{ @@ -339,24 +339,24 @@ func TestLVMVolumeGroupWatcherCtrl(t *testing.T) { }, } - err := cl.Create(ctx, testObj) + err := cl.Create(ctx, lvg) if err != nil { t.Error(err) } else { defer func() { - err = cl.Delete(ctx, testObj) + err = cl.Delete(ctx, lvg) if err != nil { t.Error(err) } }() } - valid, status, err := CheckLVMVGNodeOwnership(ctx, cl, metrics, testObj, namespace, "test_node") + valid, status, err := CheckLVMVGNodeOwnership(ctx, cl, metrics, lvg, namespace, "test_node") assert.False(t, valid) if assert.NotNil(t, status) { assert.Equal(t, NonOperational, status.Health) + assert.EqualError(t, err, "error getBlockDevice: blockdevices.storage.deckhouse.io \"test_bd\" not found") } - assert.EqualError(t, err, "blockdevices.storage.deckhouse.io \"test_bd\" not found") }) t.Run("ValidateLVMGroup_type_local_selected_bds_from_different_nodes_validation_fails", func(t *testing.T) { @@ -441,88 +441,6 @@ func TestLVMVolumeGroupWatcherCtrl(t *testing.T) { assert.Nil(t, err) }) - t.Run("ValidateLVMGroup_type_local_no_bds_affiliated_to_the_node_validation_fails", func(t *testing.T) { - const ( - name = "test_name" - firstBd = "first" - secondBd = "second" - testNode = "test_node" - ) - - bds := &v1alpha1.BlockDeviceList{ - Items: []v1alpha1.BlockDevice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: firstBd, - Namespace: namespace, - }, - Status: v1alpha1.BlockDeviceStatus{ - NodeName: testNode, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: secondBd, - Namespace: namespace, - }, - Status: v1alpha1.BlockDeviceStatus{ - NodeName: testNode, - }, - }, - }, - } - - var err error - for _, bd := range bds.Items { - err = cl.Create(ctx, &bd) - if err != nil { - t.Error(err) - } - } - - if err == nil { - defer func() { - for _, bd := range bds.Items { - err = cl.Delete(ctx, &bd) - if err != nil { - t.Error(err) - } - } - }() - } - - testLvg := &v1alpha1.LvmVolumeGroup{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, - }, - Spec: v1alpha1.LvmVolumeGroupSpec{ - BlockDeviceNames: []string{firstBd, secondBd}, - Type: Local, - }, - } - - err = cl.Create(ctx, testLvg) - if err != nil { - t.Error(err) - } else { - defer func() { - err = cl.Delete(ctx, testLvg) - if err != nil { - t.Error(err) - } - }() - } - - valid, status, err := CheckLVMVGNodeOwnership(ctx, cl, metrics, testLvg, namespace, "another-node") - assert.False(t, valid) - if assert.NotNil(t, status) { - assert.Equal(t, NonOperational, status.Health) - assert.Equal(t, "selected block devices not affiliated to current Watcher's node", status.Message) - } - assert.Nil(t, err) - }) - t.Run("ValidateLVMGroup_type_local_validation_passes", func(t *testing.T) { const ( name = "test_name" @@ -579,234 +497,9 @@ func TestLVMVolumeGroupWatcherCtrl(t *testing.T) { Namespace: namespace, }, Spec: v1alpha1.LvmVolumeGroupSpec{ - BlockDeviceNames: []string{firstBd, secondBd}, - Type: Local, - }, - } - - err = cl.Create(ctx, testLvg) - if err != nil { - t.Error(err) - } else { - defer func() { - err = cl.Delete(ctx, testLvg) - if err != nil { - t.Error(err) - } - }() - } - - valid, status, err := CheckLVMVGNodeOwnership(ctx, cl, metrics, testLvg, namespace, testNode) - assert.True(t, valid) - if assert.NotNil(t, status) { - assert.Equal(t, "", status.Health) - assert.Equal(t, "", status.Message) - } - assert.Nil(t, err) - }) - - t.Run("ValidateLVMGroup_type_shared_selected_several_bds_validation_fails", func(t *testing.T) { - const ( - name = "test_name" - firstBd = "first" - secondBd = "second" - testNode = "test_node" - ) - - bds := &v1alpha1.BlockDeviceList{ - Items: []v1alpha1.BlockDevice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: firstBd, - Namespace: namespace, - }, - Status: v1alpha1.BlockDeviceStatus{ - NodeName: testNode, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: secondBd, - Namespace: namespace, - }, - Status: v1alpha1.BlockDeviceStatus{ - NodeName: "another_node", - }, - }, - }, - } - - var err error - for _, bd := range bds.Items { - err = cl.Create(ctx, &bd) - if err != nil { - t.Error(err) - } - } - - if err == nil { - defer func() { - for _, bd := range bds.Items { - err = cl.Delete(ctx, &bd) - if err != nil { - t.Error(err) - } - } - }() - } - - testLvg := &v1alpha1.LvmVolumeGroup{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, - }, - Spec: v1alpha1.LvmVolumeGroupSpec{ - BlockDeviceNames: []string{firstBd, secondBd}, - Type: Shared, - }, - } - - err = cl.Create(ctx, testLvg) - if err != nil { - t.Error(err) - } else { - defer func() { - err = cl.Delete(ctx, testLvg) - if err != nil { - t.Error(err) - } - }() - } - - valid, status, err := CheckLVMVGNodeOwnership(ctx, cl, metrics, testLvg, namespace, testNode) - assert.False(t, valid) - if assert.NotNil(t, status) { - assert.Equal(t, NonOperational, status.Health) - assert.Equal(t, "several block devices are selected for the shared LVMVolumeGroup", status.Message) - } - assert.EqualError(t, err, "several block devices are selected for the shared LVMVolumeGroup") - }) - - t.Run("ValidateLVMGroup_type_shared_selected_absent_bd_validation_fails", func(t *testing.T) { - const ( - name = "test_name" - firstBd = "first" - testNode = "test_node" - ) - - bds := &v1alpha1.BlockDeviceList{ - Items: []v1alpha1.BlockDevice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: firstBd, - Namespace: namespace, - }, - Status: v1alpha1.BlockDeviceStatus{ - NodeName: testNode, - }, - }, - }, - } - - var err error - for _, bd := range bds.Items { - err = cl.Create(ctx, &bd) - if err != nil { - t.Error(err) - } - } - - if err == nil { - defer func() { - for _, bd := range bds.Items { - err = cl.Delete(ctx, &bd) - if err != nil { - t.Error(err) - } - } - }() - } - - testLvg := &v1alpha1.LvmVolumeGroup{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, - }, - Spec: v1alpha1.LvmVolumeGroupSpec{ - BlockDeviceNames: []string{"absent_bd"}, - Type: Shared, - }, - } - - err = cl.Create(ctx, testLvg) - if err != nil { - t.Error(err) - } else { - defer func() { - err = cl.Delete(ctx, testLvg) - if err != nil { - t.Error(err) - } - }() - } - - valid, status, err := CheckLVMVGNodeOwnership(ctx, cl, metrics, testLvg, namespace, testNode) - assert.False(t, valid) - if assert.NotNil(t, status) { - assert.Equal(t, NonOperational, status.Health) - assert.Equal(t, "selected unknown block device for the shared LVMVolumeGroup", status.Message) - } - assert.EqualError(t, err, "blockdevices.storage.deckhouse.io \"absent_bd\" not found") - }) - - t.Run("ValidateLVMGroup_type_shared_validation_passes", func(t *testing.T) { - const ( - name = "test_name" - firstBd = "first" - testNode = "test_node" - ) - - bds := &v1alpha1.BlockDeviceList{ - Items: []v1alpha1.BlockDevice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: firstBd, - Namespace: namespace, - }, - Status: v1alpha1.BlockDeviceStatus{ - NodeName: testNode, - }, - }, - }, - } - - var err error - for _, bd := range bds.Items { - err = cl.Create(ctx, &bd) - if err != nil { - t.Error(err) - } - } - - if err == nil { - defer func() { - for _, bd := range bds.Items { - err = cl.Delete(ctx, &bd) - if err != nil { - t.Error(err) - } - } - }() - } - - testLvg := &v1alpha1.LvmVolumeGroup{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, - }, - Spec: v1alpha1.LvmVolumeGroupSpec{ - BlockDeviceNames: []string{firstBd}, - Type: Shared, + BlockDeviceNames: []string{firstBd, secondBd}, + Type: Local, + ActualVGNameOnTheNode: "some-vg", }, }