Files
jagacloud/pkg/validators/validators.go
2025-11-23 11:46:58 +07:00

81 lines
1.9 KiB
Go

package validators
import (
"fmt"
"os/exec"
"jagacloud/node-agent/pkg/compute/libvirt"
"jagacloud/node-agent/pkg/config"
"jagacloud/node-agent/pkg/containers/lxc"
)
// CheckBridge returns nil if the bridge exists on the host.
func CheckBridge(name string) error {
if name == "" {
return fmt.Errorf("bridge name required")
}
if err := exec.Command("bash", "-lc", fmt.Sprintf("ip link show %s", name)).Run(); err != nil {
return fmt.Errorf("bridge %s not found", name)
}
return nil
}
// CheckBridgeSet validates all bridges in NIC specs.
func CheckBridgeSet(nics []libvirt.NICSpec) error {
for _, nic := range nics {
if err := CheckBridge(nic.Bridge); err != nil {
return err
}
}
return nil
}
// CheckBridgeSetCT validates bridges for LXC NICs.
func CheckBridgeSetCT(nics []lxc.NICSpec) error {
for _, nic := range nics {
if err := CheckBridge(nic.Bridge); err != nil {
return err
}
}
return nil
}
// CheckStoragePool is a stub; implement LVM/ZFS/dir verification as needed.
func CheckStoragePool(name string) error {
if name == "" {
return fmt.Errorf("storage pool name required")
}
// TODO: query configured pools
return nil
}
// CheckStoragePoolsVM ensures all disks reference known pools from config.
func CheckStoragePoolsVM(disks []libvirt.DiskSpec, cfg config.Config) error {
for _, d := range disks {
if d.Pool == "" {
continue
}
if !poolExists(cfg, d.Pool) {
return fmt.Errorf("storage pool %s not configured", d.Pool)
}
}
return nil
}
// CheckStoragePoolsCT ensures rootfs pool exists.
func CheckStoragePoolsCT(spec lxc.Spec, cfg config.Config) error {
if spec.RootfsPool != "" && !poolExists(cfg, spec.RootfsPool) {
return fmt.Errorf("storage pool %s not configured", spec.RootfsPool)
}
return nil
}
func poolExists(cfg config.Config, name string) bool {
for _, p := range cfg.StoragePools {
if p.Name == name {
return true
}
}
return false
}