Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #337 from hakman/hetzner_attach_watch_progress
Browse files Browse the repository at this point in the history
Wait for 1 minute for volume to be attached for Hetzner
  • Loading branch information
k8s-ci-robot authored Aug 31, 2022
2 parents 0e4087c + 34e62e4 commit 7dc6849
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions etcd-manager/pkg/volumes/hetzner/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,40 +185,43 @@ func (a *HetznerVolumes) FindMountedVolume(volume *volumes.Volume) (string, erro

// AttachVolume attaches the specified volume to the running server and returns the mountpoint if successful.
func (a *HetznerVolumes) AttachVolume(volume *volumes.Volume) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

volumeID, err := strconv.Atoi(volume.ProviderID)
if err != nil {
return fmt.Errorf("failed to convert volume id %q to int: %w", volume.ProviderID, err)
}

for {
hetznerVolume, _, err := a.hcloudClient.Volume.GetByID(context.TODO(), volumeID)
if err != nil || hetznerVolume == nil {
return fmt.Errorf("failed to get info for volume id %q: %w", volume.ProviderID, err)
}
hetznerVolume, _, err := a.hcloudClient.Volume.GetByID(ctx, volumeID)
if err != nil || hetznerVolume == nil {
return fmt.Errorf("failed to get info for volume id %q: %w", volume.ProviderID, err)
}

if hetznerVolume.Server != nil {
if hetznerVolume.Server.ID != a.server.ID {
return fmt.Errorf("found volume %s(%d) attached to a different server: %d", hetznerVolume.Name, hetznerVolume.ID, hetznerVolume.Server.ID)
}
if hetznerVolume.Server != nil {
if hetznerVolume.Server.ID != a.server.ID {
return fmt.Errorf("found volume %s(%d) attached to a different server: %d", hetznerVolume.Name, hetznerVolume.ID, hetznerVolume.Server.ID)
}

klog.V(2).Infof("Attached volume %s(%d) to the running server", hetznerVolume.Name, hetznerVolume.ID)
klog.V(2).Infof("Attached volume %s(%d) to the running server", hetznerVolume.Name, hetznerVolume.ID)

volume.LocalDevice = fmt.Sprintf("%s%d", localDevicePrefix, hetznerVolume.ID)
return nil
}
volume.LocalDevice = fmt.Sprintf("%s%d", localDevicePrefix, hetznerVolume.ID)
return nil
}

klog.V(2).Infof("Attaching volume %s(%d) to the running server", hetznerVolume.Name, hetznerVolume.ID)
klog.V(2).Infof("Attaching volume %s(%d) to the running server", hetznerVolume.Name, hetznerVolume.ID)

action, _, err := a.hcloudClient.Volume.Attach(context.TODO(), hetznerVolume, a.server)
if err != nil {
return fmt.Errorf("failed to attach volume %s(%d): %w", hetznerVolume.Name, hetznerVolume.ID, err)
}
if action.Status != hcloud.ActionStatusRunning && action.Status != hcloud.ActionStatusSuccess {
return fmt.Errorf("found invalid status for volume %s(%d): %s", hetznerVolume.Name, hetznerVolume.ID, action.Status)
}
action, _, err := a.hcloudClient.Volume.Attach(ctx, hetznerVolume, a.server)
if err != nil {
return fmt.Errorf("failed to attach volume %s(%d): %w", hetznerVolume.Name, hetznerVolume.ID, err)
}

time.Sleep(10 * time.Second)
_, errCh := a.hcloudClient.Action.WatchProgress(ctx, action)
if err := <-errCh; err != nil {
return fmt.Errorf("failed to wait for volume to %s(%d) be ready: %w", hetznerVolume.Name, hetznerVolume.ID, err)
}

return nil
}

// MyIP returns the first private IP of the running server if successful.
Expand Down

0 comments on commit 7dc6849

Please sign in to comment.