Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[controller] Add some logic refactoring and minor fixes #37

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions images/sds-local-volume-scheduler-extender/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ func (c *Cache) AddLVG(lvg *v1alpha1.LvmVolumeGroup) {
return
}

c.log.Trace(fmt.Sprintf("[AddLVG] the LVMVolumeGroup %s nodes: %v", lvg.Name, lvg.Status.Nodes))
for _, node := range lvg.Status.Nodes {
lvgsOnTheNode, _ := c.nodeLVGs.Load(node.Name)
if lvgsOnTheNode == nil {
lvgsOnTheNode = make([]string, 0, lvgsPerNodeCount)
}

lvgsOnTheNode = append(lvgsOnTheNode.([]string), lvg.Name)
c.log.Debug(fmt.Sprintf("[AddLVG] the LVMVolumeGroup %s has been added to the node %s", lvg.Name, node.Name))
c.nodeLVGs.Store(node.Name, lvgsOnTheNode)
}
}
Expand All @@ -66,6 +68,23 @@ func (c *Cache) AddLVG(lvg *v1alpha1.LvmVolumeGroup) {
func (c *Cache) UpdateLVG(lvg *v1alpha1.LvmVolumeGroup) error {
if cache, found := c.lvgs.Load(lvg.Name); found {
cache.(*lvgCache).lvg = lvg

c.log.Trace(fmt.Sprintf("[UpdateLVG] the LVMVolumeGroup %s nodes: %v", lvg.Name, lvg.Status.Nodes))
for _, node := range lvg.Status.Nodes {
lvgsOnTheNode, _ := c.nodeLVGs.Load(node.Name)
if lvgsOnTheNode == nil {
lvgsOnTheNode = make([]string, 0, lvgsPerNodeCount)
}

if !slices2.Contains(lvgsOnTheNode.([]string), lvg.Name) {
lvgsOnTheNode = append(lvgsOnTheNode.([]string), lvg.Name)
c.log.Debug(fmt.Sprintf("[UpdateLVG] the LVMVolumeGroup %s has been added to the node %s", lvg.Name, node.Name))
c.nodeLVGs.Store(node.Name, lvgsOnTheNode)
} else {
c.log.Debug(fmt.Sprintf("[UpdateLVG] the LVMVolumeGroup %s has been already added to the node %s", lvg.Name, node.Name))
}
}

return nil
}

Expand Down
20 changes: 10 additions & 10 deletions images/sds-local-volume-scheduler-extender/pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func TestCache_UpdateLVG(t *testing.T) {
Name: name,
},
Status: v1alpha1.LvmVolumeGroupStatus{
AllocatedSize: "1Gi",
AllocatedSize: resource.MustParse("1Gi"),
},
}
cache.AddLVG(lvg)
Expand All @@ -314,7 +314,7 @@ func TestCache_UpdateLVG(t *testing.T) {
Name: name,
},
Status: v1alpha1.LvmVolumeGroupStatus{
AllocatedSize: "2Gi",
AllocatedSize: resource.MustParse("2Gi"),
},
}

