From 575f52084c83d9db9fff6542706b5bdb6777eec3 Mon Sep 17 00:00:00 2001 From: rawdaGastan Date: Sun, 17 Sep 2023 12:58:02 +0300 Subject: [PATCH 1/3] validate cpus according to actual number of nodes cpus --- pkg/vm.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/pkg/vm.go b/pkg/vm.go index 655085021..e0c5991ef 100644 --- a/pkg/vm.go +++ b/pkg/vm.go @@ -6,6 +6,7 @@ import ( "net" "path/filepath" + "github.com/shirou/gopsutil/cpu" "github.com/threefoldtech/zos/pkg/gridtypes" "github.com/threefoldtech/zos/pkg/gridtypes/zos" ) @@ -126,7 +127,7 @@ type VM struct { Network VMNetworkInfo // KernelImage path to uncompressed linux kernel ELF KernelImage string - // InitrdImage (optiona) path to initrd disk + // InitrdImage (optional) path to initrd disk InitrdImage string // KernelArgs to override the default kernel arguments. (default: "ro console=ttyS0 noapic reboot=k panic=1 pci=off nomodules") KernelArgs KernelArgs @@ -175,8 +176,16 @@ func (vm *VM) Validate() error { return fmt.Errorf("invalid memory must not be less than 250M") } - if vm.CPU == 0 || vm.CPU > 32 { - return fmt.Errorf("invalid cpu must be between 1 and 32") + n, err := cpu.Counts(true) + if err != nil { + return fmt.Errorf("failed to get count of cpus") + } + + if vm.CPU == 0 || vm.CPU > uint8(n) { + if n == 1 { + return fmt.Errorf("invalid cpu must be 1") + } + return fmt.Errorf("invalid cpu must be between 1 and %d", n) } for _, shared := range vm.Shared { @@ -234,7 +243,7 @@ func (n *NetMetric) Nu() float64 { } // MachineMetric is a container for metrics from multiple networks -// currently only groped as private (wiregaurd + yggdrasil), and public (public Ips) +// currently only groped as private (wireguard + yggdrasil), and public (public Ips) type MachineMetric struct { Private NetMetric Public NetMetric From 03d48f6b718c3552c9fe5879c8b6fb3d10a4c37f Mon Sep 17 00:00:00 2001 From: rawdaGastan Date: Sun, 17 Sep 2023 13:16:15 +0300 Subject: [PATCH 2/3] fix typos --- pkg/gridtypes/zos/zmachine.go | 2 +- pkg/vm.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/gridtypes/zos/zmachine.go b/pkg/gridtypes/zos/zmachine.go index 793a289cf..2f755c622 100644 --- a/pkg/gridtypes/zos/zmachine.go +++ b/pkg/gridtypes/zos/zmachine.go @@ -184,7 +184,7 @@ func (v ZMachine) Valid(getter gridtypes.WorkloadGetter) error { } } if v.ComputeCapacity.CPU == 0 { - return fmt.Errorf("cpu capcity can't be 0") + return fmt.Errorf("cpu capacity can't be 0") } if v.ComputeCapacity.Memory < 250*gridtypes.Megabyte { return fmt.Errorf("mem capacity can't be less that 250M") diff --git a/pkg/vm.go b/pkg/vm.go index e0c5991ef..d160208b8 100644 --- a/pkg/vm.go +++ b/pkg/vm.go @@ -243,7 +243,7 @@ func (n *NetMetric) Nu() float64 { } // MachineMetric is a container for metrics from multiple networks -// currently only groped as private (wireguard + yggdrasil), and public (public Ips) +// currently only grouped as private (wireguard + yggdrasil), and public (public Ips) type MachineMetric struct { Private NetMetric Public NetMetric From f5d7c9db61a487e97b26897f45884ab95c997541 Mon Sep 17 00:00:00 2001 From: rawdaGastan Date: Mon, 25 Sep 2023 11:26:38 +0300 Subject: [PATCH 3/3] rename n to cpus --- pkg/vm.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/vm.go b/pkg/vm.go index d160208b8..b9ef42aa6 100644 --- a/pkg/vm.go +++ b/pkg/vm.go @@ -176,16 +176,16 @@ func (vm *VM) Validate() error { return fmt.Errorf("invalid memory must not be less than 250M") } - n, err := cpu.Counts(true) + cpus, err := cpu.Counts(true) if err != nil { return fmt.Errorf("failed to get count of cpus") } - if vm.CPU == 0 || vm.CPU > uint8(n) { - if n == 1 { + if vm.CPU == 0 || vm.CPU > uint8(cpus) { + if cpus == 1 { return fmt.Errorf("invalid cpu must be 1") } - return fmt.Errorf("invalid cpu must be between 1 and %d", n) + return fmt.Errorf("invalid cpu must be between 1 and %d", cpus) } for _, shared := range vm.Shared {