Skip to content

Commit

Permalink
[controller] Change size type from string to quantity (#46)
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Kramarenko <[email protected]>
  • Loading branch information
ViktorKram authored Apr 23, 2024
1 parent 14dc0d3 commit db46212
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 496 deletions.
35 changes: 18 additions & 17 deletions images/agent/api/v1alpha1/block_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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"`
}
8 changes: 4 additions & 4 deletions images/agent/api/v1alpha1/lvm_volume_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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"`
}
2 changes: 1 addition & 1 deletion images/agent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion images/agent/internal/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type LVMVolumeGroupCandidate struct {
type LVMVGStatusThinPool struct {
Name string
ActualSize resource.Quantity
UsedSize string
UsedSize resource.Quantity
}

type LVMVGDevice struct {
Expand Down
8 changes: 3 additions & 5 deletions images/agent/pkg/controller/block_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand All @@ -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 ||
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion images/agent/pkg/controller/block_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion images/agent/pkg/controller/controller_reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
34 changes: 4 additions & 30 deletions images/agent/pkg/controller/lvm_logical_volume_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 4 additions & 6 deletions images/agent/pkg/controller/lvm_logical_volume_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
46 changes: 17 additions & 29 deletions images/agent/pkg/controller/lvm_volume_group_discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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),
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
},
}
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
},
}
Expand Down Expand Up @@ -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,
})
Expand Down
Loading

0 comments on commit db46212

Please sign in to comment.