Skip to content

Commit a422bf0

Browse files
nicolai86stack72
authored andcommitted
provider/scaleway: improve volume attachment (hashicorp#10084)
* provider/scaleway: increase wait for server time according to the scaleway community, shutdown/ startup might actually take an hour. since a regular shutdown transfers data this is bound by the size of the actual volumes in use. https://community.online.net/t/solving-the-long-shutdown-boot-when-only-needed-to-attach-detach-a-volume/326 anyhow, 20 minutes seems quite optimistic, and we've seen some timeout errors in the logs, too * provider/scaleway: clear cache on volume attachment the volume attachment errors quite often, and while I have no hard evidence (yet) I guess it might be related to the cache that the official scaleway SDK includes. for now this is just a tiny experiment, clearing the cache when creating/ destroying volume attachments. let's see if this improves anything, really * provider/scaleway: guard against attaching already attached volumes * provider/scaleway: use cheaper instance types for tests Scaleway bills by the hour and C2S costs much more than C1, since in the tests we just spin up instances, to destroy them later on...
1 parent d190eef commit a422bf0

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

builtin/providers/scaleway/helpers.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ func deleteStoppedServer(scaleway *api.ScalewayAPI, server *api.ScalewayServer)
8484
// the helpers.go file pulls in quite a lot dependencies, and they're just convenience wrappers anyway
8585

8686
func waitForServerState(scaleway *api.ScalewayAPI, serverID, targetState string) error {
87-
return resource.Retry(20*time.Minute, func() *resource.RetryError {
87+
return resource.Retry(60*time.Minute, func() *resource.RetryError {
88+
scaleway.ClearCache()
89+
8890
s, err := scaleway.GetServer(serverID)
8991

9092
if err != nil {

builtin/providers/scaleway/resource_scaleway_volume_attachment.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,20 @@ func resourceScalewayVolumeAttachment() *schema.Resource {
3030
}
3131
}
3232

33+
var errVolumeAlreadyAttached = fmt.Errorf("Scaleway volume already attached")
34+
3335
func resourceScalewayVolumeAttachmentCreate(d *schema.ResourceData, m interface{}) error {
3436
scaleway := m.(*Client).scaleway
37+
scaleway.ClearCache()
3538

36-
var startServerAgain = false
39+
vol, err := scaleway.GetVolume(d.Get("volume").(string))
40+
if err != nil {
41+
return err
42+
}
43+
if vol.Server != nil {
44+
log.Printf("[DEBUG] Scaleway volume %q already attached to %q.", vol.Identifier, vol.Server.Identifier)
45+
return errVolumeAlreadyAttached
46+
}
3747

3848
// guard against server shutdown/ startup race conditiond
3949
serverID := d.Get("server").(string)
@@ -46,6 +56,7 @@ func resourceScalewayVolumeAttachmentCreate(d *schema.ResourceData, m interface{
4656
return err
4757
}
4858

59+
var startServerAgain = false
4960
// volumes can only be modified when the server is powered off
5061
if server.State != "stopped" {
5162
startServerAgain = true
@@ -63,10 +74,6 @@ func resourceScalewayVolumeAttachmentCreate(d *schema.ResourceData, m interface{
6374
volumes[i] = volume
6475
}
6576

66-
vol, err := scaleway.GetVolume(d.Get("volume").(string))
67-
if err != nil {
68-
return err
69-
}
7077
volumes[fmt.Sprintf("%d", len(volumes)+1)] = *vol
7178

7279
// the API request requires most volume attributes to be unset to succeed
@@ -83,6 +90,8 @@ func resourceScalewayVolumeAttachmentCreate(d *schema.ResourceData, m interface{
8390
}
8491

8592
if err := resource.Retry(5*time.Minute, func() *resource.RetryError {
93+
scaleway.ClearCache()
94+
8695
var req = api.ScalewayServerPatchDefinition{
8796
Volumes: &volumes,
8897
}
@@ -121,6 +130,7 @@ func resourceScalewayVolumeAttachmentCreate(d *schema.ResourceData, m interface{
121130

122131
func resourceScalewayVolumeAttachmentRead(d *schema.ResourceData, m interface{}) error {
123132
scaleway := m.(*Client).scaleway
133+
scaleway.ClearCache()
124134

125135
server, err := scaleway.GetServer(d.Get("server").(string))
126136
if err != nil {
@@ -160,6 +170,8 @@ func resourceScalewayVolumeAttachmentRead(d *schema.ResourceData, m interface{})
160170

161171
func resourceScalewayVolumeAttachmentDelete(d *schema.ResourceData, m interface{}) error {
162172
scaleway := m.(*Client).scaleway
173+
scaleway.ClearCache()
174+
163175
var startServerAgain = false
164176

165177
// guard against server shutdown/ startup race conditiond
@@ -204,6 +216,8 @@ func resourceScalewayVolumeAttachmentDelete(d *schema.ResourceData, m interface{
204216
}
205217

206218
if err := resource.Retry(5*time.Minute, func() *resource.RetryError {
219+
scaleway.ClearCache()
220+
207221
var req = api.ScalewayServerPatchDefinition{
208222
Volumes: &volumes,
209223
}

builtin/providers/scaleway/resource_scaleway_volume_attachment_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,22 @@ func testAccCheckScalewayVolumeAttachmentExists(n string) resource.TestCheckFunc
7070
}
7171
}
7272

73-
var x86_64ImageIdentifier = "aecaed73-51a5-4439-a127-6d8229847145"
74-
7573
var testAccCheckScalewayVolumeAttachmentConfig = fmt.Sprintf(`
7674
resource "scaleway_server" "base" {
7775
name = "test"
7876
# ubuntu 14.04
7977
image = "%s"
80-
type = "C2S"
78+
type = "C1"
8179
# state = "stopped"
8280
}
8381
8482
resource "scaleway_volume" "test" {
8583
name = "test"
86-
size_in_gb = 20
84+
size_in_gb = 5
8785
type = "l_ssd"
8886
}
8987
9088
resource "scaleway_volume_attachment" "test" {
9189
server = "${scaleway_server.base.id}"
9290
volume = "${scaleway_volume.test.id}"
93-
}`, x86_64ImageIdentifier)
91+
}`, armImageIdentifier)

0 commit comments

Comments
 (0)