Skip to content

Commit 7b16828

Browse files
committed
feat(docker-machine-driver): add automatic retry on machine ID conflict
Signed-off-by: Lukas Kirylak <lukas@stellatarum.com>
1 parent 60d8646 commit 7b16828

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

  • cmd/docker-machine-driver-pve/driver

cmd/docker-machine-driver-pve/driver/driver.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"math/rand"
78
"net"
89
"slices"
910
"strings"
11+
"time"
1012

1113
"github.com/luthermonson/go-proxmox"
1214
"github.com/rancher/machine/libmachine/drivers"
@@ -103,16 +105,34 @@ func (d *Driver) Create() error {
103105

104106
log.Info("Creating the machine...")
105107

106-
vmid, err := d.createPVEVirtualMachine(context.TODO())
107-
if err != nil {
108-
if vmid > 0 {
109-
log.Warn("Machine might have been created with ID='%d'", vmid)
108+
retryBackoff := 1 // seconds
109+
110+
for {
111+
vmid, err := d.createPVEVirtualMachine(context.TODO())
112+
if err != nil {
113+
if strings.Contains(err.Error(), "config file already exists") {
114+
log.Warn("Hit ID conflict when cloning the machine, will retry...")
115+
116+
//nolint:gosec // Weak number generator is good enough for this case
117+
<-time.After(time.Duration(retryBackoff+rand.Intn(retryBackoff)) * time.Second)
118+
119+
// Double the backoff for next iteration
120+
retryBackoff *= 2
121+
122+
continue
123+
}
124+
125+
if vmid > 0 {
126+
log.Warnf("Machine might have been created with ID='%d'", vmid)
127+
}
128+
129+
return fmt.Errorf("failed to create machine: %w", err)
110130
}
111131

112-
return fmt.Errorf("failed to create machine: %w", err)
113-
}
132+
d.PVEMachineID = &vmid
114133

115-
d.PVEMachineID = &vmid
134+
break
135+
}
116136

117137
if err := d.initialize(); err != nil {
118138
if removeErr := d.Remove(); removeErr != nil {

0 commit comments

Comments
 (0)