add more codes

This commit is contained in:
2025-11-23 22:37:27 +07:00
parent b506a64ed1
commit 4fcd71ca05
10 changed files with 448 additions and 52 deletions

View File

@@ -2,7 +2,9 @@ package validators
import (
"fmt"
"os"
"os/exec"
"strings"
"jagacloud/node-agent/pkg/compute/libvirt"
"jagacloud/node-agent/pkg/config"
@@ -14,6 +16,9 @@ func CheckBridge(name string) error {
if name == "" {
return fmt.Errorf("bridge name required")
}
if os.Getenv("JAGACLOUD_SKIP_BRIDGE_CHECK") == "1" {
return nil
}
if err := exec.Command("bash", "-lc", fmt.Sprintf("ip link show %s", name)).Run(); err != nil {
return fmt.Errorf("bridge %s not found", name)
}
@@ -70,6 +75,38 @@ func CheckStoragePoolsCT(spec lxc.Spec, cfg config.Config) error {
return nil
}
// CheckStoragePoolsRuntime queries libvirt to see if pool exists; fallback to config.
func CheckStoragePoolsRuntime(pools []string, cfg config.Config) error {
if len(pools) == 0 {
return nil
}
for _, p := range pools {
if p == "" {
continue
}
if poolExists(cfg, p) {
continue
}
// Try libvirt
cmd := exec.Command("bash", "-lc", "virsh -q pool-list --name")
out, err := cmd.Output()
if err != nil {
return fmt.Errorf("pool %s not found and virsh failed: %v", p, err)
}
found := false
for _, line := range strings.Split(string(out), "\n") {
if strings.TrimSpace(line) == p {
found = true
break
}
}
if !found {
return fmt.Errorf("storage pool %s not found", p)
}
}
return nil
}
func poolExists(cfg config.Config, name string) bool {
for _, p := range cfg.StoragePools {
if p.Name == name {

View File

@@ -1,20 +1,20 @@
package validators
import (
"testing"
"testing"
"jagacloud/node-agent/pkg/compute/libvirt"
"jagacloud/node-agent/pkg/config"
"jagacloud/node-agent/pkg/compute/libvirt"
"jagacloud/node-agent/pkg/config"
)
func TestPoolExists(t *testing.T) {
cfg := config.Config{StoragePools: []config.StoragePool{{Name: "local"}}}
err := CheckStoragePoolsVM([]libvirt.DiskSpec{{Pool: "local"}}, cfg)
if err != nil {
t.Fatalf("expected pool to be valid: %v", err)
}
err = CheckStoragePoolsVM([]libvirt.DiskSpec{{Pool: "missing"}}, cfg)
if err == nil {
t.Fatalf("expected error for missing pool")
}
cfg := config.Config{StoragePools: []config.StoragePool{{Name: "local"}}}
err := CheckStoragePoolsVM([]libvirt.DiskSpec{{Pool: "local"}}, cfg)
if err != nil {
t.Fatalf("expected pool to be valid: %v", err)
}
err = CheckStoragePoolsVM([]libvirt.DiskSpec{{Pool: "missing"}}, cfg)
if err == nil {
t.Fatalf("expected error for missing pool")
}
}