Skip to content

Commit

Permalink
Update crc-org/machine module
Browse files Browse the repository at this point in the history
With this update type of memory and cpu is changed to uint from int
which will allow us to fix some of interter overflow issue with latest
golint ci.
  • Loading branch information
praveenkumar committed Oct 8, 2024
1 parent 6689ec2 commit 7f4bfec
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 41 deletions.
12 changes: 6 additions & 6 deletions cmd/crc/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func init() {
flagSet := pflag.NewFlagSet("start", pflag.ExitOnError)
flagSet.StringP(crcConfig.Bundle, "b", constants.GetDefaultBundlePath(crcConfig.GetPreset(config)), crcConfig.BundleHelpMsg(config))
flagSet.StringP(crcConfig.PullSecretFile, "p", "", fmt.Sprintf("File path of image pull secret (download from %s)", constants.CrcLandingPageURL))
flagSet.IntP(crcConfig.CPUs, "c", constants.GetDefaultCPUs(crcConfig.GetPreset(config)), "Number of CPU cores to allocate to the instance")
flagSet.IntP(crcConfig.Memory, "m", constants.GetDefaultMemory(crcConfig.GetPreset(config)), "MiB of memory to allocate to the instance")
flagSet.UintP(crcConfig.CPUs, "c", constants.GetDefaultCPUs(crcConfig.GetPreset(config)), "Number of CPU cores to allocate to the instance")
flagSet.UintP(crcConfig.Memory, "m", constants.GetDefaultMemory(crcConfig.GetPreset(config)), "MiB of memory to allocate to the instance")
flagSet.UintP(crcConfig.DiskSize, "d", constants.DefaultDiskSize, "Total size in GiB of the disk used by the instance")
flagSet.StringP(crcConfig.NameServer, "n", "", "IPv4 address of nameserver to use for the instance")
flagSet.Bool(crcConfig.DisableUpdateCheck, false, "Don't check for update")
Expand Down Expand Up @@ -69,9 +69,9 @@ func runStart(ctx context.Context) (*types.StartResult, error) {

startConfig := types.StartConfig{
BundlePath: config.Get(crcConfig.Bundle).AsString(),
Memory: config.Get(crcConfig.Memory).AsInt(),
Memory: config.Get(crcConfig.Memory).AsUInt(),
DiskSize: config.Get(crcConfig.DiskSize).AsInt(),
CPUs: config.Get(crcConfig.CPUs).AsInt(),
CPUs: config.Get(crcConfig.CPUs).AsUInt(),
NameServer: config.Get(crcConfig.NameServer).AsString(),
PullSecret: cluster.NewInteractivePullSecretLoader(config),
KubeAdminPassword: config.Get(crcConfig.KubeAdminPassword).AsString(),
Expand Down Expand Up @@ -181,10 +181,10 @@ func (s *startResult) prettyPrintTo(writer io.Writer) error {
}

func validateStartFlags() error {
if err := validation.ValidateMemory(config.Get(crcConfig.Memory).AsInt(), crcConfig.GetPreset(config)); err != nil {
if err := validation.ValidateMemory(config.Get(crcConfig.Memory).AsUInt(), crcConfig.GetPreset(config)); err != nil {
return err
}
if err := validation.ValidateCPUs(config.Get(crcConfig.CPUs).AsInt(), crcConfig.GetPreset(config)); err != nil {
if err := validation.ValidateCPUs(config.Get(crcConfig.CPUs).AsUInt(), crcConfig.GetPreset(config)); err != nil {
return err
}
if err := validation.ValidateDiskSize(config.Get(crcConfig.DiskSize).AsInt()); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/containers/libhvee v0.7.1
github.com/coreos/go-systemd/v22 v22.5.0
github.com/crc-org/admin-helper v0.5.4
github.com/crc-org/machine v0.0.0-20240715101719-0c1bc9eb95f8
github.com/crc-org/machine v0.0.0-20240926103419-a943b47fd48b
github.com/crc-org/vfkit v0.5.1
github.com/cucumber/godog v0.14.1
github.com/cucumber/messages-go/v10 v10.0.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/crc-org/admin-helper v0.5.4 h1:Wq6wp6514MipPHHYdoL2VUyhUL9qh26wR1I3qPaVxf4=
github.com/crc-org/admin-helper v0.5.4/go.mod h1:sFkqIILzKrt62CH1bJn5PSBFSdhaCyMdz6BG37N3TBE=
github.com/crc-org/machine v0.0.0-20240715101719-0c1bc9eb95f8 h1:KVdN/5PTWRwth2suZEQCY54MXSXp1SAUd54CiE952oI=
github.com/crc-org/machine v0.0.0-20240715101719-0c1bc9eb95f8/go.mod h1:trWeQimjfE3dJ8qWOxI4ePtYm13aecK42bf01s6h/Nc=
github.com/crc-org/machine v0.0.0-20240926103419-a943b47fd48b h1:5577tKzQcPfd/i0dCekY32R9DUi677sNfhVLYKulBGM=
github.com/crc-org/machine v0.0.0-20240926103419-a943b47fd48b/go.mod h1:trWeQimjfE3dJ8qWOxI4ePtYm13aecK42bf01s6h/Nc=
github.com/crc-org/vfkit v0.5.1 h1:r1zNf1g1bLbgu5BgIQodirvYaIGWJQ91eS/PIgNO6lo=
github.com/crc-org/vfkit v0.5.1/go.mod h1:Hqi20zQcqXMk6JqvByvOidHYv+KzPx3G+cjkdGSWv60=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down
2 changes: 2 additions & 0 deletions pkg/crc/api/api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ func TestConfigGetAll(t *testing.T) {
switch v := v.Value.(type) {
case int:
configs[k] = float64(v)
case uint:
configs[k] = float64(v)
// when config of type Path is converted to JSON it is converted to string
case crcConfig.Path:
configs[k] = string(v)
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ func (h *Handler) Start(c *context) error {
func getStartConfig(cfg crcConfig.Storage, args client.StartConfig) types.StartConfig {
return types.StartConfig{
BundlePath: cfg.Get(crcConfig.Bundle).AsString(),
Memory: cfg.Get(crcConfig.Memory).AsInt(),
Memory: cfg.Get(crcConfig.Memory).AsUInt(),
DiskSize: cfg.Get(crcConfig.DiskSize).AsInt(),
CPUs: cfg.Get(crcConfig.CPUs).AsInt(),
CPUs: cfg.Get(crcConfig.CPUs).AsUInt(),
NameServer: cfg.Get(crcConfig.NameServer).AsString(),
PullSecret: cluster.NewNonInteractivePullSecretLoader(cfg, args.PullSecretFile),
KubeAdminPassword: cfg.Get(crcConfig.KubeAdminPassword).AsString(),
Expand Down
12 changes: 12 additions & 0 deletions pkg/crc/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ func (c *Config) Set(key string, value interface{}) (string, error) {
if err != nil {
return "", fmt.Errorf(invalidProp, value, key, err)
}
case uint:
castValue, err = cast.ToUintE(value)
if err != nil {
return "", fmt.Errorf(invalidProp, value, key, err)
}
case string, Secret:
castValue = cast.ToString(value)
case bool:
Expand Down Expand Up @@ -207,6 +212,13 @@ func (c *Config) Get(key string) SettingValue {
Invalid: true,
}
}
case uint:
value, err = cast.ToUintE(value)
if err != nil {
return SettingValue{
Invalid: true,
}
}
case string:
value = cast.ToString(value)
case Path:
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ func presetChanged(cfg *Config, _ string, _ interface{}) {
UpdateDefaults(cfg)
}

func defaultCPUs(cfg Storage) int {
func defaultCPUs(cfg Storage) uint {
return constants.GetDefaultCPUs(GetPreset(cfg))
}

func defaultMemory(cfg Storage) int {
func defaultMemory(cfg Storage) uint {
return constants.GetDefaultMemory(GetPreset(cfg))
}

Expand Down
18 changes: 9 additions & 9 deletions pkg/crc/config/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

// override for ValidateMemory in validations.go to disable the physical memory check
func validateMemoryNoPhysicalCheck(value interface{}, preset crcpreset.Preset) (bool, string) {
v, err := cast.ToIntE(value)
v, err := cast.ToUintE(value)
if err != nil {
return false, fmt.Sprintf("requires integer value in MiB >= %d", constants.GetDefaultMemory(preset))
}
Expand Down Expand Up @@ -81,15 +81,15 @@ func TestSetPreset(t *testing.T) {
_, err = cfg.Set(Memory, 10800)
require.NoError(t, err)
assert.Equal(t, SettingValue{
Value: 10800,
Value: uint(10800),
Invalid: false,
IsDefault: false,
IsSecret: false,
}, cfg.Get(Memory))
_, err = cfg.Set(CPUs, 3)
require.NoError(t, err)
assert.Equal(t, SettingValue{
Value: 3,
Value: uint(3),
Invalid: false,
IsDefault: false,
IsSecret: false,
Expand All @@ -99,13 +99,13 @@ func TestSetPreset(t *testing.T) {
_, err = cfg.Set(Preset, crcpreset.OpenShift)
require.NoError(t, err)
assert.Equal(t, SettingValue{
Value: 10800,
Value: uint(10800),
Invalid: false,
IsDefault: false,
IsSecret: false,
}, cfg.Get(Memory))
assert.Equal(t, SettingValue{
Value: 4,
Value: uint(4),
Invalid: false,
IsDefault: true,
IsSecret: false,
Expand All @@ -123,15 +123,15 @@ func TestUnsetPreset(t *testing.T) {
_, err = cfg.Set(Memory, 10800)
require.NoError(t, err)
assert.Equal(t, SettingValue{
Value: 10800,
Value: uint(10800),
Invalid: false,
IsDefault: false,
IsSecret: false,
}, cfg.Get(Memory))
_, err = cfg.Set(CPUs, 3)
require.NoError(t, err)
assert.Equal(t, SettingValue{
Value: 3,
Value: uint(3),
Invalid: false,
IsDefault: false,
IsSecret: false,
Expand All @@ -147,13 +147,13 @@ func TestUnsetPreset(t *testing.T) {
IsSecret: false,
}, cfg.Get(Preset))
assert.Equal(t, SettingValue{
Value: 10800,
Value: uint(10800),
Invalid: false,
IsDefault: false,
IsSecret: false,
}, cfg.Get(Memory))
assert.Equal(t, SettingValue{
Value: 4,
Value: uint(4),
Invalid: false,
IsDefault: true,
IsSecret: false,
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/config/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func validatePersistentVolumeSize(value interface{}) (bool, string) {

// validateCPUs checks if provided cpus count is valid in the config
func validateCPUs(value interface{}, preset crcpreset.Preset) (bool, string) {
v, err := cast.ToIntE(value)
v, err := cast.ToUintE(value)
if err != nil {
return false, fmt.Sprintf("requires integer value >= %d", constants.GetDefaultCPUs(preset))
}
Expand All @@ -69,7 +69,7 @@ func validateCPUs(value interface{}, preset crcpreset.Preset) (bool, string) {
// validateMemory checks if provided memory is valid in the config
// It's defined as a variable so that it can be overridden in tests to disable the physical memory check
var validateMemory = func(value interface{}, preset crcpreset.Preset) (bool, string) {
v, err := cast.ToIntE(value)
v, err := cast.ToUintE(value)
if err != nil {
return false, fmt.Sprintf("requires integer value in MiB >= %d", constants.GetDefaultMemory(preset))
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func GetWin32BackgroundLauncherDownloadURL() string {
version.GetWin32BackgroundLauncherVersion())
}

func GetDefaultCPUs(preset crcpreset.Preset) int {
func GetDefaultCPUs(preset crcpreset.Preset) uint {
switch preset {
case crcpreset.OpenShift, crcpreset.OKD:
return 4
Expand All @@ -211,7 +211,7 @@ func GetDefaultCPUs(preset crcpreset.Preset) int {
}
}

func GetDefaultMemory(preset crcpreset.Preset) int {
func GetDefaultMemory(preset crcpreset.Preset) uint {
switch preset {
case crcpreset.OpenShift, crcpreset.OKD:
return 10752
Expand Down
7 changes: 4 additions & 3 deletions pkg/crc/machine/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ type MachineConfig struct {
BundleName string

// Virtual machine configuration
Name string
Memory int
CPUs int
Name string
// Memory holds value in MiB
Memory uint
CPUs uint
DiskSize int
ImageSourcePath string
ImageFormat string
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/machine/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func updateDriverValue(host *host.Host, setDriverValue valueSetter) error {
return updateDriverConfig(host, driver)
}

func setMemory(host *host.Host, memorySize int) error {
func setMemory(host *host.Host, memorySize uint) error {
memorySetter := func(driver *libmachine.VMDriver) bool {
if driver.Memory == memorySize {
return false
Expand All @@ -33,7 +33,7 @@ func setMemory(host *host.Host, memorySize int) error {
return updateDriverValue(host, memorySetter)
}

func setVcpus(host *host.Host, vcpus int) error {
func setVcpus(host *host.Host, vcpus uint) error {
vcpuSetter := func(driver *libmachine.VMDriver) bool {
if driver.CPU == vcpus {
return false
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/machine/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type StartConfig struct {
BundlePath string

// Hypervisor
Memory int // Memory size in MiB
CPUs int
Memory uint // Memory size in MiB
CPUs uint
DiskSize int // Disk size in GiB

// Nameserver
Expand Down
2 changes: 1 addition & 1 deletion pkg/crc/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func setContextProperty(ctx context.Context, key string, value interface{}) {
}
}

func SetCPUs(ctx context.Context, value int) {
func SetCPUs(ctx context.Context, value uint) {
setContextProperty(ctx, "cpus", value)
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/crc/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (
)

// ValidateCPUs checks if provided cpus count is valid
func ValidateCPUs(value int, preset crcpreset.Preset) error {
func ValidateCPUs(value uint, preset crcpreset.Preset) error {
if value < constants.GetDefaultCPUs(preset) {
return fmt.Errorf("requires CPUs >= %d", constants.GetDefaultCPUs(preset))
}
return nil
}

// ValidateMemory checks if provided Memory count is valid
func ValidateMemory(value int, preset crcpreset.Preset) error {
func ValidateMemory(value uint, preset crcpreset.Preset) error {
if value < constants.GetDefaultMemory(preset) {
return fmt.Errorf("requires memory in MiB >= %d", constants.GetDefaultMemory(preset))
}
Expand All @@ -51,7 +51,7 @@ func ValidatePersistentVolumeSize(value int) error {
}

// ValidateEnoughMemory checks if enough memory is installed on the host
func ValidateEnoughMemory(value int) error {
func ValidateEnoughMemory(value uint) error {
totalMemory := memory.TotalMemory()
logging.Debugf("Total memory of system is %d bytes", totalMemory)
valueBytes := value * 1024 * 1024
Expand Down
2 changes: 1 addition & 1 deletion pkg/drivers/vfkit/driver_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (d *Driver) Start() error {
bootLoader := config.NewEFIBootloader(efiStore, create)

vm := config.NewVirtualMachine(
uint(d.CPU),
d.CPU,
uint64(d.Memory),
bootLoader,
)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ github.com/coreos/go-systemd/v22/daemon
github.com/crc-org/admin-helper/pkg/client
github.com/crc-org/admin-helper/pkg/hosts
github.com/crc-org/admin-helper/pkg/types
# github.com/crc-org/machine v0.0.0-20240715101719-0c1bc9eb95f8
# github.com/crc-org/machine v0.0.0-20240926103419-a943b47fd48b
## explicit; go 1.17
github.com/crc-org/machine/drivers/libvirt
github.com/crc-org/machine/libmachine/drivers
Expand Down

0 comments on commit 7f4bfec

Please sign in to comment.