Expand Down Expand Up @@ -352,10 +352,10 @@ func BenchmarkCache_UpdateLVG(b *testing.B) {
Name: name,
},
Status: v1alpha1.LvmVolumeGroupStatus{
AllocatedSize: fmt.Sprintf("2%dGi", i),
AllocatedSize: resource.MustParse(fmt.Sprintf("2%dGi", i)),
},
}
b.Logf("updates the LVG with allocated size: %s", updated.Status.AllocatedSize)
b.Logf("updates the LVG with allocated size: %s", updated.Status.AllocatedSize.String())
err := cache.UpdateLVG(updated)
if err != nil {
b.Error(err)
Expand Down Expand Up @@ -435,7 +435,7 @@ func BenchmarkCache_FullLoad(b *testing.B) {
Name: nodeName,
},
},
AllocatedSize: fmt.Sprintf("1%dGi", i),
AllocatedSize: resource.MustParse(fmt.Sprintf("1%dGi", i)),
},
},
{
Expand All @@ -448,7 +448,7 @@ func BenchmarkCache_FullLoad(b *testing.B) {
Name: nodeName,
},
},
AllocatedSize: fmt.Sprintf("1%dGi", i),
AllocatedSize: resource.MustParse(fmt.Sprintf("1%dGi", i)),
},
},
{
Expand All @@ -461,7 +461,7 @@ func BenchmarkCache_FullLoad(b *testing.B) {
Name: nodeName,
},
},
AllocatedSize: fmt.Sprintf("1%dGi", i),
AllocatedSize: resource.MustParse(fmt.Sprintf("1%dGi", i)),
},
},
}
Expand Down Expand Up @@ -505,23 +505,23 @@ func BenchmarkCache_FullLoad(b *testing.B) {
Name: fmt.Sprintf("test-lvg-%d", i),
},
Status: v1alpha1.LvmVolumeGroupStatus{
AllocatedSize: fmt.Sprintf("1%dGi", i+1),
AllocatedSize: resource.MustParse(fmt.Sprintf("1%dGi", i+1)),
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("test-lvg-%d", i+1),
},
Status: v1alpha1.LvmVolumeGroupStatus{
AllocatedSize: fmt.Sprintf("1%dGi", i+1),
AllocatedSize: resource.MustParse(fmt.Sprintf("1%dGi", i+1)),
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("test-lvg-%d", i+2),
},
Status: v1alpha1.LvmVolumeGroupStatus{
AllocatedSize: fmt.Sprintf("1%dGi", i+1),
AllocatedSize: resource.MustParse(fmt.Sprintf("1%dGi", i+1)),
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ func RunLVGWatcherCacheController(
return
}

err = cache.UpdateLVG(newLvg)
if err != nil {
log.Error(err, fmt.Sprintf("[RunLVGWatcherCacheController] unable to update the LVMVolumeGroup %s cache", newLvg.Name))
return
}
log.Debug(fmt.Sprintf("[RunLVGWatcherCacheController] successfully updated the LVMVolumeGroup %s in the cache", newLvg.Name))

log.Debug(fmt.Sprintf("[RunLVGWatcherCacheController] starts to calculate the size difference for LVMVolumeGroup %s", newLvg.Name))
log.Trace(fmt.Sprintf("[RunLVGWatcherCacheController] old state LVMVolumeGroup %s has size %s", oldLvg.Name, oldLvg.Status.AllocatedSize.String()))
log.Trace(fmt.Sprintf("[RunLVGWatcherCacheController] new state LVMVolumeGroup %s has size %s", newLvg.Name, newLvg.Status.AllocatedSize.String()))
Expand All @@ -114,14 +121,7 @@ func RunLVGWatcherCacheController(
log.Debug(fmt.Sprintf("[RunLVGWatcherCacheController] the LVMVolumeGroup %s should not be reconciled", newLvg.Name))
return
}

log.Debug(fmt.Sprintf("[RunLVGWatcherCacheController] the LVMVolumeGroup %s should be reconciled by Update Func. It will be updated in the cache", newLvg.Name))
err = cache.UpdateLVG(newLvg)
if err != nil {
log.Error(err, fmt.Sprintf("[RunLVGWatcherCacheController] unable to update the LVMVolumeGroup %s cache", newLvg.Name))
return
}
log.Debug(fmt.Sprintf("[RunLVGWatcherCacheController] successfully updated the LVMVolumeGroup %s in the cache", newLvg.Name))
log.Debug(fmt.Sprintf("[RunLVGWatcherCacheController] the LVMVolumeGroup %s should be reconciled by Update Func", newLvg.Name))

cachedPVCs, err := cache.GetAllPVCForLVG(newLvg.Name)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func filterNodes(

usedLVGs := RemoveUnusedLVGs(lvgs, scLVGs)
for _, lvg := range usedLVGs {
log.Trace(fmt.Sprintf("[filterNodes] the LVMVolumeGroup %s is actually used. VG size: %s, allocatedSize: %s", lvg.Name, lvg.Status.VGSize, lvg.Status.AllocatedSize))
log.Trace(fmt.Sprintf("[filterNodes] the LVMVolumeGroup %s is actually used. VG size: %s, allocatedSize: %s", lvg.Name, lvg.Status.VGSize.String(), lvg.Status.AllocatedSize.String()))
}

lvgsThickFree := getLVGThickFreeSpaces(log, usedLVGs)
Expand Down