From a776c1d82a5c99b4d44539c66c0a458032303350 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 27 Mar 2025 15:23:40 -0700 Subject: [PATCH] pkg/machine/vmconfigs: simplify IsFirstBoot This is faster and, to my best knowledge, is equivalent to the old code. Remove the error return (as we don't guarantee stable API here), and simplify callers. Signed-off-by: Kir Kolyshkin --- pkg/machine/apple/apple.go | 7 +------ pkg/machine/hyperv/stubber.go | 7 +------ pkg/machine/shim/host.go | 6 +----- pkg/machine/vmconfigs/machine.go | 8 ++------ 4 files changed, 5 insertions(+), 23 deletions(-) diff --git a/pkg/machine/apple/apple.go b/pkg/machine/apple/apple.go index 4f720b80af..5e0811eb45 100644 --- a/pkg/machine/apple/apple.go +++ b/pkg/machine/apple/apple.go @@ -216,11 +216,6 @@ func StartGenericAppleVM(mc *vmconfigs.MachineConfig, cmdBinary string, bootload cmd.Args = append(cmd.Args, endpointArgs...) - firstBoot, err := mc.IsFirstBoot() - if err != nil { - return nil, nil, err - } - if logrus.IsLevelEnabled(logrus.DebugLevel) { debugDevArgs, err := GetDebugDevicesCMDArgs() if err != nil { @@ -230,7 +225,7 @@ func StartGenericAppleVM(mc *vmconfigs.MachineConfig, cmdBinary string, bootload cmd.Args = append(cmd.Args, "--gui") // add command line switch to pop the gui open } - if firstBoot { + if mc.IsFirstBoot() { // If this is the first boot of the vm, we need to add the vsock // device to vfkit so we can inject the ignition file socketName := fmt.Sprintf("%s-%s", mc.Name, ignitionSocketName) diff --git a/pkg/machine/hyperv/stubber.go b/pkg/machine/hyperv/stubber.go index ed538d006b..13497e55b8 100644 --- a/pkg/machine/hyperv/stubber.go +++ b/pkg/machine/hyperv/stubber.go @@ -182,12 +182,7 @@ func (h HyperVStubber) StartVM(mc *vmconfigs.MachineConfig) (func() error, func( defer callbackFuncs.CleanIfErr(&err) go callbackFuncs.CleanOnSignal() - firstBoot, err := mc.IsFirstBoot() - if err != nil { - return nil, nil, err - } - - if firstBoot { + if mc.IsFirstBoot() { // Add ignition entries to windows registry // for first boot only if err := readAndSplitIgnition(mc, vm); err != nil { diff --git a/pkg/machine/shim/host.go b/pkg/machine/shim/host.go index 1d08fd643d..ad1d9ff4bf 100644 --- a/pkg/machine/shim/host.go +++ b/pkg/machine/shim/host.go @@ -578,11 +578,7 @@ func Start(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, dirs *machineDe } } - isFirstBoot, err := mc.IsFirstBoot() - if err != nil { - logrus.Error(err) - } - if mp.VMType() == machineDefine.WSLVirt && mc.Ansible != nil && isFirstBoot { + if mp.VMType() == machineDefine.WSLVirt && mc.Ansible != nil && mc.IsFirstBoot() { if err := machine.CommonSSHSilent(mc.Ansible.User, mc.SSH.IdentityPath, mc.Name, mc.SSH.Port, []string{"ansible-playbook", mc.Ansible.PlaybookPath}); err != nil { logrus.Error(err) } diff --git a/pkg/machine/vmconfigs/machine.go b/pkg/machine/vmconfigs/machine.go index 256c20ae4a..42a7bfea89 100644 --- a/pkg/machine/vmconfigs/machine.go +++ b/pkg/machine/vmconfigs/machine.go @@ -303,12 +303,8 @@ func (mc *MachineConfig) LogFile() (*define.VMFile, error) { return rtDir.AppendToNewVMFile(mc.Name+".log", nil) } -func (mc *MachineConfig) IsFirstBoot() (bool, error) { - never, err := time.Parse(time.RFC3339, "0001-01-01T00:00:00Z") - if err != nil { - return false, err - } - return mc.LastUp == never, nil +func (mc *MachineConfig) IsFirstBoot() bool { + return mc.LastUp.IsZero() } func (mc *MachineConfig) ConnectionInfo(vmtype define.VMType) (*define.VMFile, *define.VMFile, error) {