-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[controller] Add a sds scaner and a cache (#47)
Signed-off-by: Viktor Kramarenko <viktor.kramarenko@flant.com>
1 parent
fe2c39b
commit 9cb4133
Showing
12 changed files
with
412 additions
and
13 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
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
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,87 @@ | ||
package cache | ||
|
||
import ( | ||
"fmt" | ||
"sds-node-configurator/internal" | ||
"sds-node-configurator/pkg/logger" | ||
) | ||
|
||
type Cache struct { | ||
devices []internal.Device | ||
pvs []internal.PVData | ||
vgs []internal.VGData | ||
lvs []internal.LVData | ||
} | ||
|
||
func New() Cache { | ||
return Cache{} | ||
} | ||
|
||
func (c *Cache) StoreDevices(devices []internal.Device) { | ||
c.devices = devices | ||
} | ||
|
||
func (c *Cache) GetDevices() []internal.Device { | ||
dst := make([]internal.Device, len(c.devices)) | ||
copy(dst, c.devices) | ||
|
||
return dst | ||
} | ||
|
||
func (c *Cache) StorePVs(pvs []internal.PVData) { | ||
c.pvs = pvs | ||
} | ||
|
||
func (c *Cache) GetPVs() []internal.PVData { | ||
dst := make([]internal.PVData, len(c.pvs)) | ||
copy(dst, c.pvs) | ||
|
||
return dst | ||
} | ||
|
||
func (c *Cache) StoreVGs(vgs []internal.VGData) { | ||
c.vgs = vgs | ||
} | ||
|
||
func (c *Cache) GetVGs() []internal.VGData { | ||
dst := make([]internal.VGData, len(c.vgs)) | ||
copy(dst, c.vgs) | ||
|
||
return dst | ||
} | ||
|
||
func (c *Cache) StoreLVs(lvs []internal.LVData) { | ||
c.lvs = lvs | ||
} | ||
|
||
func (c *Cache) GetLVs() []internal.LVData { | ||
dst := make([]internal.LVData, len(c.lvs)) | ||
copy(dst, c.lvs) | ||
|
||
return dst | ||
} | ||
|
||
func (c *Cache) PrintTheCache(log logger.Logger) { | ||
log.Cache("*****************CACHE BEGIN*****************") | ||
log.Cache("[Devices BEGIN]") | ||
for _, d := range c.devices { | ||
log.Cache(fmt.Sprintf(" Device Name: %s, size: %s, fsType: %s, serial: %s, wwn: %s", d.Name, d.Size.String(), d.FSType, d.Serial, d.Wwn)) | ||
} | ||
log.Cache("[Devices ENDS]") | ||
log.Cache("[PVs BEGIN]") | ||
for _, pv := range c.pvs { | ||
log.Cache(fmt.Sprintf(" PV Name: %s, VG Name: %s, size: %s, vgTags: %s", pv.PVName, pv.VGName, pv.PVSize.String(), pv.VGTags)) | ||
} | ||
log.Cache("[PVs ENDS]") | ||
log.Cache("[VGs BEGIN]") | ||
for _, vg := range c.vgs { | ||
log.Cache(fmt.Sprintf(" VG Name: %s, size: %s, free: %s, vgTags: %s", vg.VGName, vg.VGSize.String(), vg.VGFree.String(), vg.VGTags)) | ||
} | ||
log.Cache("[VGs ENDS]") | ||
log.Cache("[LVs BEGIN]") | ||
for _, lv := range c.lvs { | ||
log.Cache(fmt.Sprintf(" LV Name: %s, VG name: %s, size: %s, tags: %s, attr: %s, pool: %s", lv.LVName, lv.VGName, lv.LVSize.String(), lv.LvTags, lv.LVAttr, lv.PoolLv)) | ||
} | ||
log.Cache("[LVs ENDS]") | ||
log.Cache("*****************CACHE ENDS*****************") | ||
} |
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,68 @@ | ||
package cache | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"sds-node-configurator/internal" | ||
"testing" | ||
) | ||
|
||
func TestCache(t *testing.T) { | ||
sdsCache := New() | ||
devices := []internal.Device{ | ||
{ | ||
Name: "test-1", | ||
}, | ||
{ | ||
Name: "test-2", | ||
}, | ||
{ | ||
Name: "test-3", | ||
}, | ||
} | ||
|
||
pvs := []internal.PVData{ | ||
{ | ||
PVName: "pv-1", | ||
}, | ||
{ | ||
PVName: "pv-2", | ||
}, | ||
{ | ||
PVName: "pv-3", | ||
}, | ||
} | ||
|
||
vgs := []internal.VGData{ | ||
{ | ||
VGName: "vg-1", | ||
}, | ||
{ | ||
VGName: "vg-2", | ||
}, | ||
{ | ||
VGName: "vg-3", | ||
}, | ||
} | ||
|
||
lvs := []internal.LVData{ | ||
{ | ||
LVName: "lv-1", | ||
}, | ||
{ | ||
LVName: "lv-2", | ||
}, | ||
{ | ||
LVName: "lv-3", | ||
}, | ||
} | ||
|
||
sdsCache.StoreDevices(devices) | ||
sdsCache.StorePVs(pvs) | ||
sdsCache.StoreVGs(vgs) | ||
sdsCache.StoreLVs(lvs) | ||
|
||
assert.ElementsMatch(t, devices, sdsCache.GetDevices()) | ||
assert.ElementsMatch(t, pvs, sdsCache.GetPVs()) | ||
assert.ElementsMatch(t, vgs, sdsCache.GetVGs()) | ||
assert.ElementsMatch(t, lvs, sdsCache.GetLVs()) | ||
} |
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,158 @@ | ||
package controller | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/pilebones/go-udev/netlink" | ||
"sds-node-configurator/config" | ||
"sds-node-configurator/internal" | ||
"sds-node-configurator/pkg/cache" | ||
"sds-node-configurator/pkg/logger" | ||
"sds-node-configurator/pkg/throttler" | ||
"sds-node-configurator/pkg/utils" | ||
"time" | ||
) | ||
|
||
func RunScanner(log logger.Logger, cfg config.Options, sdsCache cache.Cache) error { | ||
log.Info("[RunScanner] starts the work") | ||
|
||
t := throttler.New(cfg.ThrottleInterval * time.Second) | ||
|
||
conn := new(netlink.UEventConn) | ||
if err := conn.Connect(netlink.UdevEvent); err != nil { | ||
log.Error(err, "[RunScanner] Failed to connect to Netlink") | ||
return err | ||
} | ||
log.Debug("[RunScanner] system socket connection succeeded") | ||
|
||
errChan := make(chan error) | ||
eventChan := make(chan netlink.UEvent) | ||
matcher := &netlink.RuleDefinitions{ | ||
Rules: []netlink.RuleDefinition{ | ||
{ | ||
Env: map[string]string{ | ||
"SUBSYSTEM": "block", | ||
}, | ||
}, | ||
}, | ||
} | ||
quit := conn.Monitor(eventChan, errChan, matcher) | ||
|
||
log.Info("[RunScanner] start to listen to events") | ||
|
||
timer := time.NewTimer(1 * time.Second) | ||
for { | ||
select { | ||
case device, open := <-eventChan: | ||
timer.Reset(1 * time.Second) | ||
log.Debug(fmt.Sprintf("[RunScanner] event triggered for device: %s", device.Env["DEVNAME"])) | ||
log.Trace(fmt.Sprintf("[RunScanner] device from the event: %s", device.String())) | ||
if !open { | ||
err := errors.New("EventChan has been closed when monitor udev event") | ||
log.Error(err, "[RunScanner] unable to read from the event channel") | ||
return err | ||
} | ||
|
||
t.Do(func() { | ||
log.Info("[RunScanner] start to fill the cache") | ||
err := fillTheCache(log, sdsCache) | ||
if err != nil { | ||
log.Error(err, "[RunScanner] unable to fill the cache") | ||
return | ||
} | ||
|
||
log.Info("[RunScanner] successfully filled the cache") | ||
}) | ||
|
||
case err := <-errChan: | ||
log.Error(err, "[RunScanner] Monitor udev event error") | ||
return err | ||
|
||
case <-quit: | ||
err := errors.New("receive quit signal when monitor udev event") | ||
log.Error(err, "[RunScanner] unable to read from the event channel") | ||
return err | ||
|
||
case <-timer.C: | ||
log.Info("[RunScanner] events ran out. Start to fill the cache") | ||
err := fillTheCache(log, sdsCache) | ||
if err != nil { | ||
log.Error(err, "[RunScanner] unable to fill the cache after all events passed") | ||
break | ||
} | ||
log.Info("[RunScanner] successfully filled the cache after all events passed") | ||
} | ||
} | ||
} | ||
|
||
func fillTheCache(log logger.Logger, cache cache.Cache) error { | ||
devices, err := scanDevices(log) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pvs, err := scanPVs(log) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
vgs, err := scanVGs(log) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
lvs, err := scanLVs(log) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Debug("[fillTheCache] successfully scanned entities. Starts to fill the cache") | ||
cache.StoreDevices(devices) | ||
cache.StorePVs(pvs) | ||
cache.StoreVGs(vgs) | ||
cache.StoreLVs(lvs) | ||
log.Debug("[fillTheCache] successfully filled the cache") | ||
cache.PrintTheCache(log) | ||
|
||
return nil | ||
} | ||
|
||
func scanDevices(log logger.Logger) ([]internal.Device, error) { | ||
devices, cmdStr, err := utils.GetBlockDevices() | ||
if err != nil { | ||
log.Error(err, fmt.Sprintf("[ScanDevices] unable to scan the devices, cmd: %s", cmdStr)) | ||
return nil, err | ||
} | ||
|
||
return devices, nil | ||
} | ||
|
||
func scanPVs(log logger.Logger) ([]internal.PVData, error) { | ||
pvs, cmdStr, _, err := utils.GetAllPVs() | ||
if err != nil { | ||
log.Error(err, fmt.Sprintf("[ScanPVs] unable to scan the PVs, cmd: %s", cmdStr)) | ||
return nil, err | ||
} | ||
|
||
return pvs, nil | ||
} | ||
|
||
func scanVGs(log logger.Logger) ([]internal.VGData, error) { | ||
vgs, cmdStr, _, err := utils.GetAllVGs() | ||
if err != nil { | ||
log.Error(err, fmt.Sprintf("[ScanVGs] unable to scan the VGs, cmd: %s", cmdStr)) | ||
return nil, err | ||
} | ||
|
||
return vgs, nil | ||
} | ||
|
||
func scanLVs(log logger.Logger) ([]internal.LVData, error) { | ||
lvs, cmdStr, _, err := utils.GetAllLVs() | ||
if err != nil { | ||
log.Error(err, fmt.Sprintf("[ScanLVs] unable to scan LVs, cmd: %s", cmdStr)) | ||
return nil, err | ||
} | ||
|
||
return lvs, nil | ||
} |
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,36 @@ | ||
package throttler | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Throttler interface { | ||
Do(f func()) | ||
} | ||
|
||
type throttle struct { | ||
duration time.Duration | ||
once sync.Once | ||
m sync.Mutex | ||
} | ||
|
||
func (t *throttle) Do(f func()) { | ||
t.m.Lock() | ||
defer t.m.Unlock() | ||
t.once.Do(func() { | ||
go func() { | ||
time.Sleep(t.duration) | ||
t.m.Lock() | ||
defer t.m.Unlock() | ||
t.once = sync.Once{} | ||
}() | ||
f() | ||
}) | ||
} | ||
|
||
func New(duration time.Duration) Throttler { | ||
return &throttle{ | ||
duration: duration, | ||
} | ||
} |
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