Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add retry for waitForMachineConfigV2 #1375

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 38 additions & 24 deletions rancher2/resource_rancher2_machine_config_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,48 @@ func resourceRancher2MachineConfigV2Create(d *schema.ResourceData, meta interfac
}

func waitForMachineConfigV2(d *schema.ResourceData, config *Config, interval time.Duration) error {
log.Printf("[INFO] Waiting for state Machine Config V2 %s", d.Id())
log.Printf("[INFO] Waiting for state Machine Config V2 %s", d.Id())

ctx, cancel := context.WithTimeout(context.Background(), interval)
defer cancel()
for {
kind := d.Get("kind").(string)
_, err := getMachineConfigV2ByID(config, d.Id(), kind)
if err == nil {
return nil
}
log.Printf("[INFO] Retrying on error Refreshing Machine Config V2 %s: %v", d.Id(), err)
if IsNotFound(err) || IsForbidden(err) {
d.SetId("")
return fmt.Errorf("Machine Config V2 %s not found: %s", d.Id(), err)
}
if IsNotAccessibleByID(err) {
// Restarting clients to update RBAC
config.RestartClients()
}
ctx, cancel := context.WithTimeout(context.Background(), interval)
defer cancel()

select {
case <-time.After(rancher2RetriesWait * time.Second):
case <-ctx.Done():
return fmt.Errorf("Timeout waiting for machine config V2 ID %s", d.Id())
}
}
retryCount := 0
maxRetries := 5
backoffInterval := 5 * time.Second

for {
kind := d.Get("kind").(string)
_, err := getMachineConfigV2ByID(config, d.Id(), kind)
if err == nil {
return nil
}

log.Printf("[INFO] Retrying on error Refreshing Machine Config V2 %s: %v", d.Id(), err)

if IsNotFound(err) || IsForbidden(err) {
retryCount++
if retryCount > maxRetries {
d.SetId("")
return fmt.Errorf("Machine Config V2 %s not found after %d retries: %s", d.Id(), maxRetries, err)
}
log.Printf("[INFO] Encountered 404/403 error, retrying (%d/%d)", retryCount, maxRetries)
time.Sleep(backoffInterval)
continue
}

if IsNotAccessibleByID(err) {
config.RestartClients()
}

select {
case <-time.After(rancher2RetriesWait * time.Second):
case <-ctx.Done():
return fmt.Errorf("Timeout waiting for machine config V2 ID %s", d.Id())
}
}
}


func resourceRancher2MachineConfigV2Read(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Refreshing Machine Config V2 %s", d.Id())

Expand Down
Loading