learning_go/rbd_exporter/RBDFactory/RBDFactory.go
a.pivkin 2e19663a63 puk
2025-12-22 07:57:42 +03:00

65 lines
1.4 KiB
Go

package RBDFactory
import (
"fmt"
"rbd_exporter/logger"
"rbd_exporter/mytypes"
)
func PoolFactory(cephConn mytypes.CephConnector, poolName string) (mytypes.Pool, error) {
var rbdlist []mytypes.IRBD = []mytypes.IRBD{}
ioctx, err := cephConn.OpenIOContext(poolName)
if err != nil {
return mytypes.Pool{}, fmt.Errorf("Couldn't set context for pool %s %w", poolName, err)
}
defer ioctx.Destroy()
imageList, err := ioctx.GetImageNames()
if err != nil {
return mytypes.Pool{}, fmt.Errorf("Couldn't get list of rbds %w", err)
}
for _, rbdname := range imageList {
stat, err := RBDFacroty(ioctx, rbdname)
if err != nil {
fmt.Errorf("Coundn't get stat from disk %s %w", rbdname, err)
panic(err)
}
rbdlist = append(rbdlist, stat)
}
return mytypes.Pool{
Name: poolName,
HasRBD: len(imageList) != 0,
RBDlist: rbdlist,
}, nil
}
func RBDFacroty(ioctx mytypes.IOContexter, rbdname string) (mytypes.RBD, error) {
defer func() {
if v := recover(); v != nil {
logger.Logger.Errorf("No such RBD exists (probably just deleted)")
}
}()
image := ioctx.GetImage(rbdname)
err := image.Open()
if err != nil {
panic(err)
// logger.Logger.Errorf("Shit happened in RBDFactory")
}
defer image.Close()
info,err := image.Stat()
if err != nil {
return mytypes.RBD{},fmt.Errorf("Couldn't get stats for image %s %w",rbdname,err)
}
return mytypes.RBD{
Name: rbdname,
ImageInfo: *info,
},nil
}