Skip to content

Commit

Permalink
Add fix for race condition when creating same folder from multiple wo…
Browse files Browse the repository at this point in the history
…rkers
  • Loading branch information
sneal committed May 4, 2023
1 parent 8ec2304 commit 74ea951
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/vcenter/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func (c *Client) FindVMInClusters(ctx context.Context, azName, vmNameOrPath stri
// CreateFolder creates the specified folder including any parent folders
// If the folder(s) already exists, no folders are created and nil is returned
func (c *Client) CreateFolder(ctx context.Context, folderPath string) error {
l := log.FromContext(ctx)
l.Debugf("Creating folder %s", folderPath)

client, err := c.getOrCreateUnderlyingClient(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -142,7 +145,16 @@ func (c *Client) CreateFolder(ctx context.Context, folderPath string) error {
// folder does not exist create it
nextFolder, err = curFolder.CreateFolder(ctx, p)
if err != nil {
return fmt.Errorf("could not create new sub-folder '%s': %w", nextFolderPath, err)
if strings.Contains(err.Error(), "already exists") {
// likely another worker _just_ created this folder - see if we can _now_ find it
nextFolder, err = finder.Folder(ctx, nextFolderPath)
if err != nil {
return fmt.Errorf("folder '%s' already exists, but can't find it: %w", nextFolderPath, err)
}
l.Debugf("Another worker already created '%s', continuing", nextFolderPath)
} else {
return fmt.Errorf("could not create new sub-folder '%s': %w", nextFolderPath, err)
}
}
} else {
return fmt.Errorf("could not find the target folder '%s': %w", nextFolderPath, err)
Expand Down

0 comments on commit 74ea951

Please sign in to comment.