From 411d1a4c854287515898f53498126b95811613ab Mon Sep 17 00:00:00 2001 From: powerkimhub Date: Thu, 24 Oct 2024 16:06:31 +0900 Subject: [PATCH] Fix infinite waiting in select statement --- .../drivers/gcp/resources/VMHandler.go | 53 +++++++++---------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/cloud-control-manager/cloud-driver/drivers/gcp/resources/VMHandler.go b/cloud-control-manager/cloud-driver/drivers/gcp/resources/VMHandler.go index c109cae0..7c046554 100644 --- a/cloud-control-manager/cloud-driver/drivers/gcp/resources/VMHandler.go +++ b/cloud-control-manager/cloud-driver/drivers/gcp/resources/VMHandler.go @@ -485,45 +485,44 @@ func (vmHandler *GCPVMHandler) StartVM(vmReqInfo irs.VMReqInfo) (irs.VMInfo, err // check operation status, wait until operation is completed // This process is required because some operations have not error message but failed. timeoutDuration := 1 * time.Hour - timeout := time.After(timeoutDuration) + timeout := time.Now().Add(timeoutDuration) retryCount := 0 maxRetries := 3 for { - select { - case <-timeout: + if time.Now().After(timeout) { return irs.VMInfo{}, fmt.Errorf("Operation %s in project %s, zone %s timed out after %v", op.Name, projectID, zone, timeoutDuration) - default: - result, err := vmHandler.Client.ZoneOperations.Get(projectID, zone, op.Name).Context(context.Background()).Do() - if err != nil { - retryCount++ - if retryCount < maxRetries { - cblogger.Infof("Failed to get operation (retry %d/%d): %v. Retrying...", retryCount, maxRetries, err) - continue - } else { - return irs.VMInfo{}, fmt.Errorf("Failed to get operation %s in project %s, zone %s after %d retries: %v", op.Name, projectID, zone, maxRetries, err) - } + } + + result, err := vmHandler.Client.ZoneOperations.Get(projectID, zone, op.Name).Context(context.Background()).Do() + if err != nil { + retryCount++ + if retryCount < maxRetries { + cblogger.Infof("Failed to get operation (retry %d/%d): %v. Retrying...", retryCount, maxRetries, err) + continue } else { - retryCount = 0 + return irs.VMInfo{}, fmt.Errorf("Failed to get operation %s in project %s, zone %s after %d retries: %v", op.Name, projectID, zone, maxRetries, err) } + } else { + retryCount = 0 + } - // result.Status Possible values: "DONE", "PENDING", "RUNNING" - if result.Status == "DONE" { - if result.Error != nil { - var errorMessages []string - for _, err := range result.Error.Errors { - cblogger.Errorf("Operation error: %v", err.Message) - errorMessages = append(errorMessages, err.Message) - } - combinedError := fmt.Errorf("Operation errors: %s", strings.Join(errorMessages, ", ")) - return irs.VMInfo{}, combinedError + // result.Status Possible values: "DONE", "PENDING", "RUNNING" + if result.Status == "DONE" { + if result.Error != nil { + var errorMessages []string + for _, err := range result.Error.Errors { + cblogger.Errorf("Operation error: %v", err.Message) + errorMessages = append(errorMessages, err.Message) } - break + combinedError := fmt.Errorf("Operation errors: %s", strings.Join(errorMessages, ", ")) + return irs.VMInfo{}, combinedError } - - time.Sleep(15 * time.Second) + break } + + time.Sleep(15 * time.Second) } /*