Skip to content

Commit d9c706e

Browse files
Merge pull request #21694 from arixmkii/wait-for-gvproxy
Extract waitForGvProxy into shared utility function
2 parents 39387c2 + 49400ec commit d9c706e

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

pkg/machine/applehv/stubber.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/containers/podman/v5/utils"
2424
vfConfig "github.com/crc-org/vfkit/pkg/config"
2525
"github.com/sirupsen/logrus"
26-
"golang.org/x/sys/unix"
2726
)
2827

2928
// applehcMACAddress is a pre-defined mac address that vfkit recognizes
@@ -160,7 +159,7 @@ func (a AppleHVStubber) StartVM(mc *vmconfigs.MachineConfig) (func() error, func
160159
}
161160

162161
// Wait on gvproxy to be running and aware
163-
if err := waitForGvProxy(gvproxySocket); err != nil {
162+
if err := sockets.WaitForSocketWithBackoffs(gvProxyMaxBackoffAttempts, gvProxyWaitBackoff, gvproxySocket.GetPath(), "gvproxy"); err != nil {
164163
return nil, nil, err
165164
}
166165

@@ -321,20 +320,6 @@ func (a AppleHVStubber) VMType() define.VMType {
321320
return define.AppleHvVirt
322321
}
323322

324-
func waitForGvProxy(gvproxySocket *define.VMFile) error {
325-
backoffWait := gvProxyWaitBackoff
326-
logrus.Debug("checking that gvproxy is running")
327-
for i := 0; i < gvProxyMaxBackoffAttempts; i++ {
328-
err := unix.Access(gvproxySocket.GetPath(), unix.W_OK)
329-
if err == nil {
330-
return nil
331-
}
332-
time.Sleep(backoffWait)
333-
backoffWait *= 2
334-
}
335-
return fmt.Errorf("unable to connect to gvproxy %q", gvproxySocket.GetPath())
336-
}
337-
338323
func (a AppleHVStubber) PrepareIgnition(_ *vmconfigs.MachineConfig, _ *ignition.IgnitionBuilder) (*ignition.ReadyUnitOpts, error) {
339324
return nil, nil
340325
}

pkg/machine/sockets/sockets.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"fmt"
77
"net"
8+
"os"
89
"path/filepath"
910
"time"
1011

@@ -94,3 +95,18 @@ func DialSocketWithBackoffsAndProcCheck(
9495
}
9596
return nil, err
9697
}
98+
99+
// WaitForSocketWithBackoffs attempts to discover listening socket in maxBackoffs attempts
100+
func WaitForSocketWithBackoffs(maxBackoffs int, backoff time.Duration, socketPath string, name string) error {
101+
backoffWait := backoff
102+
logrus.Debugf("checking that %q socket is ready", name)
103+
for i := 0; i < maxBackoffs; i++ {
104+
_, err := os.Stat(socketPath)
105+
if err == nil {
106+
return nil
107+
}
108+
time.Sleep(backoffWait)
109+
backoffWait *= 2
110+
}
111+
return fmt.Errorf("unable to connect to %q socket at %q", name, socketPath)
112+
}

0 commit comments

Comments
 (0)